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 ;
BEGIN
FOR f IN
SELECT
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 ) | |
2020-10-22 12:24:10 -04:00
' , PRIMARY KEY ( ' | | string_agg ( format ( ' %I ' , cname ) , ' , ' ) FILTER ( WHERE fkey = func ) | | ' )); ' 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
2020-11-05 22:30:28 -05: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 ' | | tname | | ' WHERE ' | |
2020-10-22 23:08:11 -04:00
string_agg ( format ( ' %I ' , cname ) | | ' IS NOT NULL ' , ' AND ' ) FILTER ( WHERE fkey = func ) | | ' ON CONFLICT DO NOTHING ' AS pop ,
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - setup foreign keys - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --
' ALTER TABLE fc.live ADD CONSTRAINT fk_ ' | | func | | ' FOREIGN KEY ( ' | | string_agg ( format ( ' %I ' , cname ) , ' , ' ) FILTER ( WHERE fkey = func ) | | ' ) REFERENCES fc. ' | | func | | ' ( ' | |
string_agg ( format ( ' %I ' , cname ) , ' , ' ) FILTER ( WHERE fkey = func ) | | ' ) ' AS fk
2020-10-22 01:22:40 -04:00
FROM
fc . target_meta
GROUP BY
2020-10-22 23:07:17 -04:00
tname
, func
2020-10-22 12:24:10 -04:00
HAVING
string_agg ( cname , ' , ' ) FILTER ( WHERE fkey = func ) < > ' '
2020-10-22 01:22:40 -04:00
loop
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 ;
END ;
$ $