All posts

Fix Migration Error in Ruby

Resolve database migration errors in Ruby projects using Sequel and ROM, covering version tracking and schema synchronization.

Ruby Database Migration Errors

Beyond Rails, Ruby projects use Sequel or ROM (Ruby Object Mapper) for database access. Their migration systems have their own quirks.

Sequel Migrations

Sequel uses timestamp or integer-based migrations:

# db/migrations/001_create_users.rb
Sequel.migration do
  up do
    create_table(:users) do
      primary_key :id
      String :name, null: false
      String :email, null: false, unique: true
      DateTime :created_at
    end
  end

  down do
    drop_table(:users)
  end
end

Run migrations:

sequel -m db/migrations postgres://localhost/mydb

Common Errors

"Table already exists":

# Use if_not_exists
Sequel.migration do
  up do
    create_table?(:users) do  # ? means IF NOT EXISTS
      primary_key :id
      String :name
    end
  end
end

Column already exists:

up do
  alter_table(:users) do
    add_column :email, String unless @db.schema(:users).map(&:first).include?(:email)
  end
end

Schema Version Tracking

Sequel tracks versions in schema_migrations table. If it's out of sync:

DB = Sequel.connect(ENV['DATABASE_URL'])

# Check current version
puts DB[:schema_migrations].all

# Force a version
DB[:schema_migrations].insert(filename: '001_create_users.rb')

Rollback

# Roll back one migration
sequel -m db/migrations -M 2 postgres://localhost/mydb

Bugsly captures migration exceptions with the full error message and migration filename, making it clear which migration failed and why.

Try Bugsly Free

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

Get Started Free