Skip to content

Commit

Permalink
Introduce Flyway to manage db schema (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwitkowski authored Jul 30, 2024
1 parent b4ac7ae commit d21219f
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 26 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/db-migration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: DB Migration

on:
push:
branches: [ main ]
paths:
- db/**
pull_request:
branches: [ main ]
paths:
- db/**
workflow_dispatch:

jobs:
db-schema-migration:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- name: 'Test flyway migration scripts'
working-directory: ./db
run: |
./run_migrations_test.sh
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This project aims at reviving www.aero-offers.com - invaluable source of price t
- `job_fetch_offers` - scans few portals (e.g. soaring.de) and stores new offers in the database (not yet classified)
- `job_reclassify_offers` - assigns manufacturer and model to new (not yet classified) offers stored in the database
- `job_update_exchange_rates` - updates currency exchange rates based ok ECP api
- `db` - PostgreSQL 15 database
- `db` - PostgreSQL 15 database with DDL scripts managed by Flyway

### TODO
- use Azure secrets for db credentials
Expand All @@ -35,14 +35,14 @@ This project aims at reviving www.aero-offers.com - invaluable source of price t
- docker (compose)
- npm

Unzip database backup (optional)
Start Postgres in docker (available for debugging via `localhost:25432`):
```bash
cd db && unzip -qq prod_dump_2024_06_31.sql.zip
docker-compose up ca-aerooffers-db flyway
```

Start Postgres in docker (available for debugging via `localhost:25432`):
Unzip database backup (optional) and load into DB
```bash
docker-compose up ca-aerooffers-db
cd db && unzip -qq prod_dump_2024_06_31.sql.zip
```

Install python packages
Expand Down
16 changes: 0 additions & 16 deletions backend/run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
export PYTHONPATH=$PYTHONPATH':./'

export DB_PORT=35432

echo "Starting Postgres docker container"

docker run \
--name test-db \
-d \
-e 'POSTGRES_DB=aircraft_offers' \
-e 'POSTGRES_USER=aircraft_offers' \
-e 'POSTGRES_PASSWORD=aircraft_offers' \
-p ${DB_PORT}':5432' \
-v ${PWD}'/../db/ddl.sql:/docker-entrypoint-initdb.d/ddl.sql' \
postgres:15-alpine

sleep 5

python3 -m unittest -f

if [[ $? -ne 0 ]]; then
Expand Down
2 changes: 0 additions & 2 deletions db/ddl.sql → db/migrations/V1__baseline.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
SET client_encoding = 'UTF8';

CREATE TABLE aircraft_offer (
id SERIAL PRIMARY KEY,
date DATE,
Expand Down
25 changes: 25 additions & 0 deletions db/run_migrations_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
echo "Starting Postgres docker container"

docker network create test-network

docker run --rm \
--name test-db \
-d \
--network test-network \
-e 'POSTGRES_DB=aircraft_offers' \
-e 'POSTGRES_USER=aircraft_offers' \
-e 'POSTGRES_PASSWORD=aircraft_offers' \
postgres:15-alpine

sleep 2

echo "Running migrations"

docker run --rm \
--network test-network \
-v ${PWD}'/migrations/:/flyway/sql' \
flyway/flyway \
-url=jdbc:postgresql://test-db:5432/aircraft_offers -user=aircraft_offers -password=aircraft_offers migrate

docker rm -f test-db
docker network rm test-network
11 changes: 8 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ services:
- DB_PW=aircraft_offers
depends_on:
- ca-aerooffers-db
- flyway

ca-aerooffers-db:
image: postgres:15-alpine
restart: always
environment:
- POSTGRES_DB=aircraft_offers
- POSTGRES_USER=aircraft_offers
- POSTGRES_PASSWORD=aircraft_offers
ports:
- '25432:5432'

flyway:
image: flyway/flyway
command: -url=jdbc:postgresql://ca-aerooffers-db:5432/aircraft_offers -user=aircraft_offers -password=aircraft_offers migrate
volumes:
- ./db/ddl.sql:/docker-entrypoint-initdb.d/ddl.sql
#- ./db/prod_dump_2024_06_31.sql:/docker-entrypoint-initdb.d/prod_dump_2024_06_31.sql
- ./db/migrations:/flyway/sql
depends_on:
- ca-aerooffers-db

0 comments on commit d21219f

Please sign in to comment.