Right now, the current migration might fail, if the books
table doesn\'t have created_at
or updated_at
fields:
class AddTi
Rails 6.1 added if_exists
option to remove_index
in order to not raise an error when the index is already removed.
Rails 6.1 added if_not_exists
option to add_index
in order to not raise an error when the index is already added.
As a result, your migration can be rewritten in the following way:
class AddTimestampIndexes < ActiveRecord::Migration
def up
remove_index :books, :created_at, if_exists: true
remove_index :books, :updated_at, if_exists: true
add_index :books, :created_at
add_index :books, :updated_at
end
def down
remove_index :books, :created_at, if_exists: true
remove_index :books, :updated_at, if_exists: true
end
end
Here is a list of the links to the corresponding PRs: