Skip to content
Latchkey

Rails "ActiveRecord::PendingMigrationError" in CI

Rails compared schema_migrations to the migration files and found migrations not applied to this database. It refuses to boot until the database is migrated. This is deterministic.

What this error means

The test suite or app boot aborts with PendingMigrationError, telling you to run rails db:migrate. It happens when migrations were added but the test database was not migrated (or loaded from schema) first.

psql
ActiveRecord::PendingMigrationError

Migrations are pending. To resolve this issue, run:

        bin/rails db:migrate RAILS_ENV=test

Common causes

Test database not migrated

New migration files exist but the test database was not brought up to date, so Rails sees pending migrations at boot.

schema.rb / structure.sql out of date

CI loads the schema file to build the test database, but it was not regenerated after the latest migration.

How to fix it

Prepare the database before tests

Terminal
bin/rails db:prepare RAILS_ENV=test
bin/rails test   # or bundle exec rspec

Commit an up-to-date schema

  1. Run bin/rails db:migrate locally so schema.rb/structure.sql updates.
  2. Commit the regenerated schema file with the migration.
  3. CI then loads a current schema and sees no pending migrations.

How to prevent it

  • Always commit the regenerated schema with new migrations.
  • Run db:prepare in CI before the suite.
  • This is deterministic - retrying without migrating fails identically.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →