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=testCommon 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 rspecCommit an up-to-date schema
- Run
bin/rails db:migratelocally soschema.rb/structure.sqlupdates. - Commit the regenerated schema file with the migration.
- 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:preparein CI before the suite. - This is deterministic - retrying without migrating fails identically.
Related guides
Rails "ActiveRecord::NoDatabaseError" in CIFix Rails "ActiveRecord::NoDatabaseError: database X does not exist" in CI - the database was never created.…
Django "You have N unapplied migration(s)" in CIFix Django "You have unapplied migration(s) ... run python manage.py migrate" in CI - the test database was n…
Alembic "Target database is not up to date" in CIFix Alembic "Target database is not up to date" in CI - autogenerate or a check refuses because pending migra…