How to Detect Schema Drift in GitHub Actions
Drift means the schema and the committed migrations disagree; CI catches it by diffing the migrated database against the model and failing on any difference.
After applying migrations, ask the tool whether the model would generate further changes. A non-empty diff means someone changed the model without adding a migration. Prisma exposes migrate diff --exit-code; Django uses makemigrations --check.
Steps
- Apply committed migrations to a throwaway database.
- Diff the model against the migrated schema with
--exit-code. - Fail the job when the diff is non-empty.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npx prisma migrate deploy
env: { DATABASE_URL: postgres://postgres:postgres@localhost:5432/app_test }
- name: Fail on schema drift
run: |
npx prisma migrate diff \
--from-url "$DATABASE_URL" \
--to-schema-datamodel prisma/schema.prisma \
--exit-code
env: { DATABASE_URL: postgres://postgres:postgres@localhost:5432/app_test }Gotchas
prisma migrate diff --exit-codereturns 2 when there is a diff; treat any non-zero exit as drift.- For Django,
python manage.py makemigrations --check --dry-runfails when model changes lack a migration.
Related guides
How to Validate Migrations Are Reversible in GitHub ActionsVerify a new migration can roll back cleanly in GitHub Actions by applying it, rolling it back one step, and…
How to Run Prisma Migrations in GitHub ActionsApply Prisma migrations in GitHub Actions with prisma migrate deploy against the CI database, the non-interac…
How to Lint SQL With SQLFluff in GitHub ActionsRun SQLFluff in GitHub Actions to lint SQL and migration files for style and correctness, failing the build o…