2018-02-08 17:40:23 -05:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION tps.srce_set(_name text, _defn jsonb) RETURNS jsonb
|
|
|
|
AS $f$
|
2018-02-15 22:20:24 -05:00
|
|
|
|
|
|
|
DECLARE
|
|
|
|
_cnt int;
|
|
|
|
_conflict BOOLEAN;
|
|
|
|
_message jsonb;
|
2018-02-27 23:18:50 -05:00
|
|
|
_sql text;
|
2018-03-03 09:24:02 -05:00
|
|
|
_cur_sch jsonb;
|
2018-02-15 22:20:24 -05:00
|
|
|
|
2018-02-08 17:40:23 -05:00
|
|
|
BEGIN
|
|
|
|
|
|
|
|
/*
|
|
|
|
1. determine if insert or update
|
|
|
|
2. if update, determine if conflicts exists
|
|
|
|
3. do merge
|
|
|
|
*/
|
|
|
|
|
2018-03-03 09:24:02 -05:00
|
|
|
-------extract current source schema for compare--------------------------
|
|
|
|
SELECT
|
|
|
|
defn->'schema'
|
|
|
|
INTO
|
|
|
|
_cur_sch
|
|
|
|
FROM
|
|
|
|
tps.srce
|
|
|
|
WHERE
|
|
|
|
srce = _name;
|
|
|
|
|
2018-02-15 22:20:24 -05:00
|
|
|
-------check for transctions already existing under this source-----------
|
|
|
|
SELECT
|
|
|
|
COUNT(*)
|
|
|
|
INTO
|
|
|
|
_cnt
|
|
|
|
FROM
|
|
|
|
tps.trans
|
|
|
|
WHERE
|
|
|
|
srce = _name;
|
|
|
|
|
2018-03-03 09:24:02 -05:00
|
|
|
--if there are transaction already and the schema is different stop--------
|
2018-02-15 22:20:24 -05:00
|
|
|
IF _cnt > 0 THEN
|
2018-03-03 09:24:02 -05:00
|
|
|
IF _cur_sch <> _defn->'schema' THEN
|
|
|
|
_conflict = TRUE;
|
|
|
|
--get out of the function somehow
|
|
|
|
_message =
|
|
|
|
$$
|
|
|
|
{
|
|
|
|
"message":"transactions already exist under source profile and there is a pending schema change"
|
|
|
|
,"status":"error"
|
|
|
|
}
|
|
|
|
$$::jsonb;
|
|
|
|
return _message;
|
|
|
|
END IF;
|
2018-02-15 22:20:24 -05:00
|
|
|
END IF;
|
|
|
|
|
2018-02-27 23:18:50 -05:00
|
|
|
/*-------------------------------------------------------
|
2018-03-03 09:24:02 -05:00
|
|
|
do schema validation fo _defn object?
|
2018-02-15 22:20:24 -05:00
|
|
|
---------------------------------------------------------*/
|
|
|
|
|
2018-02-27 23:18:50 -05:00
|
|
|
-------------------insert definition----------------------------------------
|
2018-02-15 22:20:24 -05:00
|
|
|
INSERT INTO
|
|
|
|
tps.srce
|
|
|
|
SELECT
|
|
|
|
_name, _defn
|
2018-02-23 15:11:04 -05:00
|
|
|
ON CONFLICT ON CONSTRAINT srce_pkey DO UPDATE
|
2018-02-15 22:20:24 -05:00
|
|
|
SET
|
|
|
|
defn = _defn;
|
|
|
|
|
2018-02-27 23:18:50 -05:00
|
|
|
------------------drop existing type-----------------------------------------
|
|
|
|
|
|
|
|
EXECUTE format('DROP TYPE IF EXISTS tps.%I',_name);
|
|
|
|
|
|
|
|
------------------create new type--------------------------------------------
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
string_agg(quote_ident(prs.key)||' '||prs.type,',')
|
|
|
|
INTO
|
|
|
|
_sql
|
|
|
|
FROM
|
|
|
|
tps.srce
|
|
|
|
--unwrap the schema definition array
|
|
|
|
LEFT JOIN LATERAL jsonb_populate_recordset(null::tps.srce_defn_schema, defn->'schema') prs ON TRUE
|
|
|
|
WHERE
|
|
|
|
srce = _name
|
|
|
|
GROUP BY
|
|
|
|
srce;
|
|
|
|
|
|
|
|
RAISE NOTICE 'CREATE TYPE tps.% AS (%)',_name,_sql;
|
|
|
|
|
|
|
|
EXECUTE format('CREATE TYPE tps.%I AS (%s)',_name,_sql);
|
|
|
|
|
2018-03-03 09:24:02 -05:00
|
|
|
EXECUTE format('COMMENT ON TYPE tps.%I IS %L',_name,(_defn->>'description'));
|
|
|
|
|
2018-02-27 23:18:50 -05:00
|
|
|
----------------set message-----------------------------------------------------
|
|
|
|
|
2018-02-23 15:11:04 -05:00
|
|
|
_message =
|
|
|
|
$$
|
|
|
|
{
|
|
|
|
"message":"definition set"
|
|
|
|
,"status":"success"
|
|
|
|
}
|
|
|
|
$$::jsonb;
|
|
|
|
return _message;
|
2018-02-08 17:40:23 -05:00
|
|
|
|
|
|
|
END;
|
|
|
|
$f$
|
|
|
|
LANGUAGE plpgsql
|