begin29
12/1/2016 - 10:03 AM

how rails migration works?

how rails migration works?

# 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')")
# 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