From 87407e36fcfd4ec8d8dec2c13116fc3f9ff46230 Mon Sep 17 00:00:00 2001 From: Paul Trowbridge Date: Sun, 2 Dec 2018 01:18:29 -0500 Subject: [PATCH] use select in with block and update with join back in order to retain lateral joins in the update --- .../upgrade_scripts/001.extract_schemas.sql | 37 +++++++------------ 1 file changed, 14 insertions(+), 23 deletions(-) diff --git a/database/upgrade_scripts/001.extract_schemas.sql b/database/upgrade_scripts/001.extract_schemas.sql index dba329e..1f5c1d4 100644 --- a/database/upgrade_scripts/001.extract_schemas.sql +++ b/database/upgrade_scripts/001.extract_schemas.sql @@ -1,26 +1,9 @@ -UPDATE - tps.srce -SET - defn = - --delete "schemas" from existing json and tack on revamped layout - jsonb_pretty((defn - 'schemas')|| - --rebuild the schemas key value from below - jsonb_build_object( - 'schemas' - --aggregate all the new key values for a single soure - ,jsonb_agg( - --combine a new key 'name' with the columns for that name - jsonb_build_object('name',k)||jsonb_build_object('columns',v) - ) - )) - - ----------------select statement test----------------------- -/* +WITH +mod AS ( SELECT srce - ,jsonb_pretty(defn) - ,jsonb_pretty((defn - 'schemas')|| + ,jsonb_pretty(defn) orig + ,(defn - 'schemas')|| --rebuild the schemas key value from below jsonb_build_object( 'schemas' @@ -29,11 +12,19 @@ SELECT --combine a new key 'name' with the columns for that name jsonb_build_object('name',k)||jsonb_build_object('columns',v) ) - )) + ) rebuild FROM tps.srce LEFT JOIN LATERAL jsonb_each(defn->'schemas') WITH ORDINALITY je(k,v, rn) ON TRUE GROUP BY srce ,defn -*/ \ No newline at end of file +) +UPDATE + tps.srce s +SET + defn = rebuild +FROM + mod +WHERE + mod.srce = s.srce \ No newline at end of file