class ActiveRecord::Migration::CommandRecorder

Parent:
Object
Included modules:
ActiveRecord::Migration::CommandRecorder::StraightReversions

ActiveRecord::Migration::CommandRecorder records commands done during a migration and knows how to reverse those commands. The CommandRecorder knows how to invert the following commands:

  • add_column

  • add_foreign_key

  • add_index

  • add_reference

  • add_timestamps

  • change_column

  • change_column_default (must supply a :from and :to option)

  • change_column_null

  • create_join_table

  • create_table

  • disable_extension

  • drop_join_table

  • drop_table (must supply a block)

  • enable_extension

  • remove_column (must supply a type)

  • remove_columns (must specify at least one column name or more)

  • remove_foreign_key (must supply a second table)

  • remove_index

  • remove_reference

  • remove_timestamps

  • rename_column

  • rename_index

  • rename_table

Constants

ReversibleAndIrreversibleMethods

Attributes

commands[RW]
delegate[RW]
reverting[RW]

Public Class Methods

new(delegate = nil) Show source
# File activerecord/lib/active_record/migration/command_recorder.rb, line 42
def initialize(delegate = nil)
  @commands = []
  @delegate = delegate
  @reverting = false
end

Public Instance Methods

inverse_of(command, args, &block) Show source
# File activerecord/lib/active_record/migration/command_recorder.rb, line 84
      def inverse_of(command, args, &block)
        method = :"invert_#{command}"
        raise IrreversibleMigration, "          This migration uses #{command}, which is not automatically reversible.
          To make the migration reversible you can either:
          1. Define #up and #down methods in place of the #change method.
          2. Use the #reversible method to define reversible behavior.
".strip_heredoc unless respond_to?(method, true)
        send(method, args, &block)
      end

Returns the inverse of the given command. For example:

recorder.inverse_of(:rename_table, [:old, :new])
# => [:rename_table, [:new, :old]]

This method will raise an IrreversibleMigration exception if it cannot invert the command.

record(*command, &block) Show source
# File activerecord/lib/active_record/migration/command_recorder.rb, line 69
def record(*command, &block)
  if @reverting
    @commands << inverse_of(*command, &block)
  else
    @commands << (command << block)
  end
end

Record command. command should be a method name and arguments. For example:

recorder.record(:method_name, [:arg1, :arg2])
revert() { || ... } Show source
# File activerecord/lib/active_record/migration/command_recorder.rb, line 55
def revert
  @reverting = !@reverting
  previous = @commands
  @commands = []
  yield
ensure
  @commands = previous.concat(@commands.reverse)
  @reverting = !@reverting
end

While executing the given block, the recorded will be in reverting mode. All commands recorded will end up being recorded reverted and in reverse order. For example:

recorder.revert{ recorder.record(:rename_table, [:old, :new]) }
# same effect as recorder.record(:rename_table, [:new, :old])

© 2004–2018 David Heinemeier Hansson
Licensed under the MIT License.