Fix ActiveRecord::PendingMigrationError: Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development in Rails
This error means your database schema is out of sync with your migration files. Rails detected migrations that have not been applied yet. Run bin/rails db:migrate to apply pending migrations and update your schema. This commonly happens after pulling new code that includes database changes from teammates.
Reading the Stack Trace
Here's what each line means:
- activerecord (7.1.3) lib/active_record/migration.rb:621:in `check_pending!': Rails checks for pending migrations on every request in development mode and raises this error if any are found.
- actionpack (7.1.3) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call': The migration check runs as middleware before your request reaches the controller.
- actionpack (7.1.3) lib/action_dispatch/middleware/executor.rb:14:in `call': The executor middleware wraps the request lifecycle where the pending check is triggered.
Common Causes
1. New migration files not yet applied
A teammate added a migration and you pulled the code, but did not run db:migrate to apply it locally.
# db/migrate/20240101120000_add_status_to_orders.rb exists but has not been run
class AddStatusToOrders < ActiveRecord::Migration[7.1]
def change
add_column :orders, :status, :string, default: 'pending'
end
end
2. Migration created but forgotten
You generated a migration with rails generate but forgot to run it before starting the server.
# Terminal output:
# $ rails generate migration AddEmailToProfiles email:string
# create db/migrate/20240315090000_add_email_to_profiles.rb
# Developer starts server without running db:migrate
3. Environment mismatch
Migrations were run in development but the test database was not updated, causing errors when running tests.
# Migrations applied to development but test DB is behind
# $ RAILS_ENV=test rails runner 'puts ActiveRecord::Base.connection.migration_context.needs_migration?'
# => true
The Fix
Run bin/rails db:migrate to apply all pending migrations. Also run it for the test environment if needed. Use db:migrate:status to verify all migrations are applied.
# Pending migration exists but has not been applied
# db/migrate/20240101120000_add_status_to_orders.rb
class AddStatusToOrders < ActiveRecord::Migration[7.1]
def change
add_column :orders, :status, :string, default: 'pending'
end
end
# Server started without running migrations
# Run pending migrations:
# $ bin/rails db:migrate
# $ bin/rails db:migrate RAILS_ENV=test
# Verify migration status:
# $ bin/rails db:migrate:status
# Status Migration ID Migration Name
# up 20240101120000 Add status to orders
Testing the Fix
require 'rails_helper'
RSpec.describe Order, type: :model do
describe 'status column' do
it 'has a default status of pending' do
order = Order.create!(total: 100)
expect(order.status).to eq('pending')
end
it 'allows updating status' do
order = Order.create!(total: 100)
order.update!(status: 'shipped')
expect(order.reload.status).to eq('shipped')
end
end
end
Run your tests:
bundle exec rspec spec/models/order_spec.rb
Pushing Through CI/CD
git checkout -b fix/rails-migration-pending,bin/rails db:migrate,git add db/migrate/ db/schema.rb,git commit -m "fix: apply pending migration for orders status column",git push origin fix/rails-migration-pending
Your CI config should look something like this:
name: CI
on:
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
ports: ['5432:5432']
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.3'
bundler-cache: true
- run: bin/rails db:setup
- run: bundle exec rspec
The Full Manual Process: 18 Steps
Here's every step you just went through to fix this one bug:
- Notice the error alert or see it in your monitoring tool
- Open the error dashboard and read the stack trace
- Identify the file and line number from the stack trace
- Open your IDE and navigate to the file
- Read the surrounding code to understand context
- Reproduce the error locally
- Identify the root cause
- Write the fix
- Run the test suite locally
- Fix any failing tests
- Write new tests covering the edge case
- Run the full test suite again
- Create a new git branch
- Commit and push your changes
- Open a pull request
- Wait for code review
- Merge and deploy to production
- Monitor production to confirm the error is resolved
Total time: 30-60 minutes. For one bug.
Or Let bugstack Fix It in Under 2 minutes
Every step above? bugstack does it automatically.
Step 1: Install the SDK
gem install bugstack
Step 2: Initialize
require 'bugstack'
Bugstack.init(api_key: ENV['BUGSTACK_API_KEY'])
Step 3: There is no step 3.
bugstack handles everything from here:
- Captures the stack trace and request context
- Pulls the relevant source files from your GitHub repo
- Analyzes the error and understands the code context
- Generates a minimal, verified fix
- Runs your existing test suite
- Pushes through your CI/CD pipeline
- Deploys to production (or opens a PR for review)
Time from error to fix deployed: Under 2 minutes.
Human involvement: zero.
Try bugstack Free →No credit card. 5-minute setup. Cancel anytime.
Deploying the Fix (Manual Path)
- Run bin/rails db:migrate locally and verify the schema change.
- Run the full test suite to confirm nothing is broken.
- Open a pull request with the updated schema.rb.
- Wait for CI to pass and get a code review.
- Merge to main and run db:migrate on staging before promoting to production.
Frequently Asked Questions
BugStack runs the fix through your existing test suite, generates additional edge-case tests, and validates that no other components are affected before marking it safe to deploy.
BugStack never pushes directly to production. Every fix goes through a pull request with full CI checks, so your team can review it before merging.
Yes, run bin/rails db:rollback to undo the last migration, or db:rollback STEP=N to undo multiple. Ensure your migration defines both up and down methods for reversibility.
Rails only checks for pending migrations automatically in development mode. In production, you must run migrations explicitly during deployment, so this middleware check is skipped.