Last active
October 17, 2017 08:13
-
-
Save yoshuki/9d2ae85928240c4158c79834804b3f4d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 ChangeIdsFromIntegerToBigint < ActiveRecord::Migration[5.1] | |
def up | |
do_migrate :integer, :bigint | |
end | |
def down | |
do_migrate :bigint, :integer | |
end | |
private | |
def do_migrate(type_from, type_to) | |
ApplicationRecord.connection.tables.each do |table| | |
begin | |
klass = table.classify.constantize | |
klass.columns.each do |column| | |
next unless column.name =~ /\Aid\z|_id\z/ && column.type == type_from | |
puts "#{table}, #{column.name}, #{column.type}, #{column.default.inspect}, #{column.null}" | |
change_column table, column.name, type_to, default: column.default, null: column.null | |
end | |
rescue NameError => e | |
# ignore | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment