From 511a38faf9ec40fc5f6e84e224b7271ce4a9cc1a Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 22 Oct 2020 11:42:50 -0400 Subject: [PATCH] prevent inserting null keys, and conflicting keys which is possible with disperate master data (maybe use max?) --- sql/build_master_tables.sql | 2 +- sql/temp.sql | 23 ++++++++++++----------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/sql/build_master_tables.sql b/sql/build_master_tables.sql index cc285de..51d6633 100644 --- a/sql/build_master_tables.sql +++ b/sql/build_master_tables.sql @@ -12,7 +12,7 @@ BEGIN string_agg(cname || ' ' || dtype,', ' ORDER BY opos ASC) || ', PRIMARY KEY ('||string_agg(cname,', ') FILTER (WHERE fkey = func)||'));' AS ddl, ---need to add a clause to exclude where the key is null - 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev' AS populate + 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(cname,'||') FILTER (WHERE fkey = func)||' IS NOT NULL ON CONFLICT DO NOTHING' AS populate FROM fc.target_meta WHERE diff --git a/sql/temp.sql b/sql/temp.sql index 96775b6..0c6db84 100644 --- a/sql/temp.sql +++ b/sql/temp.sql @@ -1,11 +1,12 @@ -SELECT - 'CREATE TABLE IF NOT EXISTS fc.'||func||' (' || - string_agg(cname || ' ' || dtype,', ' ORDER BY opos ASC) || - ', PRIMARY KEY ('||string_agg(cname,', ') FILTER (WHERE fkey = func)||'))' AS ddl, - 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM fc.target' AS populate -FROM - fc.target_meta -WHERE - func <> 'doc' -GROUP BY - func; + SELECT + 'DROP TABLE IF EXISTS fc.'||func||'; CREATE TABLE IF NOT EXISTS fc.'||func||' (' || + string_agg(cname || ' ' || dtype,', ' ORDER BY opos ASC) || + ', PRIMARY KEY ('||string_agg(cname,', ') FILTER (WHERE fkey = func)||'));' AS ddl, + ---need to add a clause to exclude where the key is null + 'INSERT INTO fc.'||func||' SELECT DISTINCT ' || string_agg(cname,', ' ORDER BY opos ASC) || ' FROM rlarp.osm_dev WHERE '||string_agg(cname,'||') FILTER (WHERE fkey = func)||' IS NOT NULL ON CONFLICT DO NOTHING' AS populate + FROM + fc.target_meta + WHERE + func <> 'doc' + GROUP BY + func