Skip to content

Commit

Permalink
Do not auto start
Browse files Browse the repository at this point in the history
  • Loading branch information
rodo committed Dec 12, 2024
1 parent 19aaf84 commit 6cf60bd
Show file tree
Hide file tree
Showing 14 changed files with 630 additions and 128 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ clean:
$(DIST): $(FILES)
cat sql/table.sql > $@
cat sql/function.sql >> $@
cat sql/function-stop.sql >> $@
cat sql/start.sql >> $@
cat $@ > dist/$(EXTENSION).sql

Expand Down
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
161 changes: 131 additions & 30 deletions dist/pgtle.schedoc-0.0.2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ CREATE TYPE schedoc_status AS ENUM ('public', 'private', 'legacy', 'wip');
-- as a foreign key target
--
--
CREATE TABLE schedoc_valid (status boolean NOT NULL PRIMARY KEY CHECK (status = true));
CREATE TABLE @extschema@.schedoc_valid (status boolean NOT NULL PRIMARY KEY CHECK (status = true));
INSERT INTO schedoc_valid (status) VALUES (true);
--
--
--
CREATE TABLE @[email protected]_valid_status (status schedoc_status NOT NULL PRIMARY KEY);
INSERT INTO schedoc_valid_status VALUES ('public'), ('private'), ('legacy'), ('wip');
--
-- The column is_valid references schedoc_status to make sure we have
-- true in this column but in a way that permits to deferre the check
-- of the constraint.
Expand All @@ -25,16 +30,30 @@ INSERT INTO schedoc_valid (status) VALUES (true);
-- enforce that any new column creation must include in the same
-- transaction a COMMENT statement.
--
CREATE TABLE schedoc_column_raw (
CREATE TABLE @extschema@.schedoc_column_raw (
objoid oid,
objsubid oid,
comment jsonb,
is_valid boolean DEFAULT false REFERENCES schedoc_valid (status) DEFERRABLE INITIALLY DEFERRED,
status schedoc_status,
is_valid boolean DEFAULT false REFERENCES @extschema@.schedoc_valid (status) DEFERRABLE INITIALLY DEFERRED,
status schedoc_status REFERENCES @[email protected]_valid_status (status) DEFERRABLE INITIALLY DEFERRED,
PRIMARY KEY (objoid, objsubid)
);

CREATE VIEW schedoc_column_comments AS
--
--
--
CREATE TABLE @[email protected]_column_log (
objoid oid,
objsubid oid,
comment text,
is_valid boolean DEFAULT false,
created_at timestamp with time zone DEFAULT current_timestamp
);
--
--
--

CREATE VIEW @[email protected]_column_comments AS

SELECT current_database() as databasename, c.relname as tablename, a.attname as columnname, status
FROM schedoc_column_raw ccr
Expand All @@ -46,55 +65,101 @@ CREATE VIEW schedoc_column_comments AS
--
--
--
CREATE OR REPLACE FUNCTION schedoc_start()
CREATE OR REPLACE FUNCTION @extschema@.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 $$
CREATE OR REPLACE FUNCTION @[email protected]_trg()
RETURNS trigger LANGUAGE plpgsql AS $fsub$
BEGIN
INSERT INTO %s.schedoc_column_raw (objoid, objsubid, comment, status)

-- keep a log of all values
INSERT INTO @[email protected]_column_log (objoid, objsubid, comment, is_valid)
VALUES (
NEW.objoid,
NEW.objsubid,
@[email protected]_get_column_description(NEW.objoid, NEW.objsubid),
@[email protected]_get_column_description(NEW.objoid, NEW.objsubid) IS JSON
);


-- if the json is valid
IF schedoc_get_column_description(NEW.objoid, NEW.objsubid) IS JSON THEN
INSERT INTO @[email protected]_column_raw (objoid, objsubid, comment, status, is_valid)
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
@[email protected]_get_column_description(NEW.objoid, NEW.objsubid)::jsonb,
@[email protected]_get_column_status(NEW.objoid, NEW.objsubid)::public.schedoc_status,
@[email protected]_get_column_description(NEW.objoid, NEW.objsubid) IS JSON
) 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;
comment = @[email protected]_get_column_description(EXCLUDED.objoid, EXCLUDED.objsubid)::jsonb,
status = @[email protected]_get_column_status(EXCLUDED.objoid, EXCLUDED.objsubid)::public.schedoc_status,
is_valid = @[email protected]_get_column_description(NEW.objoid, NEW.objsubid) IS JSON;
ELSE
--
-- This is not a valid json, we store it
--
INSERT INTO @[email protected]_column_raw (objoid, objsubid, is_valid)
VALUES (
NEW.objoid,
NEW.objsubid,
@[email protected]_get_column_description(NEW.objoid, NEW.objsubid) IS JSON
) ON CONFLICT (objoid, objsubid)
DO UPDATE SET
is_valid = @[email protected]_get_column_description(NEW.objoid, NEW.objsubid) IS JSON;
END IF;
RETURN NEW;
END;
$$', schemaname, schemaname, schemaname, schemaname, schemaname, schemaname);
$fsub$;

--
-- Create two triggers, one for UPDATE and one for INSERT
-- Executed when a new column is created
--
CREATE OR REPLACE FUNCTION @[email protected]_column_trg()
RETURNS trigger LANGUAGE plpgsql AS $fsub$
BEGIN
--
--
INSERT INTO @[email protected]_column_raw (objoid, objsubid, is_valid)
VALUES (
NEW.attrelid,
NEW.attnum,
false
) ON CONFLICT (objoid, objsubid)
DO UPDATE SET
is_valid = false;

RETURN NEW;
END;
$fsub$;

--
-- Create triggers on INSERT
--
CREATE TRIGGER schedoc_comment_trg
BEFORE INSERT ON @[email protected]_history
FOR EACH ROW
WHEN (NEW.ddl_tag = 'COMMENT')
EXECUTE PROCEDURE @[email protected]_trg();

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);
CREATE TRIGGER schedoc_column_trg
BEFORE INSERT ON @[email protected]_history_column
FOR EACH ROW
EXECUTE PROCEDURE @[email protected]_column_trg();

END;
$EOF$;

--
--
--
CREATE OR REPLACE FUNCTION schedoc_get_column_description(bjoid oid, bjsubid oid)
CREATE OR REPLACE FUNCTION @extschema@.schedoc_get_column_description(bjoid oid, bjsubid oid)
RETURNS text
LANGUAGE plpgsql AS
$EOF$
Expand All @@ -110,7 +175,7 @@ $EOF$;
--
--
--
CREATE OR REPLACE FUNCTION schedoc_get_column_status(bjoid oid, bjsubid oid)
CREATE OR REPLACE FUNCTION @extschema@.schedoc_get_column_status(bjoid oid, bjsubid oid)
RETURNS text
LANGUAGE plpgsql AS
$EOF$
Expand All @@ -124,7 +189,43 @@ BEGIN
END;
$EOF$;
--
-- Remove the triggers and the functions to stop the process
--
CREATE OR REPLACE FUNCTION @[email protected]_stop()
RETURNS void
LANGUAGE plpgsql AS
$EOF$
BEGIN
--
-- Remove all triggers
--
DROP TRIGGER schedoc_comment_trg ON @[email protected]_history;
DROP TRIGGER schedoc_column_trg ON @[email protected]_history_column;

--
-- Remove all functions
--
DROP FUNCTION @[email protected]_trg();
DROP FUNCTION @[email protected]_column_trg();

END;
$EOF$;
--
-- Check the schema of installation for schedoc
--
SELECT schedoc_start();
DO
LANGUAGE plpgsql
$check_start$
BEGIN

IF NOT EXISTS (SELECT n.nspname FROM pg_extension e JOIN pg_namespace n ON n.oid=e.extnamespace
WHERE e.extname='ddl_historization' AND n.nspname='@extschema@') THEN

RAISE EXCEPTION 'schedoc must be installed in the same schema as ddl_historization';

END IF;

END;
$check_start$;
$_pg_tle_$
);
Loading

0 comments on commit 6cf60bd

Please sign in to comment.