Skip to content

Commit

Permalink
Improve Makefile
Browse files Browse the repository at this point in the history
  • Loading branch information
rodo committed Dec 11, 2024
1 parent 7a9bae9 commit 8155d85
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 3 deletions.
2 changes: 1 addition & 1 deletion META.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"requires": {
"plpgsql": 0,
"PostgreSQL": "12.0.0",
"ddl_historization": "0.0.7"
"ddl_historization": "0.0.8"
}
}
},
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ SCHEMA = @extschema@

include $(PGXS)

all: $(DIST) $(PGTLEOUT)
all: $(DIST) $(PGTLEOUT) $(EXTENSION).control

clean:
rm -f $(PGTLEOUT) $(DIST)
Expand All @@ -42,3 +42,6 @@ $(PGTLEOUT): dist/$(EXTENSION)--$(EXTVERSION).sql pgtle_header.in pgtle_footer.i

dist: $(PGTLEOUT)
git archive --format zip --prefix=$(EXTENSION)-$(EXTVERSION)/ -o $(EXTENSION)-$(EXTVERSION).zip HEAD

$(EXTENSION).control: $(EXTENSION).control.in META.json
sed 's,EXTVERSION,$(EXTVERSION),g; ' $< > $@;
2 changes: 1 addition & 1 deletion schedoc.control → schedoc.control.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
comment = 'Schema automatic documentation based on ddl_historization and COMMENT'
default_version = '0.0.2'
default_version = 'EXTVERSION'
relocatable = false
module_pathname = '$libdir/pg_schedoc'
requires = 'ddl_historization'
80 changes: 80 additions & 0 deletions sql/function.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
--
--
--
CREATE OR REPLACE FUNCTION schedoc_start()
RETURNS void
LANGUAGE plpgsql AS
$EOF$
DECLARE
schemaname TEXT;
BEGIN
SELECT n.nspname FROM pg_extension e JOIN pg_namespace n ON n.oid=e.extnamespace WHERE e.extname='ddl_historization' INTO schemaname;

--
-- Function to manage INSERT statements
--

EXECUTE format('CREATE OR REPLACE FUNCTION %s.schedoc_trg()
RETURNS trigger LANGUAGE plpgsql AS $$
BEGIN
INSERT INTO %s.schedoc_column_raw (objoid, objsubid, comment, status)
VALUES (
NEW.objoid,
NEW.objsubid,
%s.schedoc_get_column_description(NEW.objoid, NEW.objsubid)::jsonb,
%s.schedoc_get_column_status(NEW.objoid, NEW.objsubid)::public.schedoc_status,
) ON CONFLICT (objoid, objsubid)
DO UPDATE SET
comment = %s.schedoc_get_column_description(EXCLUDED.objoid, EXCLUDED.objsubid)::jsonb,
status = %s.schedoc_get_column_status(EXCLUDED.objoid, EXCLUDED.objsubid)::public.schedoc_status;
RETURN NEW;
END;
$$', schemaname, schemaname, schemaname, schemaname, schemaname, schemaname);

--
-- Create two triggers, one for UPDATE and one for INSERT
--

EXECUTE format('
CREATE TRIGGER schedoc_trg
BEFORE INSERT ON %s.ddl_history
FOR EACH ROW
WHEN (NEW.ddl_tag = ''COMMENT'')
EXECUTE PROCEDURE %s.schedoc_trg()',
schemaname,schemaname);

END;
$EOF$;

--
--
--
CREATE OR REPLACE FUNCTION schedoc_get_column_description(bjoid oid, bjsubid oid)
RETURNS text
LANGUAGE plpgsql AS
$EOF$
DECLARE
description TEXT;
BEGIN
SELECT pg_description.description FROM pg_description
WHERE pg_description.objoid=bjoid AND pg_description.objsubid=bjsubid INTO description;

RETURN description;
END;
$EOF$;
--
--
--
CREATE OR REPLACE FUNCTION schedoc_get_column_status(bjoid oid, bjsubid oid)
RETURNS text
LANGUAGE plpgsql AS
$EOF$
DECLARE
status TEXT;
BEGIN
SELECT pg_description.description::jsonb->>'status' FROM pg_description
WHERE pg_description.objoid=bjoid AND pg_description.objsubid=bjsubid INTO status;

RETURN status;
END;
$EOF$;
50 changes: 50 additions & 0 deletions test/create_column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
--
-- This test must fail
--

SET search_path=public,pgtap;

BEGIN;

SELECT plan(9);

TRUNCATE ddl_history;

-- 2
CREATE TABLE foobar_schedoc (id int);

DROP EXTENSION IF EXISTS schedoc CASCADE;
CREATE EXTENSION schedoc CASCADE;


-- create some objects non concerned by the extension
ALTER TABLE foobar_schedoc ADD COLUMN toto int;
CREATE INDEX ON foobar_schedoc (toto);

--
TRUNCATE ddl_history;
COMMENT ON COLUMN foobar_schedoc.id IS '{"status": "private"}';

--
SELECT results_eq(
'SELECT count(*) FROM ddl_history',
'SELECT CAST(1 as bigint)',
'We have 1 row in ddl_history');

SELECT results_eq(
'SELECT count(*) FROM schedoc_column_raw',
'SELECT CAST(1 as bigint)',
'We have 1 row in schedoc_column_raw');

SELECT results_eq(
'SELECT comment,status::text FROM schedoc_column_raw LIMIT 1',
'SELECT ''{"status": "private"}''::jsonb, ''private''::text ',
'We have right values in schedoc_column_raw');


SELECT results_eq(
'SELECT status::text FROM schedoc_column_raw',
'SELECT ''private''::text ',
'We have right values in schedoc_column_comments');

ROLLBACK;

0 comments on commit 8155d85

Please sign in to comment.