Skip to content

Instantly share code, notes, and snippets.

@RichOrElse
Created February 27, 2020 02:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichOrElse/737ffa089129d7c962f4b60812d09d5a to your computer and use it in GitHub Desktop.
Save RichOrElse/737ffa089129d7c962f4b60812d09d5a to your computer and use it in GitHub Desktop.
DB migration helper
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