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.
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=testCommon 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.
bin/rails db:prepare RAILS_ENV=test
bin/rails test # or bundle exec rspecCommit an up-to-date schema
- Run
bin/rails db:migratelocally soschema.rb/structure.sqlupdates. - Commit the regenerated schema file alongside the migration.
- CI then loads a current schema and sees no pending migrations.
How to prevent it
- Always commit the regenerated
schema.rb/structure.sqlwith new migrations. - Run
db:preparein CI before the test suite. - Add a CI check that no migrations are pending after preparing the database.