Skip to content

Commit

Permalink
👷(api) test database migration
Browse files Browse the repository at this point in the history
Starting with a pristine database, we now ensure that migrations run.
That's the least we can do.
  • Loading branch information
jmaupetit committed May 20, 2024
1 parent ac0c018 commit a7c27f6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/python-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,46 @@ jobs:
- name: Lint with MyPy
run: pipenv run mypy qualicharge tests

test-database-migrations:
needs: build-api
runs-on: ubuntu-latest
services:
postgresql:
image: timescale/timescaledb-ha:pg14-ts2.14-oss
env:
POSTGRES_DB: test-qualicharge-api
POSTGRES_USER: qualicharge
POSTGRES_PASSWORD: pass
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
defaults:
run:
working-directory: ./src/api
steps:
- uses: actions/checkout@v4
- name: Create postgis extension
run: psql "postgresql://qualicharge:pass@localhost:5432/test-qualicharge-api" -c "create extension postgis;"
- name: Install pipenv
run: pipx install pipenv
- name: Set up Python 3.12
uses: actions/setup-python@v5
with:
python-version: "3.12"
cache: "pipenv"
- name: Run migrations
run: pipenv run alembic -c qualicharge/alembic.ini upgrade head
env:
QUALICHARGE_DB_HOST: localhost
QUALICHARGE_DB_NAME: test-qualicharge-api
QUALICHARGE_TEST_DB_NAME: test-qualicharge-api
# This is a fake setting required to run the app
QUALICHARGE_OIDC_PROVIDER_BASE_URL: http://localhost:8080/fake

test-api:
needs: build-api
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
from typing import Sequence, Union

from alembic import op
from sqlmodel import select
from sqlmodel import Session, select

from qualicharge.db import get_session
from qualicharge.fixtures.operational_units import operational_units
from qualicharge.schemas import Station

Expand All @@ -22,9 +21,6 @@
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None

# Database session
session = next(get_session())


def upgrade():
data_upgrades()
Expand All @@ -37,6 +33,9 @@ def downgrade():
def data_upgrades():
"""Add any optional data upgrade migrations here!"""

# We are running in a transaction, hence we need to get the current active connection
session = Session(op.get_bind())

# Reset table before inserting data
data_downgrades()
session.add_all(operational_units)
Expand All @@ -50,6 +49,9 @@ def data_upgrades():
def data_downgrades():
"""Add any optional data downgrade migrations here!"""

# We are running in a transaction, hence we need to get the current active connection
session = Session(op.get_bind())

# Reset FK
stations = session.exec(select(Station)).all()
for station in stations:
Expand Down

0 comments on commit a7c27f6

Please sign in to comment.