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_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
Constant Summary
-
ReversibleAndIrreversibleMethods =
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 33[:create_table, :create_join_table, :rename_table, :add_column, :remove_column, :rename_index, :rename_column, :add_index, :remove_index, :, :, :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 ]
Class Method Summary
- .new(delegate = nil) ⇒ CommandRecorder constructor
Instance Attribute Summary
- #commands rw
- #delegate rw
- #reverting rw
Instance Method Summary
- #add_belongs_to
-
#inverse_of(command, args, &block)
Returns the inverse of the given command.
- #invert_add_belongs_to
- #invert_add_foreign_key(args)
- #invert_add_index(args)
- #invert_change_column_default(args)
- #invert_change_column_null(args)
- #invert_drop_table(args, &block)
- #invert_remove_belongs_to
- #invert_remove_column(args)
- #invert_remove_foreign_key(args)
- #invert_remove_index(args)
- #invert_rename_column(args)
- #invert_rename_index(args)
- #invert_rename_table(args)
-
#method_missing(method, *args, &block)
Forwards any missing method call to the target.
-
#record(*command, &block)
Record
command
. - #remove_belongs_to
- #respond_to_missing?(method, _) ⇒ Boolean
-
#revert
While executing the given block, the recorded will be in reverting mode.
Constructor Details
.new(delegate = nil) ⇒ CommandRecorder
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.
Instance Attribute Details
#commands (rw)
[ GitHub ]#delegate (rw)
[ GitHub ]#reverting (rw)
[ GitHub ]Instance Method Details
#add_belongs_to
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 104
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]]
This method will raise an ::ActiveRecord::IrreversibleMigration
exception if it cannot invert the command
.
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 86
def inverse_of(command, args, &block) method = :"invert_#{command}" raise IrreversibleMigration, <<-MSG.strip_heredoc 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 183
alias :invert_add_belongs_to :invert_add_reference
#invert_add_foreign_key(args)
[ GitHub ]#invert_add_index(args)
[ GitHub ]#invert_change_column_default(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 186
def invert_change_column_default(args) table, column, = *args unless && .is_a?(Hash) && .has_key?(:from) && .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: [:to], to: [:from]]] end
#invert_change_column_null(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 196
def invert_change_column_null(args) args[2] = !args[2] [:change_column_null, args] end
#invert_drop_table(args, &block)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 136
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 184
alias :invert_remove_belongs_to :invert_remove_reference
#invert_remove_column(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 147
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_foreign_key(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 216
def invert_remove_foreign_key(args) from_table, to_table, = args raise ActiveRecord::IrreversibleMigration, "remove_foreign_key is only reversible if given a second table" if to_table.nil? || to_table.is_a?(Hash) reversed_args = [from_table, to_table] reversed_args << if [:add_foreign_key, reversed_args] end
#invert_remove_index(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 170
def invert_remove_index(args) table, = *args if ( = ).is_a?(Hash) unless [:column] raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." end = .dup [:add_index, [table, .delete(:column), ]] elsif (column = ).present? [:add_index, [table, column]] end end
#invert_rename_column(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 156
def invert_rename_column(args) [:rename_column, [args.first] + args.last(2).reverse] end
#invert_rename_index(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 152
def invert_rename_index(args) [:rename_index, [args.first] + args.last(2).reverse] end
#invert_rename_table(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 143
def invert_rename_table(args) [:rename_table, args.reverse] end
#record(*command, &block)
Record command
. command
should be a method name and arguments. For example:
recorder.record(:method_name, [:arg1, :arg2])
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 71
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 105
alias :remove_belongs_to :remove_reference
#respond_to_missing?(method, _) ⇒ Boolean
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 226
def respond_to_missing?(method, _) super || delegate.respond_to?(method) end
#revert
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 57
def revert @reverting = !@reverting previous = @commands @commands = [] yield ensure @commands = previous.concat(@commands.reverse) @reverting = !@reverting end