123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Migration::CommandRecorder

Relationships & Source Files
Inherits: Object
Defined in: activerecord/lib/active_record/migration/command_recorder.rb

Overview

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_check_constraint

  • add_index

  • add_reference

  • add_timestamps

  • change_column

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

  • change_column_null

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

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

  • 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_check_constraint

  • remove_index

  • remove_reference

  • remove_timestamps

  • rename_column

  • rename_index

  • rename_table

Constant Summary

  • ReversibleAndIrreversibleMethods =
    # File 'activerecord/lib/active_record/migration/command_recorder.rb', line 37
    [
      :create_table, :create_join_table, :rename_table, :add_column, :remove_column,
      :rename_index, :rename_column, :add_index, :remove_index, :add_timestamps, :remove_timestamps,
      :change_column_default, :add_reference, :remove_reference, :transaction,
      :drop_join_table, :drop_table, :execute_block, :enable_extension, :disable_extension,
      :change_column, :execute, :remove_columns, :change_column_null,
      :add_foreign_key, :remove_foreign_key,
      :change_column_comment, :change_table_comment,
      :add_check_constraint, :remove_check_constraint
    ]

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(delegate = nil) ⇒ CommandRecorder

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 51

def initialize(delegate = nil)
  @commands = []
  @delegate = delegate
  @reverting = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block)

Forwards any missing method call to the target.

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 282

def method_missing(method, *args, &block)
  if delegate.respond_to?(method)
    delegate.public_send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#commands (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 49

attr_accessor :commands, :delegate, :reverting

#delegate (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 49

attr_accessor :commands, :delegate, :reverting

#reverting (rw)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 49

attr_accessor :commands, :delegate, :reverting

Instance Method Details

#add_belongs_to

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 117

alias :add_belongs_to :add_reference

#inverse_of(command, args, &block)

Returns the inverse of the given command. For example:

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

If the inverse of a command requires several commands, returns array of commands.

recorder.inverse_of(:remove_columns, [:some_table, :foo, :bar, type: :string])
# => [[:add_column, :some_table, :foo, :string], [:add_column, :some_table, :bar, :string]]

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

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 98

def inverse_of(command, args, &block)
  method = :"invert_#{command}"
  raise IrreversibleMigration, <<~MSG unless respond_to?(method, true)
    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.
  MSG
  send(method, args, &block)
end

#invert_add_belongs_to

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 220

alias :invert_add_belongs_to :invert_add_reference

#invert_change_column_comment(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 252

def invert_change_column_comment(args)
  table, column, options = args

  unless options.is_a?(Hash) && options.has_key?(:from) && options.has_key?(:to)
    raise ActiveRecord::IrreversibleMigration, "change_column_comment is only reversible if given a :from and :to option."
  end

  [:change_column_comment, [table, column, from: options[:to], to: options[:from]]]
end

#invert_change_column_default(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 223

def invert_change_column_default(args)
  table, column, options = args

  unless options.is_a?(Hash) && options.has_key?(:from) && options.has_key?(:to)
    raise ActiveRecord::IrreversibleMigration, "change_column_default is only reversible if given a :from and :to option."
  end

  [:change_column_default, [table, column, from: options[:to], to: options[:from]]]
end

#invert_change_column_null(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 233

def invert_change_column_null(args)
  args[2] = !args[2]
  [:change_column_null, args]
end

#invert_change_table_comment(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 262

def invert_change_table_comment(args)
  table, options = args

  unless options.is_a?(Hash) && options.has_key?(:from) && options.has_key?(:to)
    raise ActiveRecord::IrreversibleMigration, "change_table_comment is only reversible if given a :from and :to option."
  end

  [:change_table_comment, [table, from: options[:to], to: options[:from]]]
end

#invert_drop_table(args, &block)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 168

def invert_drop_table(args, &block)
  if args.size == 1 && block == nil
    raise ActiveRecord::IrreversibleMigration, "To avoid mistakes, drop_table is only reversible if given options or a block (can be empty)."
  end
  super
end

#invert_remove_belongs_to

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 221

alias :invert_remove_belongs_to :invert_remove_reference

#invert_remove_check_constraint(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 272

def invert_remove_check_constraint(args)
  raise ActiveRecord::IrreversibleMigration, "remove_check_constraint is only reversible if given an expression." if args.size < 2
  super
end

#invert_remove_column(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 179

def invert_remove_column(args)
  raise ActiveRecord::IrreversibleMigration, "remove_column is only reversible if given a type." if args.size <= 2
  super
end

#invert_remove_columns(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 184

def invert_remove_columns(args)
  unless args[-1].is_a?(Hash) && args[-1].has_key?(:type)
    raise ActiveRecord::IrreversibleMigration, "remove_columns is only reversible if given a type."
  end

  [:add_columns, args]
end

#invert_remove_foreign_key(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 238

def invert_remove_foreign_key(args)
  options = args.extract_options!
  from_table, to_table = args

  to_table ||= options.delete(:to_table)

  raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a second table" if to_table.nil?

  reversed_args = [from_table, to_table]
  reversed_args << options unless options.empty?

  [:add_foreign_key, reversed_args]
end

#invert_remove_index(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 202

def invert_remove_index(args)
  options = args.extract_options!
  table, columns = args

  columns ||= options.delete(:column)

  unless columns
    raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option."
  end

  options.delete(:if_exists)

  args = [table, columns]
  args << options unless options.empty?

  [:add_index, args]
end

#invert_rename_column(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 197

def invert_rename_column(args)
  table_name, old_name, new_name = args
  [:rename_column, [table_name, new_name, old_name]]
end

#invert_rename_index(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 192

def invert_rename_index(args)
  table_name, old_name, new_name = args
  [:rename_index, [table_name, new_name, old_name]]
end

#invert_rename_table(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 175

def invert_rename_table(args)
  [:rename_table, args.reverse]
end

#invert_transaction(args)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 157

def invert_transaction(args)
  sub_recorder = CommandRecorder.new(delegate)
  sub_recorder.revert { yield }

  invertions_proc = proc {
    sub_recorder.replay(self)
  }

  [:transaction, args, invertions_proc]
end

#record(*command, &block)

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

recorder.record(:method_name, [:arg1, :arg2])
[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 78

def record(*command, &block)
  if @reverting
    @commands << inverse_of(*command, &block)
  else
    @commands << (command << block)
  end
end

#remove_belongs_to

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 118

alias :remove_belongs_to :remove_reference

#replay(migration)

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 124

def replay(migration)
  commands.each do |cmd, args, block|
    migration.send(cmd, *args, &block)
  end
end

#respond_to_missing?(method, _) ⇒ Boolean

[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 277

def respond_to_missing?(method, _)
  super || delegate.respond_to?(method)
end

#revert

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])
[ GitHub ]

  
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 64

def revert
  @reverting = !@reverting
  previous = @commands
  @commands = []
  yield
ensure
  @commands = previous.concat(@commands.reverse)
  @reverting = !@reverting
end