diff --git a/.github/workflows/registry_upload.yml b/.github/workflows/registry_upload.yml index 63514f6e..7dfe3261 100644 --- a/.github/workflows/registry_upload.yml +++ b/.github/workflows/registry_upload.yml @@ -15,7 +15,6 @@ env: CPU: "arm64" jobs: - on-failure: runs-on: ubuntu-latest if: ${{ github.event.workflow_run.conclusion == 'failure' }} @@ -37,6 +36,8 @@ jobs: IMAGE_NAME: api - context: ./ingest/ IMAGE_NAME: ingest + - context: ./datastore/migrate/ + IMAGE_NAME: migrate steps: - name: Checkout repository uses: actions/checkout@v4 diff --git a/datastore/migrate/Dockerfile b/datastore/migrate/Dockerfile new file mode 100644 index 00000000..62e917f1 --- /dev/null +++ b/datastore/migrate/Dockerfile @@ -0,0 +1,7 @@ +FROM migrate/migrate:4 + +COPY data/migrations /migrations + +COPY ./run-migrate.sh / + +ENTRYPOINT [ "/run-migrate.sh" ] diff --git a/datastore/migrate/README.md b/datastore/migrate/README.md index 7e4353e0..a9d2794b 100644 --- a/datastore/migrate/README.md +++ b/datastore/migrate/README.md @@ -1,17 +1,21 @@ # 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. ``` @@ -21,8 +25,20 @@ The migration file name format follows the suggestion in [MIGRATIONS.md](https:/ 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 path `./migrate/data/migrations` is copied to 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`. + +### Configure migrate image + +There are a few environment variables needed to run the migrate script. + +| Name | Requiered | Explenation | +|------------|------|--------------| +| DB_USER | Yes | The username for the database | +| DB_PASS | Yes | The password for the user | +| DB_URL | Yes | URL or IP to the postgis database | +| DB_PORT | No | Database port, default `5432` | +| ENABLE_SSL | No | Wether to use ssl for connection, defailt `disable`| diff --git a/datastore/migrate/run-migrate.sh b/datastore/migrate/run-migrate.sh new file mode 100755 index 00000000..8b3ca65f --- /dev/null +++ b/datastore/migrate/run-migrate.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +exec migrate -path /migrations -database "postgres://${DB_USER}:${DB_PASS}@${DB_URL}:${DB_PORT:-5432}/data?sslmode=${ENABLE_SSL:-disable}" up diff --git a/docker-compose.yml b/docker-compose.yml index 3e970870..29333ea1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,17 +30,12 @@ services: start_period: 30s # Failures in 30 seconds do not mark container as unhealthy migrate: - image: migrate/migrate:4 - volumes: - - ./datastore/migrate/data/migrations:/data/migrations - command: - [ - "-path", - "/data/migrations", - "-database", - "postgres://postgres:mysecretpassword@db:5432/data?sslmode=disable", - "up", - ] + build: + context: datastore/migrate + environment: + - DB_USER=postgres + - DB_PASS=mysecretpassword + - DB_URL=db depends_on: db: condition: service_healthy