Skip to content
Latchkey

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.

RSpec output
Failure/Error: ActiveRecord::Migration.maintain_test_schema!

ActiveRecord::PendingMigrationError:

Migrations are pending. To resolve this issue, run:
  bin/rails db:migrate RAILS_ENV=test

Common 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.

Terminal
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 rspec

Keep schema.rb committed and current

  1. Commit db/schema.rb (or structure.sql) so CI can load it.
  2. Run db:test:prepare to sync the test DB to the schema before specs.
  3. Ensure the CI service (Postgres/MySQL) is up and the DATABASE_URL points at it.

How to prevent it

  • Run db:schema:load/db:migrate for the test env in CI before RSpec.
  • Commit schema.rb/structure.sql so the test DB is reproducible.
  • Start the database service and verify connectivity before the suite.

Related guides

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