2020-10-22 01:22:40 -04:00
- - if the data is already cleansed is it necessary to even have master data tables ? - > yes for adding new scenarios
- - it is possible some parts not longer exist in the item master becuase they have since been deleted , so it is not possible to cleanse the data
do
$ $
DECLARE
f record ;
_sql text ;
2022-04-06 01:52:18 -04:00
_target_table text ;
2020-10-22 01:22:40 -04:00
BEGIN
2022-04-06 01:52:18 -04:00
SELECT format ( ' %I ' , max ( schema ) ) | | ' . ' | | format ( ' %I ' , max ( tname ) ) INTO _target_table FROM fc . target_meta ;
2020-10-22 01:22:40 -04:00
FOR f IN
SELECT
2022-04-06 01:52:18 -04:00
tname ,
func ,
2020-10-22 22:28:23 -04:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - create table - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --
2020-10-22 23:08:11 -04:00
' DROP TABLE IF EXISTS fc. ' | | func | | ' CASCADE; CREATE TABLE IF NOT EXISTS fc. ' | | func | | ' ( ' | |
2020-11-05 22:30:28 -05:00
string_agg ( format ( ' %I ' , cname ) | | ' ' | | dtype , ' , ' ORDER BY CASE WHEN fkey IS NOT NULL THEN 0 ELSE opos END ASC ) | |
2022-04-06 01:52:18 -04:00
' , PRIMARY KEY ( ' | | string_agg ( format ( ' %I ' , cname ) , ' , ' ) FILTER ( WHERE COALESCE ( fkey , ' ' ) < > ' ' ) | | ' )); ' AS ddl ,
2020-10-22 22:28:23 -04:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - populate table - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --
2020-10-22 01:22:40 -04:00
- - - need to add a clause to exclude where the key is null
2022-04-06 01:52:18 -04:00
' INSERT INTO fc. ' | | func | | ' SELECT DISTINCT ' | | string_agg ( format ( ' %I ' , cname ) , ' , ' ORDER BY CASE WHEN fkey IS NOT NULL THEN 0 ELSE opos END ASC ) | | ' FROM ' | | schema | | ' . ' | | tname | | ' WHERE ' | |
string_agg ( format ( ' %I ' , cname ) | | ' IS NOT NULL ' , ' AND ' ) FILTER ( WHERE COALESCE ( fkey , ' ' ) < > ' ' ) | | ' ON CONFLICT DO NOTHING ' AS pop ,
2020-10-22 23:08:11 -04:00
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - setup foreign keys - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --
2022-04-06 01:52:18 -04:00
' ALTER TABLE ' | | tm . schema | | ' . ' | | tm . tname | | ' ADD CONSTRAINT fk_ ' | | func | | ' FOREIGN KEY ( ' | | string_agg ( format ( ' %I ' , cname ) , ' , ' ) FILTER ( WHERE COALESCE ( fkey , ' ' ) < > ' ' ) | | ' ) REFERENCES fc. ' | | func | | ' ( ' | |
string_agg ( format ( ' %I ' , cname ) , ' , ' ) FILTER ( WHERE COALESCE ( fkey , ' ' ) < > ' ' ) | | ' ) ' AS fk
2020-10-22 01:22:40 -04:00
FROM
2022-04-06 01:52:18 -04:00
fc . target_meta tm
WHERE
func IS NOT NULL
2022-04-26 22:50:59 -04:00
- - if func is populated for a date type , a foreign table will be setup unless excluded here or func not populated
- - AND dtype < > ' date '
2020-10-22 01:22:40 -04:00
GROUP BY
2022-04-06 01:52:18 -04:00
schema
, tname
2020-10-22 23:07:17 -04:00
, func
2022-04-06 01:52:18 -04:00
- - HAVING
-- string_agg(cname,', ') FILTER (WHERE fkey = func) <> ''
2020-10-22 01:22:40 -04:00
loop
2022-04-13 01:15:40 -04:00
INSERT INTO fc . sql SELECT f . func , f . ddl ON CONFLICT ON CONSTRAINT sql_pkey DO UPDATE SET t = EXCLUDED . t ;
2020-10-22 01:22:40 -04:00
EXECUTE format ( ' %s ' , f . ddl ) ;
2020-10-22 11:50:15 -04:00
EXECUTE format ( ' %s ' , f . pop ) ;
2020-10-22 23:08:11 -04:00
EXECUTE format ( ' %s ' , f . fk ) ;
2020-10-22 01:22:40 -04:00
END LOOP ;
2022-04-09 02:27:35 -04:00
- - - - - - - add a units column if one doesn ' t exist--------------------------------
IF ( SELECT COUNT ( * ) FROM fc . target_meta WHERE appcol = ' units ' ) = 0 THEN
SELECT ' ALTER TABLE ' | | _target_table | | ' ADD COLUMN app_units numeric DEFAULT 0 ' INTO _sql ;
EXECUTE format ( ' %s ' , _sql ) ;
- - insert the newly created row meta into target_meta
INSERT INTO
fc . target_meta
SELECT
2022-04-12 22:52:42 -04:00
- - schema tname cname opos func fkey fcol dtype appcol pretty
table_schema , table_name , column_name , ordinal_position , null : : text , null : : text , null : : text , ' numeric ' , ' units ' , null : : text
2022-04-09 02:27:35 -04:00
FROM
information_schema . columns
WHERE
format ( ' %I ' , table_schema ) | | ' . ' | | format ( ' %I ' , table_name ) = _target_table
AND column_name = ' app_units ' ;
END IF ;
2022-04-06 01:52:18 -04:00
- - - - - - - add a version column if one doesn ' t exist--------------------------------
IF ( SELECT COUNT ( * ) FROM fc . target_meta WHERE appcol = ' version ' ) = 0 THEN
SELECT ' ALTER TABLE ' | | _target_table | | ' ADD COLUMN app_version text ' INTO _sql ;
EXECUTE format ( ' %s ' , _sql ) ;
- - insert the newly created row meta into target_meta
INSERT INTO
fc . target_meta
SELECT
2022-04-12 22:52:42 -04:00
table_schema , table_name , column_name , ordinal_position , null : : text , null : : text , null : : text , ' text ' , ' version ' , null : : text
2022-04-06 01:52:18 -04:00
FROM
information_schema . columns
WHERE
format ( ' %I ' , table_schema ) | | ' . ' | | format ( ' %I ' , table_name ) = _target_table
AND column_name = ' app_version ' ;
END IF ;
- - - - - - - add a iter column if one doesn ' t exist--------------------------------
IF ( SELECT COUNT ( * ) FROM fc . target_meta WHERE appcol = ' iter ' ) = 0 THEN
SELECT ' ALTER TABLE ' | | _target_table | | ' ADD COLUMN app_iter text ' INTO _sql ;
EXECUTE format ( ' %s ' , _sql ) ;
- - insert the newly created row meta into target_meta
INSERT INTO
fc . target_meta
SELECT
2022-04-12 22:52:42 -04:00
table_schema , table_name , column_name , ordinal_position , null : : text , null : : text , null : : text , ' text ' , ' iter ' , null : : text
2022-04-06 01:52:18 -04:00
FROM
information_schema . columns
WHERE
format ( ' %I ' , table_schema ) | | ' . ' | | format ( ' %I ' , table_name ) = _target_table
AND column_name = ' app_iter ' ;
END IF ;
- - - - - - - add a logid column if one doesn ' t exist--------------------------------
IF ( SELECT COUNT ( * ) FROM fc . target_meta WHERE appcol = ' logid ' ) = 0 THEN
SELECT ' ALTER TABLE ' | | _target_table | | ' ADD COLUMN app_logid text ' INTO _sql ;
EXECUTE format ( ' %s ' , _sql ) ;
- - insert the newly created row meta into target_meta
INSERT INTO
fc . target_meta
SELECT
2022-04-12 22:52:42 -04:00
table_schema , table_name , column_name , ordinal_position , null : : text , null : : text , null : : text , ' bigint ' , ' logid ' , null : : text
2022-04-06 01:52:18 -04:00
FROM
information_schema . columns
WHERE
format ( ' %I ' , table_schema ) | | ' . ' | | format ( ' %I ' , table_name ) = _target_table
AND column_name = ' app_logid ' ;
END IF ;
2020-10-22 01:22:40 -04:00
END ;
$ $