Rails migration, mysql, composite index and remove_column

# DB table
users:
name: string
domain: string
username: string

# Composite index on 2 fields
add_index :users, [:username, :domain]
<User1 name: 'John', domain: 'example.com', username: 'john'>  
<User1 name: 'Mike', domain: 'example.com', username: 'mike'>
def up  
remove_column :users, :username
remove_column :users, :domain
end
Mysql2::Error: Duplicate entry 'example.com' for key 'index_users_on_username_and_domain': ALTER TABLE `users` DROP `username`
def up  
if index_exists?(:users, [:username, :domain])
remove_index :users, [:username, :domain]
end

remove_column :users, :username
remove_column :users, :domain
end

Dmitriy Zaporozhets

Written by

Ruby developer, GitLab cofounder

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade