Skip to content

Commit

Permalink
Fix exclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
rodo committed Dec 13, 2024
1 parent a92c41a commit aa00e2f
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 277 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $(DIST): $(FILES) exclude.sql
exclude.sql: $(TOOLSEXC)
cat $(TOOLSEXC) > exclude.sql

test:
test: $(UNITTESTS) $(INTETESTS)
pg_prove -f test/sql/*.sql

test/sql/%.sql: test/sql/%.sql.in
Expand Down
161 changes: 98 additions & 63 deletions dist/pgtle.schedoc--0.0.2.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ CREATE TABLE @[email protected]_table_exclusion_templates (
PRIMARY KEY (schema_name, table_name)
);

--
--
--
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
JOIN pg_class c ON c.oid = ccr.objoid
JOIN pg_attribute a ON (a.attnum = ccr.objsubid AND a.attrelid = ccr.objoid);

--
--
--
Expand Down Expand Up @@ -173,6 +163,20 @@ $EOF$;
--
--
--
CREATE OR REPLACE FUNCTION @[email protected]_init_existing_comments()
RETURNS void
LANGUAGE plpgsql AS
$EOF$
DECLARE
status boolean;
BEGIN

PERFORM @[email protected]_fill_raw(objoid, objsubid)
FROM @[email protected]_column_existing_comments
WHERE description IS NOT NULL;

END;
$EOF$;
--
--
--
Expand All @@ -189,44 +193,7 @@ BEGIN
RETURNS trigger LANGUAGE plpgsql AS $fsub$
BEGIN

-- 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 NOT @[email protected]_is_table_excluded(NEW.objoid) THEN
-- 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,
@[email protected]_get_column_description(NEW.objoid, NEW.objsubid)::jsonb,
@[email protected]_get_column_status(NEW.objoid, NEW.objsubid)::@[email protected]_status,
@[email protected]_get_column_description(NEW.objoid, NEW.objsubid) IS JSON
) ON CONFLICT (objoid, objsubid)
DO UPDATE SET
comment = @[email protected]_get_column_description(EXCLUDED.objoid, EXCLUDED.objsubid)::jsonb,
status = @[email protected]_get_column_status(EXCLUDED.objoid, EXCLUDED.objsubid)::@[email protected]_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;
END IF;
PERFORM @[email protected]_fill_raw(NEW.objoid, NEW.objsubid);

RETURN NEW;
END;
Expand All @@ -241,6 +208,7 @@ BEGIN
BEGIN
--
--
IF NOT @[email protected]_is_table_excluded(NEW.attrelid) THEN
INSERT INTO @[email protected]_column_raw (objoid, objsubid, is_valid)
VALUES (
NEW.attrelid,
Expand All @@ -249,7 +217,7 @@ BEGIN
) ON CONFLICT (objoid, objsubid)
DO UPDATE SET
is_valid = false;

END IF;
RETURN NEW;
END;
$fsub$;
Expand All @@ -275,6 +243,58 @@ BEGIN

END;
$EOF$;
--
-- schedoc_fill_raw
--
CREATE OR REPLACE FUNCTION @[email protected]_fill_raw(p_oid oid, p_subid oid)
RETURNS void
LANGUAGE plpgsql AS
$EOF$
BEGIN
--
--
-- keep a log of all values
INSERT INTO @[email protected]_column_log (objoid, objsubid, comment, is_valid)
VALUES (
p_oid,
p_subid,
@[email protected]_get_column_description(p_oid, p_subid),
@[email protected]_get_column_description(p_oid, p_subid) IS JSON
);

IF NOT @[email protected]_is_table_excluded(p_oid) THEN
-- if the json is valid
IF schedoc_get_column_description(p_oid, p_subid) IS JSON THEN
INSERT INTO @[email protected]_column_raw (objoid, objsubid, comment, status, is_valid)
VALUES (
p_oid,
p_subid,
@[email protected]_get_column_description(p_oid, p_subid)::jsonb,
@[email protected]_get_column_status(p_oid, p_subid)::@[email protected]_status,
@[email protected]_get_column_description(p_oid, p_subid) IS JSON
) ON CONFLICT (objoid, objsubid)
DO UPDATE SET
comment = @[email protected]_get_column_description(EXCLUDED.objoid, EXCLUDED.objsubid)::jsonb,
status = @[email protected]_get_column_status(EXCLUDED.objoid, EXCLUDED.objsubid)::@[email protected]_status,
is_valid = @[email protected]_get_column_description(p_oid, p_subid) IS JSON;
ELSE
--
-- This is not a valid json, we store it
--
INSERT INTO @[email protected]_column_raw (objoid, objsubid, is_valid)
VALUES (
p_oid,
p_subid,
@[email protected]_get_column_description(p_oid, p_subid) IS JSON
) ON CONFLICT (objoid, objsubid)
DO UPDATE SET
is_valid = @[email protected]_get_column_description(EXCLUDED.objoid, EXCLUDED.objsubid) IS JSON;
END IF;
END IF;
END;
$EOF$;



--
--
Expand Down Expand Up @@ -349,31 +369,46 @@ BEGIN
END IF;
END;
$EOF$;
--
--
--

CREATE VIEW schedoc_column_comments AS

SELECT current_database() as databasename, c.relname as tablename, a.attname as columnname, status
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 VIEW schedoc_object_tables AS

SELECT n.nspname, c.relname
FROM pg_depend d
JOIN pg_class c ON c.oid = d.objid
JOIN pg_namespace n ON n.oid = c.relnamespace
JOIN pg_extension e ON e.oid = d.refobjid
WHERE e.extname in ('schedoc', 'ddl_historization')
AND c.relkind = 'r';
--
--
--
DROP VIEW IF EXISTS schedoc_column_existing_comments;
CREATE OR REPLACE VIEW schedoc_column_existing_comments AS


WITH descr AS (
SELECT c.relname, d.description
SELECT c.oid as objoid, d.objsubid, c.relname, d.description
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
LEFT JOIN pg_description d ON d.objoid = c.oid
LEFT JOIN schedoc_table_exclusion e ON e.table_name = c.relname
WHERE c.relkind = 'r' AND n.nspname='public'
AND e.table_name IS NULL
AND c.relname NOT IN (

'ddl_history',
'ddl_history_column',
'ddl_history_schema',
'schedoc_column_log',
'schedoc_column_raw',
'schedoc_table_exclusion',
'schedoc_table_exclusion_templates',
'schedoc_valid',
'schedoc_valid_status')
AND c.relname NOT IN (
SELECT relname FROM @[email protected]_object_tables)
)
SELECT relname, description, description IS NOT NULL AND description IS JSON as is_ok
SELECT objoid, objsubid, relname, description, description IS NOT NULL AND description IS JSON as is_ok
FROM descr

ORDER BY relname
Expand Down
Loading

0 comments on commit aa00e2f

Please sign in to comment.