How to Run Analytics Schema Migrations in CI
Running migrations against a fresh CI database proves they apply cleanly from zero before you deploy them.
Bring up an empty database, run your migration tool to head, then optionally test a downgrade so both directions are verified.
Steps
- Start a clean Postgres service.
- Run
alembic upgrade head(orflyway migrate). - Optionally run
alembic downgrade -1to test reversibility.
Workflow
.github/workflows/ci.yml
jobs:
migrate:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env: { POSTGRES_PASSWORD: test }
ports: ['5432:5432']
options: >-
--health-cmd "pg_isready -U postgres" --health-interval 10s --health-retries 5
env:
DATABASE_URL: postgres://postgres:test@localhost:5432/postgres
steps:
- uses: actions/checkout@v4
- run: pip install alembic psycopg2-binary
- run: alembic upgrade head
- run: alembic downgrade -1 && alembic upgrade headGotchas
- Test the migration from an empty database, not just from your local state.
- A downgrade check catches migrations that cannot be rolled back safely.