Created
February 27, 2020 02:02
-
-
Save RichOrElse/737ffa089129d7c962f4b60812d09d5a to your computer and use it in GitHub Desktop.
DB migration helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MigrationReversible | |
def remove_existing_index(table_name, column_names, **options) | |
index_name = options[:name] | |
exists = suppress_messages { index_exists?(table_name, column_names, name: index_name) } | |
reversible do |dir| | |
dir.up { remove_index(table_name, name: index_name) if exists } | |
dir.down do | |
if exists | |
say "existing index #{index_name.inspect} on table #{table_name.inspect}" | |
else | |
add_index(table_name, column_names, **options) | |
end | |
end | |
end | |
end | |
def execute_when(up: nil, down: nil) | |
reversible do |dir| | |
dir.up { execute(up) } if up | |
dir.down { execute(down) } if down | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment