Created
November 1, 2017 09:54
-
-
Save zinovyev/4203f8565994db1270c666fef36e40b6 to your computer and use it in GitHub Desktop.
A pirate's way of removing several tables in one migration
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
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