Skip to content

Commit

Permalink
Create container for migrate (#198)
Browse files Browse the repository at this point in the history
* Create dockerfile for migrate container

* Update compose for new migrate image

* Update migrate readme

* Add image to ci build pipeline

* Add executable

* Remove unecesarry RUN
  • Loading branch information
Teddy-1000 authored Oct 2, 2024
1 parent e5e680c commit d6e5992
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 16 deletions.
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
7 changes: 7 additions & 0 deletions datastore/migrate/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM migrate/migrate:4

COPY data/migrations /migrations

COPY ./run-migrate.sh /

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

1 comment on commit d6e5992

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

API Unit Test Coverage Report
FileStmtsMissCoverMissing
\_\_init\_\_.py00100% 
datastore_pb2.py614821%34–81
datastore_pb2_grpc.py542750%15–16, 19, 65–80, 121–123, 128–130, 135–137, 142–144, 148–173, 219, 246, 273, 300
export_metrics.py100100% 
grpc_getter.py201145%15–19, 23–26, 30–32, 36–38
locustfile.py15150%1–31
main.py43784%45, 50, 60, 70–71, 81–82
metadata_endpoints.py653152%45–54, 58, 85, 100–219, 223
response_classes.py50100% 
utilities.py1744674%20, 38, 45, 67–70, 78–89, 94–101, 121, 125, 127, 155, 161, 179, 193–194, 198, 214–218, 222–228, 232–234, 264, 268, 290, 295
custom_geo_json
   edr_feature_collection.py60100% 
formatters
   \_\_init\_\_.py110100% 
   covjson.py59198%91
   geojson.py21290%27, 52
openapi
   custom_dimension_examples.py40100% 
   edr_query_parameter_descriptions.py110100% 
   openapi_examples.py130100% 
routers
   \_\_init\_\_.py00100% 
   edr.py101496%348–349, 438–439
   feature.py471960%99–132, 148–153, 159–181
TOTAL72021171% 

API Unit Test Coverage Summary

Tests Skipped Failures Errors Time
30 0 💤 0 ❌ 0 🔥 1.899s ⏱️

Please sign in to comment.