Skip to content

Instantly share code, notes, and snippets.

@alebian
Created May 26, 2019 15:37
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 alebian/c7bada4085d6b52ea1859f008aedd2f7 to your computer and use it in GitHub Desktop.
Save alebian/c7bada4085d6b52ea1859f008aedd2f7 to your computer and use it in GitHub Desktop.
Change schema version type of a Rails app to use the DB by a Phoenix app
class ChangeVersionType < ActiveRecord::Migration[5.2]
def up
add_column :schema_migrations, :version2, :string
versions = ActiveRecord::Base.connection.execute('SELECT * FROM schema_migrations').to_a
versions.each do |version|
ActiveRecord::Base.connection.execute("UPDATE schema_migrations SET version2 = '#{version['version']}' WHERE version = '#{version['version']}'")
end
remove_column :schema_migrations, :version, :string
add_column :schema_migrations, :version, :bigint
versions = ActiveRecord::Base.connection.execute('SELECT * FROM schema_migrations').to_a
versions.each do |version|
ActiveRecord::Base.connection.execute("UPDATE schema_migrations SET version = '#{version['version2']}' WHERE version2 = '#{version['version2']}'")
end
remove_column :schema_migrations, :version2
end
def down
add_column :schema_migrations, :version2, :string
versions = ActiveRecord::Base.connection.execute('SELECT * FROM schema_migrations').to_a
versions.each do |version|
ActiveRecord::Base.connection.execute("UPDATE schema_migrations SET version2 = '#{version['version']}' WHERE version = '#{version['version']}'")
end
remove_column :schema_migrations, :version, :bigint
add_column :schema_migrations, :version, :string
versions = ActiveRecord::Base.connection.execute('SELECT * FROM schema_migrations').to_a
versions.each do |version|
ActiveRecord::Base.connection.execute("UPDATE schema_migrations SET version = '#{version['version2']}' WHERE version2 = '#{version['version2']}'")
end
remove_column :schema_migrations, :version2, :string
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment