From a657106bf55a302a0d0a577f86777bbe24185700 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Tue, 12 Apr 2022 22:52:42 -0400 Subject: [PATCH] use proper data type for app columns, include master table for app columns --- setup_sql/build_master_tables.sql | 9 +++++---- setup_sql/schema.sql | 32 +++++++++++++++++++++++++++++++ setup_sql/target_info.sql | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/setup_sql/build_master_tables.sql b/setup_sql/build_master_tables.sql index 43bdb2a..b64a2f1 100644 --- a/setup_sql/build_master_tables.sql +++ b/setup_sql/build_master_tables.sql @@ -48,7 +48,8 @@ BEGIN INSERT INTO fc.target_meta SELECT - table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, data_type, 'units', null::text + --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 FROM information_schema.columns WHERE @@ -63,7 +64,7 @@ BEGIN INSERT INTO fc.target_meta SELECT - table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, data_type, 'version', null::text + table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, 'text', 'version', null::text FROM information_schema.columns WHERE @@ -78,7 +79,7 @@ BEGIN INSERT INTO fc.target_meta SELECT - table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, data_type, 'iter', null::text + table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, 'text' , 'iter', null::text FROM information_schema.columns WHERE @@ -93,7 +94,7 @@ BEGIN INSERT INTO fc.target_meta SELECT - table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, data_type, 'logid', null::text + table_schema, table_name, column_name, ordinal_position, null::text, null::text, null::text, 'bigint' , 'logid', null::text FROM information_schema.columns WHERE diff --git a/setup_sql/schema.sql b/setup_sql/schema.sql index 172a5f6..c0f7b8a 100644 --- a/setup_sql/schema.sql +++ b/setup_sql/schema.sql @@ -29,6 +29,38 @@ COMMENT ON COLUMN fc.target_meta.dtype IS 'data type of the sales table column'; COMMENT ON COLUMN fc.target_meta.appcol IS 'supply column name to be used for application variables - (specifcy the order date column)'; COMMENT ON COLUMN fc.target_meta.pretty IS 'the presentation name of the column'; +CREATE TABLE IF NOT EXISTS fc.appcols ( + col text, + dtype text, + required boolean, + dflt text +); +ALTER TABLE fc.appcols DROP CONSTRAINT IF EXISTS appcols_pkey CASCADE; +ALTER TABLE fc.appcols ADD CONSTRAINT appcols_pkey PRIMARY KEY (col, dtype); +COMMENT ON TABLE fc.appcols IS 'hard-coded columns names searched for by the application'; +INSERT INTO + fc.appcols (col, dtype, required, dflt) +VALUES + ('value' ,'numeric',true, null), + ('cost' ,'numeric',true, '0'), + ('units' ,'numeric',true, '0'), + ('order_date' ,'date' ,true, null), + ('ship_date' ,'date' ,false, null), + ('order_status' ,'text' ,true, 'CLOSED'), + ('version' ,'text' ,true, 'ACTUALS'), + ('iter' ,'text' ,true, 'ACTUALS'), + ('logid' ,'bigint' ,true, null), + ('tag' ,'text' ,true, null), + ('comment' ,'text' ,true, null), + ('customer' ,'text' ,false, null), + ('item' ,'text' ,false, null) +ON CONFLICT ON CONSTRAINT appcols_pkey DO UPDATE SET + dtype = EXCLUDED.dtype + ,required = EXCLUDED.required + ,dflt = EXCLUDED.dflt; + +ALTER TABLE fc.target_meta ADD CONSTRAINT fk_appcol FOREIGN KEY (appcol,dtype) REFERENCES fc.appcols(col, dtype); + CREATE TABLE IF NOT EXISTS fc.log ( id int GENERATED ALWAYS AS IDENTITY ,doc jsonb diff --git a/setup_sql/target_info.sql b/setup_sql/target_info.sql index 1b3f542..df9e3bb 100644 --- a/setup_sql/target_info.sql +++ b/setup_sql/target_info.sql @@ -9,7 +9,7 @@ SELECT ,ordinal_position ,null::text func ,null::text fkey --foreign key to a master table - ,column_name fcol + ,null::text fcol ,data_type::text dtype ,null::text appcol ,null::text pretty