Skip to content

Commit

Permalink
Merge pull request #8 from ZeroGachis/status_constraint
Browse files Browse the repository at this point in the history
Make comment mandatory
  • Loading branch information
rodo authored Dec 12, 2024
2 parents 70c8876 + 48fd1fc commit 0d801f4
Show file tree
Hide file tree
Showing 27 changed files with 1,164 additions and 379 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,9 @@ jobs:
run: pg_prove -v --host localhost --dbname postgres --username postgres test/*.sql
env:
PGPASSWORD: postgres

- name: shell tests
working-directory: test/shell
run: ./run.sh
env:
PGPASSWORD: postgres
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*~
schedoc.control
*.zip
6 changes: 3 additions & 3 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "schedoc",
"abstract": "Schema documentation for PostgreSQL based on ddl_historization extension",
"description": "Schema documentation system based on COMMENT with json data and ddl_historization.",
"version": "0.0.1",
"version": "0.0.2",
"maintainer": [
"Rodolphe Quiédeville <[email protected]>"
],
Expand All @@ -14,15 +14,15 @@
"requires": {
"plpgsql": 0,
"PostgreSQL": "12.0.0",
"ddl_historization": "0.0.5"
"ddl_historization": "0.0.8"
}
}
},
"provides": {
"schedoc": {
"abstract": "Schema documentation for PostgreSQL",
"file": "dist/schedoc",
"version": "0.0.1"
"version": "0.0.2"
}
},
"resources": {
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ SCHEMA = @extschema@

include $(PGXS)

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

clean:
rm -f $(PGTLEOUT) $(DIST)

$(DIST): $(FILES)
cat sql/table.sql > $@
cat sql/function.sql >> $@
cat sql/function-stop.sql >> $@
cat sql/function-status.sql >> $@
cat sql/start.sql >> $@
cat $@ > dist/$(EXTENSION).sql

test:
Expand All @@ -40,3 +44,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; ' $< > $@;
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ Comment are parsed and store in a table to make information easy accessible

## Install

There is no other action to do, only CREATE EXTENSION, with CASCADE
the dependency will be automatically created.
Once the extension is installed, call the function `schedoc_start()`
to create the triggers and launch the process.

```
CREATE EXTENSION schedoc CASCADE;
SELECT schedoc_start();
```

## Why schedoc
Expand Down
84 changes: 0 additions & 84 deletions dist/pgtle.schedoc-0.0.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,89 +24,5 @@ CREATE VIEW schedoc_column_comments AS
FROM schedoc_column_raw ccr
JOIN pg_class c ON c.oid = ccr.objoid
JOIN pg_attribute a ON (a.attnum = ccr.objsubid AND a.attrelid = ccr.objoid);

--
--
--
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$;

-- CREATE TRIGGER schedoc_trg
-- BEFORE INSERT ON ddl_history
-- FOR EACH ROW
-- EXECUTE PROCEDURE schedoc_trg();

SELECT schedoc_start();
$_pg_tle_$
);
Loading

0 comments on commit 0d801f4

Please sign in to comment.