Class: ActiveRecord::Migration::CommandRecorder

Inherits:
Object
  • Object
show all
Includes:
StraightReversions, JoinTable
Defined in:
lib/active_record/migration/command_recorder.rb

Overview

Migration Command Recorder

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_check_constraint

  • add_exclusion_constraint

  • add_unique_constraint

  • add_index

  • add_reference

  • add_timestamps

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

  • create_join_table

  • create_virtual_table

  • create_table

  • disable_extension

  • drop_enum (must supply a list of values)

  • drop_join_table

  • drop_virtual_table (must supply options)

  • drop_table (must supply a block)

  • enable_extension

  • remove_column (must supply a type)

  • remove_columns (must supply a :type option)

  • remove_foreign_key (must supply a second table)

  • remove_check_constraint

  • remove_exclusion_constraint

  • remove_unique_constraint

  • remove_index

  • remove_reference

  • remove_timestamps

  • rename_column

  • rename_enum

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

  • rename_index

  • rename_table

Defined Under Namespace

Modules: StraightReversions

Constant Summary collapse

ReversibleAndIrreversibleMethods =
[
  :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,
  :add_exclusion_constraint, :remove_exclusion_constraint,
  :add_unique_constraint, :remove_unique_constraint,
  :create_enum, :drop_enum, :rename_enum, :add_enum_value, :rename_enum_value,
  :create_schema, :drop_schema,
  :create_virtual_table, :drop_virtual_table
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate = nil) ⇒ CommandRecorder

Returns a new instance of CommandRecorder.



67
68
69
70
71
# File 'lib/active_record/migration/command_recorder.rb', line 67

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) ⇒ Object (private)

Forwards any missing method call to the target.



397
398
399
400
401
402
403
# File 'lib/active_record/migration/command_recorder.rb', line 397

def method_missing(method, ...)
  if delegate.respond_to?(method)
    delegate.public_send(method, ...)
  else
    super
  end
end

Instance Attribute Details

#commandsObject

Returns the value of attribute commands.



65
66
67
# File 'lib/active_record/migration/command_recorder.rb', line 65

def commands
  @commands
end

#delegateObject

Returns the value of attribute delegate.



65
66
67
# File 'lib/active_record/migration/command_recorder.rb', line 65

def delegate
  @delegate
end

#revertingObject

Returns the value of attribute reverting.



65
66
67
# File 'lib/active_record/migration/command_recorder.rb', line 65

def reverting
  @reverting
end

Instance Method Details

#change_table(table_name, **options) ⇒ Object

:nodoc:



136
137
138
139
140
141
142
143
144
145
146
# File 'lib/active_record/migration/command_recorder.rb', line 136

def change_table(table_name, **options) # :nodoc:
  if delegate.supports_bulk_alter? && options[:bulk]
    recorder = self.class.new(self.delegate)
    recorder.reverting = @reverting
    yield recorder.delegate.update_table_definition(table_name, recorder)
    commands = recorder.commands
    @commands << [:change_table, [table_name], -> t { bulk_change_table(table_name, commands) }]
  else
    yield delegate.update_table_definition(table_name, self)
  end
end

#inverse_of(command, args, &block) ⇒ Object

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 IrreversibleMigration exception if it cannot invert the command.



114
115
116
117
118
119
120
121
122
123
# File 'lib/active_record/migration/command_recorder.rb', line 114

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

#record(*command, &block) ⇒ Object

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

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


94
95
96
97
98
99
100
# File 'lib/active_record/migration/command_recorder.rb', line 94

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

#replay(migration) ⇒ Object



148
149
150
151
152
# File 'lib/active_record/migration/command_recorder.rb', line 148

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

#revertObject

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


80
81
82
83
84
85
86
87
88
# File 'lib/active_record/migration/command_recorder.rb', line 80

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