How to Validate Migrations Are Reversible in GitHub Actions
Run up, then down, then up again on a disposable CI database so a broken or missing down migration fails the pipeline, not production.
Apply migrations to the latest revision, roll back one step, then re-apply. If the down step errors or the re-apply diverges, the migration is not safely reversible. Most tools expose this: Rails db:rollback, Alembic downgrade -1, Knex migrate:down.
Steps
- Migrate up to head on a throwaway database.
- Roll back one revision, then migrate up again.
- Fail the job if any step errors.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- run: pip install -r requirements.txt
- name: Up, down, up (Alembic)
run: |
alembic upgrade head
alembic downgrade -1
alembic upgrade head
env:
DATABASE_URL: postgresql+psycopg://postgres:postgres@localhost:5432/app_testGotchas
- Some changes are intentionally irreversible (dropping a column with data); document those and exclude them from the round-trip check.
- Run this against a throwaway database so a failed downgrade does not leave a shared database in a dirty state.
Related guides
How to Detect Schema Drift in GitHub ActionsCatch schema drift in GitHub Actions by checking that the committed migrations fully describe the schema, fai…
How to Dump and Restore a Database in GitHub ActionsCapture a Postgres database with pg_dump and reload it with pg_restore inside a GitHub Actions job, useful fo…
How to Run Database Migrations Before Tests in GitHub ActionsApply schema migrations against the CI database in a step that runs before your test command, so the test sui…