Skip to content

Instantly share code, notes, and snippets.

@zinovyev
Created November 1, 2017 09:54
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 zinovyev/4203f8565994db1270c666fef36e40b6 to your computer and use it in GitHub Desktop.
Save zinovyev/4203f8565994db1270c666fef36e40b6 to your computer and use it in GitHub Desktop.
A pirate's way of removing several tables in one migration
class FixJenkinsMigration3 < ActiveRecord::Migration
self.disable_ddl_transaction!
TABLE_NAMES = %i[table_1 table_2 table_3]
def up
TABLE_NAMES.each do |table|
if ActiveRecord::Base.connection.table_exists? table
select_indexies(table).each do |index|
begin
remove_index(table, name: index)
rescue Exception => e
puts e.message
end
drop_table(table)
end
end
end
end
def down
end
def select_indexies(table)
return [] unless table.present?
query = "select indexname, tablename from pg_indexes where tablename = '#{table}'"
ActiveRecord::Base.connection.execute(query).to_a.map { |i| i['indexname'].to_sym }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment