From 293ce4fd24bcfc30aa2920b5e61daf81ea740236 Mon Sep 17 00:00:00 2001 From: Jeffrey Vervoort Date: Mon, 11 Dec 2023 15:53:25 +0100 Subject: [PATCH] Cleanup the migrations. --- .../1701872471_initialise_schema.down.sql | 3 --- .../1701872471_initialise_schema.up.sql | 21 +----------------- .../1702281165_geo_polygon.down.sql | 7 ++++++ .../1702281165_geo_polygon.up.sql | 22 +++++++++++++++++++ 4 files changed, 30 insertions(+), 23 deletions(-) create mode 100644 migrate/data/not_supported_yet/1702281165_geo_polygon.down.sql create mode 100644 migrate/data/not_supported_yet/1702281165_geo_polygon.up.sql diff --git a/migrate/data/migrations/1701872471_initialise_schema.down.sql b/migrate/data/migrations/1701872471_initialise_schema.down.sql index fce56ac..4953ff0 100644 --- a/migrate/data/migrations/1701872471_initialise_schema.down.sql +++ b/migrate/data/migrations/1701872471_initialise_schema.down.sql @@ -1,9 +1,6 @@ -- Commented out the statements below as you never want to undo the initialise. --- DROP TABLE IF EXISTS users; -- DROP TABLE IF EXISTS observation; -- DROP TABLE IF EXISTS geo_point; --- -- not supported yet --- -- DROP TABLE IF EXISTS geo_polygon; -- DROP TABLE IF EXISTS time_series; -- DROP USER IF EXISTS db_user; -- DROP EXTENSION IF EXISTS postgis; diff --git a/migrate/data/migrations/1701872471_initialise_schema.up.sql b/migrate/data/migrations/1701872471_initialise_schema.up.sql index 5ffd4d2..750a89e 100644 --- a/migrate/data/migrations/1701872471_initialise_schema.up.sql +++ b/migrate/data/migrations/1701872471_initialise_schema.up.sql @@ -47,14 +47,6 @@ CREATE TABLE IF NOT EXISTS geo_point ( CREATE INDEX geo_point_idx ON geo_point USING GIST(point); --- not supported yet --- CREATE TABLE IF NOT EXISTS geo_polygon ( --- id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, --- polygon GEOGRAPHY(Polygon, 4326) NOT NULL --- ) --- --- CREATE INDEX geo_polygon_idx ON geo_polygon USING GIST(polygon); - CREATE TABLE IF NOT EXISTS observation ( ts_id BIGINT NOT NULL REFERENCES time_series(id) ON DELETE CASCADE, @@ -64,7 +56,6 @@ CREATE TABLE IF NOT EXISTS observation ( -- Refer to geometry via a foreign key to ensure that each distinct geometry is -- stored only once in the geo_* table, thus speeding up geo search. geo_point_id BIGINT NOT NULL REFERENCES geo_point(id) ON DELETE CASCADE, - -- geo_polygon_id integer NOT NULL REFERENCES geo_polygon(id) ON DELETE CASCADE, -- not supported yet pubtime timestamptz NOT NULL, -- required data_id TEXT NOT NULL, -- required @@ -72,19 +63,9 @@ CREATE TABLE IF NOT EXISTS observation ( metadata_id TEXT NOT NULL, -- required -- --- BEGIN for now support only a single instant for obs time --------- - obstime_instant timestamptz, -- NOT NULL, but implied by being part of PK + obstime_instant timestamptz, -- NOT NULL, but implied by being part of PK; obs time variant 1: single instant -- --- END for now support only a single instant for obs time --------- - -- --- BEGIN support both single instant and interval for obs time --------- - -- obstime_instant timestamptz, -- obs time variant 1: single instant - -- obstime_start timestamptz, -- obs time variant 2: interval - -- obstime_end timestamptz, - -- CHECK ( -- ensure exactly one of [1] obstime_instant and [2] obstime_start/-end is defined - -- ((obstime_instant IS NOT NULL) AND (obstime_start IS NULL) AND (obstime_end IS NULL)) OR - -- ((obstime_instant IS NULL) AND (obstime_start IS NOT NULL) AND (obstime_end IS NOT NULL)) - -- ), - -- --- END support both single instant and interval for obs time --------- - processing_level TEXT, value TEXT NOT NULL, -- obs value -- --- END metadata fields that usually vary with obs time --- diff --git a/migrate/data/not_supported_yet/1702281165_geo_polygon.down.sql b/migrate/data/not_supported_yet/1702281165_geo_polygon.down.sql new file mode 100644 index 0000000..2302967 --- /dev/null +++ b/migrate/data/not_supported_yet/1702281165_geo_polygon.down.sql @@ -0,0 +1,7 @@ +ALTER TABLE observation + DROP COLUMN IF EXISTS geo_polygon_id, + DROP COLUMN IF EXISTS obstime_start, -- obs time variant 2: interval + DROP COLUMN IF EXISTS obstime_end, + DROP CONSTRAINT IF EXISTS observation_chk_one_obs_time; + +DROP TABLE IF EXISTS geo_polygon; diff --git a/migrate/data/not_supported_yet/1702281165_geo_polygon.up.sql b/migrate/data/not_supported_yet/1702281165_geo_polygon.up.sql new file mode 100644 index 0000000..478cc49 --- /dev/null +++ b/migrate/data/not_supported_yet/1702281165_geo_polygon.up.sql @@ -0,0 +1,22 @@ +-- not supported yet +CREATE TABLE IF NOT EXISTS geo_polygon ( + id BIGINT PRIMARY KEY GENERATED ALWAYS AS IDENTITY, + polygon GEOGRAPHY(Polygon, 4326) NOT NULL +); + +CREATE INDEX geo_polygon_idx ON geo_polygon USING GIST(polygon); + +------- BEGIN support both single instant and interval for obs time --------- +-- TODO: Fix geo_polygon_id. How to fill the existing rows, otherwise column cannot be added +-- ALTER TABLE observation +-- ADD geo_polygon_id integer NOT NULL REFERENCES geo_polygon(id) ON DELETE CASCADE; -- not supported yet + +ALTER TABLE observation + ADD obstime_start timestamptz, -- obs time variant 2: interval + ADD obstime_end timestamptz, + ADD CONSTRAINT observation_chk_one_obs_time + CHECK ( -- ensure exactly one of [1] obstime_instant and [2] obstime_start/-end is defined + ((obstime_instant IS NOT NULL) AND (obstime_start IS NULL) AND (obstime_end IS NULL)) OR + ((obstime_instant IS NULL) AND (obstime_start IS NOT NULL) AND (obstime_end IS NOT NULL)) + ); +------- END support both single instant and interval for obs time ---------