extrapolate a view from schema

This commit is contained in:
Paul Trowbridge 2018-05-23 17:18:17 -04:00
parent eb8da0f6c5
commit 80406a2dc6
3 changed files with 35 additions and 36 deletions

View File

@ -11,7 +11,7 @@ COMMENT ON SCHEMA tps IS 'third party source views';
DROP USER IF EXISTS api;
CREATE USER api WITH
CREATE ROLE api WITH
LOGIN
NOSUPERUSER
NOCREATEDB
@ -23,6 +23,9 @@ CREATE USER api WITH
-----need to setup all database objects and then grant priveledges to api----------------------------------------------------------------------------
--grant schema USAGE
GRANT USAGE ON SCHEMA tps TO api;
--grant current table privledges
GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA tps TO api;
GRANT SELECT, UPDATE, INSERT, DELETE ON ALL TABLES IN SCHEMA tpsv TO api;

View File

@ -0,0 +1,28 @@
DO
$f$
DECLARE
_path text[];
_srce text;
_sql text;
BEGIN
_path:= '{schemas,default}'::text[];
_srce:= 'dcard';
SELECT
'CREATE VIEW tpsv.'||_srce||'_'||_path[2]||' AS SELECT '||string_agg('(rec#>>'''||r.PATH::text||''')::'||r.type||' AS "'||r.column_name||'"',', ')||' FROM tps.trans WHERE srce = '''||_srce||''''
INTO
_sql
FROM
tps.srce
JOIN LATERAL jsonb_array_elements(defn#>_path) ae(v) ON TRUE
JOIN LATERAL jsonb_to_record (ae.v) AS r(PATH text[], "type" text, column_name text) ON TRUE
WHERE
srce = _srce
GROUP BY
srce.srce;
RAISE NOTICE '%',_sql;
END
$f$;

View File

@ -1,3 +1,5 @@
--need to build history (trigger)?
DO $f$
DECLARE
@ -54,49 +56,15 @@ BEGIN
$$
INTO
_defn;
/*
validate schema? should not need validation if created by ui
*/
-------extract current source schema for compare--------------------------
SELECT
defn#>'{schemas,default}'
INTO
_cur_sch
FROM
tps.srce
WHERE
srce = _defn->>'name';
/*-------------------------------------------------------
do schema validation
---------------------------------------------------------*/
-------------------insert definition----------------------------------------
INSERT INTO
tps.srce
tps.srce (srce, defn)
SELECT
_defn->>'name', _defn
ON CONFLICT ON CONSTRAINT srce_pkey DO UPDATE
SET
defn = _defn;
_message =
$$
{
"message": "definition set",
"status": "success"
}
$$: :jsonb;
return _message;
END;
$f$