Skip to content

Instantly share code, notes, and snippets.

@begin29
Last active December 23, 2016 15:29
Show Gist options
  • Save begin29/bfa1c41634b3768f9be89a01b3cfa44c to your computer and use it in GitHub Desktop.
Save begin29/bfa1c41634b3768f9be89a01b3cfa44c to your computer and use it in GitHub Desktop.
how rails migration works?
# it looks to `schema_migrations` table and check what migrations was already run
# after merge your code with migration if your migration has name e.g. 2015000000
# and it used some old column name that already renamed in new migrations, this
# migration will crash
# file: 2015000000_old_migration
class OldMigration < ActiveRecord::Migration
def up
# trow Error, that column car_id doesn't exist
User.where(car_id: Car.where(color: 'red').pluck(:id))
end
end
# 2016000000_new_migration
# already run in your env and wount run again
class NewMigration < ActiveRecord::Migration
def up
rename_column :users, :car_id, :super_car_id
end
def down
# ... rename back
end
end
# show which migration is up and down
rake db:migrate:status
# call up migrate method from other migration
OldMigration.new.up
# call separate migration call
rake db:migrate:up VERSION=20080906120000
#update schema migrations table
ActiveRecord::Base.connection.execute("INSERT INTO schema_migrations values ('some number of migration')")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment