From cd311ee1ce4299a7280c9e02f8a083152e117822 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Thu, 24 May 2018 01:10:08 -0400 Subject: [PATCH] add function to setup, drop dev set, add test script --- deploy/setup.sql | 71 ++++++++++++++++++++++++- interface/source_maint/srce_set_dev.sql | 71 ------------------------- sample_discovercard/srce_set_test.sql | 39 ++++++++++++++ 3 files changed, 109 insertions(+), 72 deletions(-) delete mode 100644 interface/source_maint/srce_set_dev.sql create mode 100644 sample_discovercard/srce_set_test.sql diff --git a/deploy/setup.sql b/deploy/setup.sql index 1918a0a..06a2f9c 100644 --- a/deploy/setup.sql +++ b/deploy/setup.sql @@ -147,4 +147,73 @@ ALTER TABLE ONLY tps.map_rv ADD CONSTRAINT map_rv_fk_rm FOREIGN KEY (srce, target) REFERENCES tps.map_rm(srce, target); ALTER TABLE ONLY tps.trans - ADD CONSTRAINT trans_srce_fkey FOREIGN KEY (srce) REFERENCES tps.srce(srce); \ No newline at end of file + ADD CONSTRAINT trans_srce_fkey FOREIGN KEY (srce) REFERENCES tps.srce(srce); + +-------------create functions------------------------------------------------------------------------------------------------------------------------ + +CREATE FUNCTION tps.srce_set(_defn jsonb) RETURNS jsonb +AS +$f$ +DECLARE + _message jsonb; + _MESSAGE_TEXT text; + _PG_EXCEPTION_DETAIL text; + _PG_EXCEPTION_HINT text; +BEGIN + INSERT INTO + tps.srce (srce, defn, hist) + SELECT + --extract name from defintion + _defn->>'name' + --add current timestamp to defintions + ,_defn + --add definition + ,jsonb_build_object( + 'hist_defn',_defn + ,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz) + ) || '[]'::jsonb + ON CONFLICT ON CONSTRAINT srce_pkey DO UPDATE + SET + defn = _defn + ,hist = + --the new definition going to position -0- + jsonb_build_object( + 'hist_defn',_defn + ,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz) + ) + --the previous definition, set upper bound of effective range which was previously null + || jsonb_set( + srce.hist + ,'{0,effective,1}'::text[] + ,to_jsonb(CURRENT_TIMESTAMP) + ); + + _message:= + ( + $$ + { + "status":"complete", + "message":"source set" + } + $$::jsonb + ); + RETURN _message; + + EXCEPTION WHEN OTHERS THEN + GET STACKED DIAGNOSTICS + _MESSAGE_TEXT = MESSAGE_TEXT, + _PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL, + _PG_EXCEPTION_HINT = PG_EXCEPTION_HINT; + _message:= + ($$ + { + "status":"fail", + "message":"error importing data" + } + $$::jsonb) + ||jsonb_build_object('message_text',_MESSAGE_TEXT) + ||jsonb_build_object('pg_exception_detail',_PG_EXCEPTION_DETAIL); + RETURN _message; +END; +$f$ +LANGUAGE plpgsql; \ No newline at end of file diff --git a/interface/source_maint/srce_set_dev.sql b/interface/source_maint/srce_set_dev.sql deleted file mode 100644 index 78327d5..0000000 --- a/interface/source_maint/srce_set_dev.sql +++ /dev/null @@ -1,71 +0,0 @@ ---need to build history (trigger)? - -DO $f$ - -DECLARE -_defn jsonb; -_cnt int; -_conflict BOOLEAN; -_message jsonb; -_sql text; -_cur_sch jsonb; - -BEGIN - - SELECT - $$ - { - "name":"dcard", - "source":"client_file", - "loading_function":"csv", - "constraint":[ - "{Trans. Date}", - "{Post Date}" - ], - "schemas":{ - "default":[ - { - "path":"{Trans. Date}", - "type":"date", - "column_name":"Trans. Date" - }, - { - "path":"{Post Date}", - "type":"date", - "column_name":"Post Date" - }, - { - "path":"{Description}", - "type":"text", - "column_name":"Description" - }, - { - "path":"{Amount}", - "type":"numeric", - "column_name":"Amount" - }, - { - "path":"{Category}", - "type":"text", - "column_name":"Category" - } - ], - "version2":[] - } - } - $$ - INTO - _defn; - - -------------------insert definition---------------------------------------- - INSERT INTO - tps.srce (srce, defn) - SELECT - _defn->>'name', _defn - ON CONFLICT ON CONSTRAINT srce_pkey DO UPDATE - SET - defn = _defn; - -END; -$f$ -LANGUAGE plpgsql diff --git a/sample_discovercard/srce_set_test.sql b/sample_discovercard/srce_set_test.sql new file mode 100644 index 0000000..8bf132c --- /dev/null +++ b/sample_discovercard/srce_set_test.sql @@ -0,0 +1,39 @@ +SELECT * FROM TPS.SRCE_SET($${ + "name":"dcard", + "source":"client_file", + "loading_function":"csv", + "constraint":[ + "{Trans. Date}", + "{Post Date}" + ], + "schemas":{ + "default":[ + { + "path":"{Trans. Date}", + "type":"date", + "column_name":"Trans. Date" + }, + { + "path":"{Post Date}", + "type":"date", + "column_name":"Post Date" + }, + { + "path":"{Description}", + "type":"text", + "column_name":"Description" + }, + { + "path":"{Amount}", + "type":"numeric", + "column_name":"Amount" + }, + { + "path":"{Category}", + "type":"text", + "column_name":"Category" + } + ], + "version2":[] + } +}$$::JSONB) \ No newline at end of file