Skip to content

Commit

Permalink
Cleanup the migrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrey-Vervoort-KNMI committed Dec 11, 2023
1 parent f8bf092 commit 293ce4f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 23 deletions.
3 changes: 0 additions & 3 deletions migrate/data/migrations/1701872471_initialise_schema.down.sql
Original file line number Diff line number Diff line change
@@ -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;
21 changes: 1 addition & 20 deletions migrate/data/migrations/1701872471_initialise_schema.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -64,27 +56,16 @@ 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
history TEXT,
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 ---
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
22 changes: 22 additions & 0 deletions migrate/data/not_supported_yet/1702281165_geo_polygon.up.sql
Original file line number Diff line number Diff line change
@@ -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 ---------

0 comments on commit 293ce4f

Please sign in to comment.