-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
630 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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 | ||
|
@@ -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$ | ||
|
@@ -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$ | ||
|
@@ -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_$ | ||
); |
Oops, something went wrong.