Skip to content
Latchkey

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_test

Gotchas

  • 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

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