All posts

Fix Migration Error in Rails

Resolve Rails ActiveRecord migration errors including failed migrations, pending migrations in production, and schema conflicts.

Rails Migration Errors

Rails migrations fail for various reasons — database connection issues, conflicting schema changes, or migrations that aren't reversible. Here's how to handle each case.

Stuck Migration

A migration that failed partway through leaves the database in an inconsistent state:

# Check status
bin/rails db:migrate:status

# If a migration shows 'down' but its changes are partially applied:
# Fix the database manually, then mark it as run
bin/rails db:migrate:up VERSION=20240115120000

Pending Migration in Production

# Add this to config/environments/production.rb
# to prevent app boot with pending migrations
config.active_record.migration_error = :page_load

Safe Migration Patterns

For zero-downtime deploys, avoid locking operations:

class AddIndexToUsersEmail < ActiveRecord::Migration[7.0]
  disable_ddl_transaction!

  def change
    # CONCURRENTLY doesn't lock the table
    add_index :users, :email, algorithm: :concurrently
  end
end

Irreversible Migration Fix

class ChangeColumnType < ActiveRecord::Migration[7.0]
  def up
    change_column :products, :price, :decimal, precision: 10, scale: 2
  end

  def down
    change_column :products, :price, :float
  end
end

Schema Conflicts After Merge

# Regenerate schema.rb from the database
bin/rails db:schema:dump

Never manually edit schema.rb — it's auto-generated.

Bugsly captures ActiveRecord::PendingMigrationError and other migration-related exceptions, alerting your team when a deployment leaves the database in a mismatched state.

Try Bugsly Free

AI-powered error tracking that explains your bugs. Set up in 2 minutes, free forever for small projects.

Get Started Free