try modifying the mult map set function

This commit is contained in:
Paul Trowbridge 2018-06-08 00:48:07 -04:00
parent e122c4987b
commit 5027c792ae
2 changed files with 78 additions and 40 deletions

View File

@ -10,44 +10,81 @@ DECLARE
BEGIN BEGIN
WITH
INSERT INTO ------------------------------------------stage rows to insert-----------------------------------------------------
tps.map_rm (srce, target, regex, seq, hist) stg AS (
SELECT SELECT
--data source --data source
ae.r->>'srce' ae.r->>'srce' srce
--map name --map name
,ae.r->>'name' ,ae.r->>'name' target
--map definition --map definition
,ae.r ,ae.r regex
--map aggregation sequence --map aggregation sequence
,(ae.r->>'sequence')::INTEGER ,(ae.r->>'sequence')::INTEGER seq
--history definition --history definition
,jsonb_build_object( ,jsonb_build_object(
'hist_defn',ae.r 'hist_defn',ae.r
,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz) ,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz)
) || '[]'::jsonb ) || '[]'::jsonb hist
--determine if the rows are new or match
,(m.regex->>'regex' = ae.r->>'regex')::BOOLEAN rebuild
FROM
jsonb_array_elements(_defn) ae(r)
LEFT OUTER JOIN tps.map_rm m ON
m.srce = ae.r->>'srce'
AND m.target = ae.t->>'name'
)
---------------------------------------do the upsert-------------------------------------------------------------------
,ins AS (
INSERT INTO
tps.map_rm (srce, target, regex, seq, hist)
SELECT
srce
,target
,regex
,seq
,hist
FROM
stg
ON CONFLICT ON CONSTRAINT map_rm_pk DO UPDATE SET
srce = excluded.srce
,target = excluded.target
,regex = excluded.regex
,seq = excluded.seq
,hist =
--the new definition going to position -0-
jsonb_build_object(
'hist_defn',excluded.regex
,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz)
)
--the previous definition, set upper bound of effective range which was previously null
|| jsonb_set(
map_rm.hist
,'{0,effective,1}'::text[]
,to_jsonb(CURRENT_TIMESTAMP)
)
)
---------------------------get list of sources that had maps change--------------------------------------------------------
, to_update AS (
SELECT DISTINCT
srce
FROM FROM
jsonb_array_elements(_defn) ae(r) ins
ON CONFLICT ON CONSTRAINT map_rm_pk DO UPDATE SET WHERE
srce = excluded.srce rebuild = TRUE
,target = excluded.target )
,regex = excluded.regex --------------------------call the map overwrite for each source and return all the messages into message----------------
,seq = excluded.seq /*the whole source must be overwritten because if an element is no longer returned it shoudl be wiped from the data*/
,hist = SELECT
--the new definition going to position -0- jsonb_agg(x.message)
jsonb_build_object( INTO
'hist_defn',excluded.regex _message
,'effective',jsonb_build_array(CURRENT_TIMESTAMP,null::timestamptz) FROM
) to_update
--the previous definition, set upper bound of effective range which was previously null JOIN LATERAL tps.srce_map_overwrite(to_update.srce) AS x(message) ON TRUE;
|| jsonb_set(
map_rm.hist
,'{0,effective,1}'::text[]
,to_jsonb(CURRENT_TIMESTAMP)
);
_message:= jsonb_build_object('status','complete','message','definition has been set'); _message:= jsonb_build_object('status','complete','message','definition has been set');
return _message; return _message;

View File

@ -19,7 +19,7 @@ BEGIN
$$ $$
{ {
"status":"complete", "status":"complete",
"message":"source was not different no action taken" "message":"map was not different no action taken"
} }
$$::jsonb $$::jsonb
); );
@ -69,14 +69,15 @@ BEGIN
INTO INTO
_message _message
FROM FROM
tps.srce_map_overwrite as X(message) tps.srce_map_overwrite as X(message);
EXCEPTION WHEN OTHERS THEN EXCEPTION WHEN OTHERS THEN
GET STACKED DIAGNOSTICS GET STACKED DIAGNOSTICS
_MESSAGE_TEXT = MESSAGE_TEXT, _MESSAGE_TEXT = MESSAGE_TEXT,
_PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL, _PG_EXCEPTION_DETAIL = PG_EXCEPTION_DETAIL,
_PG_EXCEPTION_HINT = PG_EXCEPTION_HINT; _PG_EXCEPTION_HINT = PG_EXCEPTION_HINT;
_message:=
_message:=
($$ ($$
{ {
"status":"fail", "status":"fail",
@ -85,7 +86,7 @@ BEGIN
$$::jsonb) $$::jsonb)
||jsonb_build_object('message_text',_MESSAGE_TEXT) ||jsonb_build_object('message_text',_MESSAGE_TEXT)
||jsonb_build_object('pg_exception_detail',_PG_EXCEPTION_DETAIL); ||jsonb_build_object('pg_exception_detail',_PG_EXCEPTION_DETAIL);
return _message; return _message;