Compare commits

..

No commits in common. "0f698d562152836d535e5ea3c3528c2fbc53d1c3" and "860b34fad86a8b6a485e41b8a370a6cb09089592" have entirely different histories.

2 changed files with 6 additions and 11 deletions

View File

@ -9,27 +9,22 @@ BEGIN
FOR f IN FOR f IN
SELECT SELECT
-------------------------------------------create table--------------------------------------------------------------------------------------------------------- -------------------------------------------create table---------------------------------------------------------------------------------------------------------
'DROP TABLE IF EXISTS fc.'||func||' CASCADE; CREATE TABLE IF NOT EXISTS fc.'||func||' (' || 'DROP TABLE IF EXISTS fc.'||func||'; CREATE TABLE IF NOT EXISTS fc.'||func||' (' ||
string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY opos ASC) || string_agg(format('%I',cname) || ' ' || dtype,', ' ORDER BY opos ASC) ||
', PRIMARY KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||'));' AS ddl, ', PRIMARY KEY ('||string_agg(format('%I',cname),', ') FILTER (WHERE fkey = func)||'));' AS ddl,
-------------------------------------------populate table------------------------------------------------------------------------------------------------------- -------------------------------------------populate table-------------------------------------------------------------------------------------------------------
---need to add a clause to exclude where the key is null ---need to add a clause to exclude where the key is null
'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY opos ASC) || ' FROM '||tname||' WHERE '|| 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(format('%I',cname),', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||
string_agg(format('%I',cname)||' IS NOT NULL ',' AND ') FILTER (WHERE fkey = func)||' ON CONFLICT DO NOTHING' AS pop, 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
FROM FROM
fc.target_meta fc.target_meta
GROUP BY GROUP BY
tname func
,func
HAVING HAVING
string_agg(cname,', ') FILTER (WHERE fkey = func) <> '' string_agg(cname,', ') FILTER (WHERE fkey = func) <> ''
loop loop
EXECUTE format('%s',f.ddl); EXECUTE format('%s',f.ddl);
EXECUTE format('%s',f.pop); EXECUTE format('%s',f.pop);
EXECUTE format('%s',f.fk);
END LOOP; END LOOP;
END; END;
$$ $$

View File

@ -13,8 +13,8 @@ SELECT
FROM FROM
information_schema.columns information_schema.columns
WHERE WHERE
table_name = 'live' table_name = 'osm_dev'
AND table_schema = 'fc' AND table_schema = 'rlarp'
ON CONFLICT ON CONSTRAINT target_meta_pk DO UPDATE SET ON CONFLICT ON CONSTRAINT target_meta_pk DO UPDATE SET
opos = EXCLUDED.opos opos = EXCLUDED.opos
,dtype = EXCLUDED.dtype; ,dtype = EXCLUDED.dtype;