-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from EURODEO/migrate
Add migration framework so that changes to the database are executed once and consistent between environments.
- Loading branch information
Showing
7 changed files
with
91 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/env bash | ||
|
||
CONNECTION_STRING=$1 # Postgres connection string | ||
UPTIME_AMOUNT=${2:-1} # Number of e.g. hours, minutes, seconds | ||
UPTIME_TYPE=${3:-"minute"} # E.g. hour, minute, second | ||
|
||
# Return exit code based on the uptime of postgres | ||
if [[ $(psql "${CONNECTION_STRING}" -XtAc \ | ||
"SELECT COUNT(*) FROM (SELECT current_timestamp - pg_postmaster_start_time() AS uptime) AS t WHERE t.uptime > interval '${UPTIME_AMOUNT} ${UPTIME_TYPE}'") == 1 ]]; | ||
then | ||
exit 0 | ||
else | ||
exit 1 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Migration Framework | ||
To have reproducible environments, support rollbacks and that every change is only executed once, we use [Golang Migrate](https://github.com/golang-migrate/migrate/tree/master) as a migration framework. | ||
|
||
See the following URL for installation instructions and basic commands: | ||
https://github.com/golang-migrate/migrate/tree/master/cmd/migrate | ||
|
||
See the following URL for the migration file format instructions: | ||
https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md | ||
|
||
## Practicalities | ||
### Initialisation | ||
The migration framework initialises the database. Therefore, no database tables exist before running the migrate step in the docker compose. | ||
|
||
### File name format | ||
The migration file name format follows the suggestion in [MIGRATIONS.md](https://github.com/golang-migrate/migrate/blob/master/MIGRATIONS.md) to use a timestamp as version. | ||
|
||
``` | ||
{version}_{title}.up.{extension} | ||
{version}_{title}.down.{extension} | ||
``` | ||
|
||
On Linux, you can retrieve the current timestamp by running: `date +%s`. | ||
|
||
|
||
### Migration Path | ||
The path `./migrate/data/migrations` is mounted on the migrate container. Thus, the docker container only executes the migrations in this path. | ||
|
||
The other path: `./migrate/data/not_supported_yet`, contains an example migration based on code comments about unfinished work from the initialise script. As the path is not mounted, the docker container does not execute migrations in that path. To try out the migrations move the files to `./migrate/data/migrations`. |
5 changes: 5 additions & 0 deletions
5
migrate/data/migrations/1701872471_initialise_schema.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- Commented out the statements below as you never want to undo the initialise. | ||
-- DROP TABLE IF EXISTS observation; | ||
-- DROP TABLE IF EXISTS geo_point; | ||
-- DROP TABLE IF EXISTS time_series; | ||
-- DROP EXTENSION IF EXISTS postgis; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
migrate/data/not_supported_yet/1702281165_geo_polygon.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
22
migrate/data/not_supported_yet/1702281165_geo_polygon.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 --------- |