I am a seasoned developer with over 9 years of experience in Ruby on Rails and Golang. My passion lies in tackling complex data structures and algorithm problems, always pushing myself to learn and grow. In my downtime, I find solace in the pages of books and the soothing sounds of a guitar. But I'm not just a one-trick pony! I love to stay active and challenge myself in other ways too. Whether it's on the football pitch, cricket field, table tennis table, or badminton court, I'm always up for some fun activities.
1 minute read
In few applications, we have migrations to remove index where we don’t want to raise an error
if an index was already removed.
Rails 6.1
adds
a feature, where we can now pass if_exists option to remove_index operation.
This is similar to
if_not_exists
option when we are adding an index on table.
Before
Let’s say we have a comments table with description, post_id and user_id as columns.
We add a data migration to remove the index on the description column.
If this index was already removed the data migration will throw an error as below.
After
After the changes in Rails 6.1, the above migration will not raise any error
if we pass if_exists as true.
Rails does not execute any SQL query for DROP INDEX in the above case.