From ba3d9e682e1eb558d3343153b59b59db7b2fc1e1 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 22 Oct 2020 01:22:40 -0400 Subject: [PATCH] initial work on building schema, related tables --- readme.md | 6 ++++++ sql/build_master_tables.sql | 27 +++++++++++++++++++++++++++ sql/schema.sql | 17 +++++++++++++++++ sql/target_info.sql | 22 ++++++++++++++++++++++ sql/temp.sql | 11 +++++++++++ 5 files changed, 83 insertions(+) create mode 100644 readme.md create mode 100644 sql/build_master_tables.sql create mode 100644 sql/schema.sql create mode 100644 sql/target_info.sql create mode 100644 sql/temp.sql diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..b320f83 --- /dev/null +++ b/readme.md @@ -0,0 +1,6 @@ +will need to dynamically build sql for whatever layout the target table is in + +name of table +read columns +assign meaning +create a pretty name for display? diff --git a/sql/build_master_tables.sql b/sql/build_master_tables.sql new file mode 100644 index 0000000..cc285de --- /dev/null +++ b/sql/build_master_tables.sql @@ -0,0 +1,27 @@ +--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 + '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' AS populate + FROM + fc.target_meta + WHERE + func <> 'doc' + GROUP BY + func + loop + EXECUTE format('%s',f.ddl); + EXECUTE format('%s',f.populate); + END LOOP; +END; +$$ diff --git a/sql/schema.sql b/sql/schema.sql new file mode 100644 index 0000000..385efd7 --- /dev/null +++ b/sql/schema.sql @@ -0,0 +1,17 @@ +--assumes schema fc already exists + +DROP TABLE IF EXISTS fc.target_meta; +CREATE TABLE fc.target_meta ( + tname text + ,cname text + ,opos int + ,func text + ,fkey text + ,pretty text + ,dtype text +); + +--ALTER TABLE fc.target_meta DROP CONSTRAINT IF EXISTS target_meta_pk; +ALTER TABLE fc.target_meta ADD CONSTRAINT target_meta_pk PRIMARY KEY (tname, cname); + +COMMENT ON TABLE fc.target_meta IS 'target table layout info'; diff --git a/sql/target_info.sql b/sql/target_info.sql new file mode 100644 index 0000000..ddb0e6a --- /dev/null +++ b/sql/target_info.sql @@ -0,0 +1,22 @@ +BEGIN; + +INSERT INTO + fc.target_meta +SELECT + table_name + ,column_name + ,ordinal_position + ,'doc'::text func + ,null::text fkey --foreign key to a master table + ,null::text pretty + ,data_type::text dtype +FROM + information_schema.columns +WHERE + table_name = 'osm_dev' + AND table_schema = 'rlarp' +ON CONFLICT ON CONSTRAINT target_meta_pk DO UPDATE SET + opos = EXCLUDED.opos + ,dtype = EXCLUDED.dtype; + +END; diff --git a/sql/temp.sql b/sql/temp.sql new file mode 100644 index 0000000..96775b6 --- /dev/null +++ b/sql/temp.sql @@ -0,0 +1,11 @@ +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;