Skip to content

Instantly share code, notes, and snippets.

@yoshuki
Last active October 17, 2017 08:13
Show Gist options
  • Save yoshuki/9d2ae85928240c4158c79834804b3f4d to your computer and use it in GitHub Desktop.
Save yoshuki/9d2ae85928240c4158c79834804b3f4d to your computer and use it in GitHub Desktop.
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