From 487c297747682bfc524fa2b2f820f4596aca77ea Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Sat, 19 Jul 2025 12:20:55 +0200 Subject: [PATCH] Add database upgrade file 37. Fixes database upgrade file 35. --- ...-v0.5.2-fix-preplot_lines_summary-view.sql | 3 + ...-v0.5.4-fix-preplot_lines_summary-view.sql | 145 ++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 etc/db/upgrades/upgrade37-v0.5.4-fix-preplot_lines_summary-view.sql diff --git a/etc/db/upgrades/upgrade35-v0.5.2-fix-preplot_lines_summary-view.sql b/etc/db/upgrades/upgrade35-v0.5.2-fix-preplot_lines_summary-view.sql index 9617e59..e9a0913 100644 --- a/etc/db/upgrades/upgrade35-v0.5.2-fix-preplot_lines_summary-view.sql +++ b/etc/db/upgrades/upgrade35-v0.5.2-fix-preplot_lines_summary-view.sql @@ -2,6 +2,9 @@ -- -- New schema version: 0.5.2 -- +-- WARNING: This update is buggy and does not give the desired +-- results. Schema version 0.5.4 fixes this. +-- -- ATTENTION: -- -- ENSURE YOU HAVE BACKED UP THE DATABASE BEFORE RUNNING THIS SCRIPT. diff --git a/etc/db/upgrades/upgrade37-v0.5.4-fix-preplot_lines_summary-view.sql b/etc/db/upgrades/upgrade37-v0.5.4-fix-preplot_lines_summary-view.sql new file mode 100644 index 0000000..4120178 --- /dev/null +++ b/etc/db/upgrades/upgrade37-v0.5.4-fix-preplot_lines_summary-view.sql @@ -0,0 +1,145 @@ +-- Fix preplot_lines_summary view +-- +-- New schema version: 0.5.4 +-- +-- ATTENTION: +-- +-- ENSURE YOU HAVE BACKED UP THE DATABASE BEFORE RUNNING THIS SCRIPT. +-- +-- +-- NOTE: This upgrade affects all schemas in the database. +-- NOTE: Each application starts a transaction, which must be committed +-- or rolled back. +-- +-- Fixes upgrade 35 (0.5.2). The original description of 0.5.2 is included +-- below for ease of reference: +-- +-- Following introduction of `preplot_saillines` (0.5.0), the incr and +-- ntba statuses are stored in a separate table, not in `preplot_lines` +-- (TODO: a future upgrade should remove those columns from `preplot_lines`) +-- +-- Now any views referencing `incr` and `ntba` must be updated to point to +-- the new location of those attributes. +-- +-- This update fixes #312. +-- +-- To apply, run as the dougal user: +-- +-- psql <>'db_schema' INTO current_db_version FROM public.info WHERE key = 'version'; + + IF current_db_version >= '0.5.4' THEN + RAISE EXCEPTION + USING MESSAGE='Patch already applied'; + END IF; + + IF current_db_version != '0.5.3' THEN + RAISE EXCEPTION + USING MESSAGE='Invalid database version: ' || current_db_version, + HINT='Ensure all previous patches have been applied.'; + END IF; + + FOR row IN + SELECT schema_name FROM information_schema.schemata + WHERE schema_name LIKE 'survey_%' + ORDER BY schema_name + LOOP + CALL pg_temp.upgrade_survey_schema(row.schema_name); + END LOOP; +END; +$outer$ LANGUAGE plpgsql; + +CALL pg_temp.upgrade(); + +CALL pg_temp.show_notice('Cleaning up'); +DROP PROCEDURE pg_temp.upgrade_survey_schema (schema_name text); +DROP PROCEDURE pg_temp.upgrade (); + +CALL pg_temp.show_notice('Updating db_schema version'); +INSERT INTO public.info VALUES ('version', '{"db_schema": "0.5.4"}') +ON CONFLICT (key) DO UPDATE + SET value = public.info.value || '{"db_schema": "0.5.4"}' WHERE public.info.key = 'version'; + + +CALL pg_temp.show_notice('All done. You may now run "COMMIT;" to persist the changes'); +DROP PROCEDURE pg_temp.show_notice (notice text); + +-- +--NOTE Run `COMMIT;` now if all went well +--