RSpec "Failure/Error: ActiveRecord" - Test Database Errors in CI
An RSpec example failed inside ActiveRecord - the test database is missing, has pending migrations, or its schema does not match schema.rb. The test logic is fine; the database the suite runs against is not prepared.
What this error means
Examples fail with "Failure/Error:" and an ActiveRecord error: PendingMigrationError, StatementInvalid for a missing column/table, or a connection error. It typically follows a migration or a fresh CI database.
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
ActiveRecord::PendingMigrationError:
Migrations are pending. To resolve this issue, run:
bin/rails db:migrate RAILS_ENV=testCommon causes
Pending migrations against the test DB
The test database is behind the migration set. maintain_test_schema! detects the drift and fails every example with PendingMigrationError.
Test database not created or loaded
On a fresh CI database the schema was never loaded, so tables/columns the specs use do not exist, producing StatementInvalid errors.
How to fix it
Prepare the test database in CI
Create and load the schema (and run migrations) before the suite.
RAILS_ENV=test bin/rails db:create db:schema:load
# or, if you rely on migrations
RAILS_ENV=test bin/rails db:migrate
bundle exec rspecKeep schema.rb committed and current
- Commit
db/schema.rb(orstructure.sql) so CI can load it. - Run
db:test:prepareto sync the test DB to the schema before specs. - Ensure the CI service (Postgres/MySQL) is up and the DATABASE_URL points at it.
How to prevent it
- Run
db:schema:load/db:migratefor the test env in CI before RSpec. - Commit
schema.rb/structure.sqlso the test DB is reproducible. - Start the database service and verify connectivity before the suite.