Skip to content
Latchkey

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 (or flyway migrate).
  • Optionally run alembic downgrade -1 to 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 head

Gotchas

  • Test the migration from an empty database, not just from your local state.
  • A downgrade check catches migrations that cannot be rolled back safely.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →