Skip to content
Latchkey

Rails "ActiveRecord::PendingMigrationError" in CI

Rails compared the schema_migrations table to the migration files and found migrations that have not been applied to this database. It refuses to boot the app until the database is migrated.

What this error means

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

Rails output
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 never 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, leaving the database behind the migrations.

How to fix it

Prepare the database before tests

Run db:prepare (or db:migrate) in the test environment as a setup step.

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 alongside the migration.
  3. CI then loads a current schema and sees no pending migrations.

How to prevent it

  • Always commit the regenerated schema.rb/structure.sql with new migrations.
  • Run db:prepare in CI before the test suite.
  • Add a CI check that no migrations are pending after preparing the database.

Related guides

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