From 80406a2dc691dd773abf4e8618e37de36d1f5424 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Wed, 23 May 2018 17:18:17 -0400 Subject: [PATCH] extrapolate a view from schema --- deploy/setup.sql | 5 ++- interface/source_maint/srce_build_view.sql | 28 ++++++++++++++++ interface/source_maint/srce_set_dev.sql | 38 ++-------------------- 3 files changed, 35 insertions(+), 36 deletions(-) create mode 100644 interface/source_maint/srce_build_view.sql diff --git a/deploy/setup.sql b/deploy/setup.sql index 3a58aad..a76a2d6 100644 --- a/deploy/setup.sql +++ b/deploy/setup.sql @@ -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; diff --git a/interface/source_maint/srce_build_view.sql b/interface/source_maint/srce_build_view.sql new file mode 100644 index 0000000..00ed3c3 --- /dev/null +++ b/interface/source_maint/srce_build_view.sql @@ -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$; diff --git a/interface/source_maint/srce_set_dev.sql b/interface/source_maint/srce_set_dev.sql index caf91f4..78327d5 100644 --- a/interface/source_maint/srce_set_dev.sql +++ b/interface/source_maint/srce_set_dev.sql @@ -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$