diff --git a/deploy/ubm_schema.sql b/deploy/ubm_schema.sql index 2f12333..50299be 100644 --- a/deploy/ubm_schema.sql +++ b/deploy/ubm_schema.sql @@ -30,6 +30,19 @@ COMMENT ON SCHEMA tps IS 'third party source'; SET search_path = tps, pg_catalog; +-- +-- Name: DCARD; Type: TYPE; Schema: tps; Owner: - +-- + +CREATE TYPE "DCARD" AS ( + "Trans. Date" date, + "Post Date" date, + "Description" text, + "Amount" numeric, + "Category" text +); + + -- -- Name: dcard; Type: TYPE; Schema: tps; Owner: - -- @@ -362,6 +375,7 @@ DECLARE _cnt int; _conflict BOOLEAN; _message jsonb; +_sql text; BEGIN @@ -395,10 +409,11 @@ BEGIN return _message; END IF; - /*-----------------schema validation--------------------- - yeah dont feel like it right now + /*------------------------------------------------------- + schema validation ---------------------------------------------------------*/ + -------------------insert definition---------------------------------------- INSERT INTO tps.srce SELECT @@ -407,6 +422,31 @@ BEGIN SET defn = _defn; + ------------------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); + + ----------------set message----------------------------------------------------- + _message = $$ { diff --git a/functions/srce_edit.sql b/functions/srce_edit.sql index cbfae26..e2eef8c 100644 --- a/functions/srce_edit.sql +++ b/functions/srce_edit.sql @@ -6,6 +6,7 @@ DECLARE _cnt int; _conflict BOOLEAN; _message jsonb; +_sql text; BEGIN @@ -39,10 +40,11 @@ BEGIN return _message; END IF; - /*-----------------schema validation--------------------- - yeah dont feel like it right now + /*------------------------------------------------------- + schema validation ---------------------------------------------------------*/ + -------------------insert definition---------------------------------------- INSERT INTO tps.srce SELECT @@ -51,6 +53,31 @@ BEGIN SET defn = _defn; + ------------------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); + + ----------------set message----------------------------------------------------- + _message = $$ {