class ActiveRecord::Migration::CommandRecorder
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_index
-
add_timestamps
-
create_table
-
remove_timestamps
-
rename_column
-
rename_index
-
rename_table
Attributes
commands[RW]
delegate[RW]
Public Class Methods
new(delegate = nil)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 18 def initialize(delegate = nil) @commands = [] @delegate = delegate end
Public Instance Methods
inverse()
click to toggle source
Returns a list that represents commands that are the inverse of the
commands stored in commands
. For example:
recorder.record(:rename_table, [:old, :new]) recorder.inverse # => [:rename_table, [:new, :old]]
This method will raise an IrreversibleMigration
exception if
it cannot invert the commands
.
# File lib/active_record/migration/command_recorder.rb, line 39 def inverse @commands.reverse.map { |name, args| method = :"invert_#{name}" raise IrreversibleMigration unless respond_to?(method, true) send(method, args) } end
record(*command)
click to toggle source
record command
. command
should be a method name
and arguments. For example:
recorder.record(:method_name, [:arg1, :arg2])
# File lib/active_record/migration/command_recorder.rb, line 27 def record(*command) @commands << command end
Private Instance Methods
invert_add_column(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 69 def invert_add_column(args) [:remove_column, args.first(2)] end
invert_add_index(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 81 def invert_add_index(args) table, columns, options = *args index_name = options.try(:[], :name) options_hash = index_name ? {:name => index_name} : {:column => columns} [:remove_index, [table, options_hash]] end
invert_add_timestamps(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 92 def invert_add_timestamps(args) [:remove_timestamps, args] end
invert_create_table(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 61 def invert_create_table(args) [:drop_table, [args.first]] end
invert_remove_timestamps(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 88 def invert_remove_timestamps(args) [:add_timestamps, args] end
invert_rename_column(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 77 def invert_rename_column(args) [:rename_column, [args.first] + args.last(2).reverse] end
invert_rename_index(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 73 def invert_rename_index(args) [:rename_index, [args.first] + args.last(2).reverse] end
invert_rename_table(args)
click to toggle source
# File lib/active_record/migration/command_recorder.rb, line 65 def invert_rename_table(args) [:rename_table, args.reverse] end
method_missing(method, *args, &block)
click to toggle source
Forwards any missing method call to the target.
# File lib/active_record/migration/command_recorder.rb, line 97 def method_missing(method, *args, &block) @delegate.send(method, *args, &block) rescue NoMethodError => e raise e, e.message.sub(/ for #<.*$/, " via proxy for #{@delegate}") end