Home > Enterprise >  ActiveRecord migration - when reversible.up/reversible.down feature was introduced?
ActiveRecord migration - when reversible.up/reversible.down feature was introduced?

Time:05-28

I'm currently in the process of upgrading old legacy Rails app and trying to find the exact version number when this reversible.up, reversible.down block feature was introduced.

class SplitNameMigration < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string

    reversible do |dir|
      User.reset_column_information
      User.all.each do |u|
        dir.up   { u.first_name, u.last_name = u.full_name.split(' ') }
        dir.down { u.full_name = "#{u.first_name} #{u.last_name}" }
        u.save
      end
    end
  end
end

Does anyone know?

CodePudding user response:

4.0.0 it was.

New method reversible makes it possible to specify code to be run when migrating up or down. See the Guide on Migration

  • Related