Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create container for migrate #198

Merged
merged 6 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/registry_upload.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ env:
CPU: "arm64"

jobs:

on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
Expand All @@ -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
Expand Down
9 changes: 9 additions & 0 deletions datastore/migrate/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM migrate/migrate:4

COPY data/migrations /migrations

COPY ./run-migrate.sh /

RUN chmod +x /run-migrate.sh
Teddy-1000 marked this conversation as resolved.
Show resolved Hide resolved

ENTRYPOINT [ "/run-migrate.sh" ]
24 changes: 20 additions & 4 deletions datastore/migrate/README.md
Original file line number Diff line number Diff line change
@@ -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
<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
<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.

```
Expand All @@ -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`|
3 changes: 3 additions & 0 deletions datastore/migrate/run-migrate.sh
Original file line number Diff line number Diff line change
@@ -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
17 changes: 6 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down