Created
May 26, 2019 15:37
-
-
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
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 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