diff --git a/.gitignore b/.gitignore index c6114bc..692065f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ .dbeaver-data-sources.xml .project Scripts/ +*.log +*.swp diff --git a/database/templates/srce.json b/database/templates/srce.json index bea9cbd..daaf1e4 100644 --- a/database/templates/srce.json +++ b/database/templates/srce.json @@ -7,70 +7,76 @@ "{Post Date}", "{Description}" ], - "schemas": { - "default": [ - { - "path": "{Trans. Date}", - "type": "date", - "column_name": "Trans. Date" - }, - { - "path": "{Post Date}", - "type": "date", - "column_name": "Post Date" - }, - { - "path": "{Description}", - "type": "text", - "column_name": "Description" - }, - { - "path": "{Amount}", - "type": "numeric", - "column_name": "Amount" - }, - { - "path": "{Category}", - "type": "text", - "column_name": "Category" - } - ], - "mapped": [ - { - "path": "{Trans. Date}", - "type": "date", - "column_name": "Trans. Date" - }, - { - "path": "{Post Date}", - "type": "date", - "column_name": "Post Date" - }, - { - "path": "{Description}", - "type": "text", - "column_name": "Description" - }, - { - "path": "{Amount}", - "type": "numeric", - "column_name": "Amount" - }, - { - "path": "{Category}", - "type": "text", - "column_name": "Category" - }, - { - "path": "{party}", - "type": "text", - "column_name": "Party" - }, - { - "path": "{reason}", - "type": "text", - "column_name": "Reason" - } - ] - } + "schemas": [ + { + "name": "default", + "columns": [ + { + "path": "{Trans. Date}", + "type": "date", + "column_name": "Trans. Date" + }, + { + "path": "{Post Date}", + "type": "date", + "column_name": "Post Date" + }, + { + "path": "{Description}", + "type": "text", + "column_name": "Description" + }, + { + "path": "{Amount}", + "type": "numeric", + "column_name": "Amount" + }, + { + "path": "{Category}", + "type": "text", + "column_name": "Category" + } + ] + }, + { + "name": "mapped", + "columns": [ + { + "path": "{Trans. Date}", + "type": "date", + "column_name": "Trans. Date" + }, + { + "path": "{Post Date}", + "type": "date", + "column_name": "Post Date" + }, + { + "path": "{Description}", + "type": "text", + "column_name": "Description" + }, + { + "path": "{Amount}", + "type": "numeric", + "column_name": "Amount" + }, + { + "path": "{Category}", + "type": "text", + "column_name": "Category" + }, + { + "path": "{party}", + "type": "text", + "column_name": "Party" + }, + { + "path": "{reason}", + "type": "text", + "column_name": "Reason" + } + ] + } + ] } \ No newline at end of file diff --git a/database/upgrade_scripts/001.extract_schemas.sql b/database/upgrade_scripts/001.extract_schemas.sql new file mode 100644 index 0000000..1f5c1d4 --- /dev/null +++ b/database/upgrade_scripts/001.extract_schemas.sql @@ -0,0 +1,30 @@ +WITH +mod AS ( +SELECT + srce + ,jsonb_pretty(defn) orig + ,(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) + ) + ) rebuild +FROM + tps.srce + LEFT JOIN LATERAL jsonb_each(defn->'schemas') WITH ORDINALITY je(k,v, rn) ON TRUE +GROUP BY + srce + ,defn +) +UPDATE + tps.srce s +SET + defn = rebuild +FROM + mod +WHERE + mod.srce = s.srce \ No newline at end of file