Class: ActiveRecord::Migration::CommandRecorder
Relationships & Source Files | |
Inherits: | Object |
Defined in: | activerecord/lib/active_record/migration/command_recorder.rb |
Overview
Migration Command Recorder
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_table
-
disable_extension
-
drop_enum (must supply a list of values)
-
drop_join_table
-
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 (must supply a
:to
option) -
rename_enum_value (must supply a
:from
and:to
option) -
rename_index
-
rename_table
Constant Summary
-
ReversibleAndIrreversibleMethods =
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 46[ :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, :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, ]
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_check_constraint(args)
- #invert_add_foreign_key(args)
- #invert_add_unique_constraint(args)
- #invert_change_column_comment(args)
- #invert_change_column_default(args)
- #invert_change_column_null(args)
- #invert_change_table_comment(args)
- #invert_create_table(args, &block)
- #invert_drop_enum(args)
- #invert_drop_table(args, &block)
- #invert_remove_belongs_to
- #invert_remove_check_constraint(args)
- #invert_remove_column(args)
- #invert_remove_columns(args)
- #invert_remove_exclusion_constraint(args)
- #invert_remove_foreign_key(args)
- #invert_remove_index(args)
- #invert_remove_unique_constraint(args)
- #invert_rename_column(args)
- #invert_rename_enum(args)
- #invert_rename_enum_value(args)
- #invert_rename_index(args)
- #invert_rename_table(args)
- #invert_transaction(args, &block)
-
#method_missing(method, *args, &block)
Forwards any missing method call to the target.
-
#record(*command, &block)
Record
command
. - #remove_belongs_to
- #replay(migration)
- #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 129
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, :, 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
.
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 110
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 256
alias :invert_add_belongs_to :invert_add_reference
#invert_add_check_constraint(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 313
def invert_add_check_constraint(args) if ( = args.last).is_a?(Hash) .delete(:validate) [:if_exists] = .delete(:if_not_exists) if .key?(:if_not_exists) end super end
#invert_add_foreign_key(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 274
def invert_add_foreign_key(args) args.last.delete(:validate) if args.last.is_a?(Hash) super end
#invert_add_unique_constraint(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 335
def invert_add_unique_constraint(args) = args.dup. raise ActiveRecord::IrreversibleMigration, "add_unique_constraint is not reversible if given an using_index." if [:using_index] super end
#invert_change_column_comment(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 293
def invert_change_column_comment(args) table, column, = args unless .is_a?(Hash) && .has_key?(:from) && .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: [:to], to: [:from]]] end
#invert_change_column_default(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 259
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 269
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 303
def invert_change_table_comment(args) table, = args unless .is_a?(Hash) && .has_key?(:from) && .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: [:to], to: [:from]]] end
#invert_create_table(args, &block)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 191
def invert_create_table(args, &block) if args.last.is_a?(Hash) args.last.delete(:if_not_exists) end super end
#invert_drop_enum(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 349
def invert_drop_enum(args) _enum, values = args.dup.tap(&: ) raise ActiveRecord::IrreversibleMigration, "drop_enum is only reversible if given a list of enum values." unless values super end
#invert_drop_table(args, &block)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 198
def invert_drop_table(args, &block) if args.last.is_a?(Hash) args.last.delete(:if_exists) end 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 257
alias :invert_remove_belongs_to :invert_remove_reference
#invert_remove_check_constraint(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 321
def invert_remove_check_constraint(args) raise ActiveRecord::IrreversibleMigration, "remove_check_constraint is only reversible if given an expression." if args.size < 2 if ( = args.last).is_a?(Hash) [:if_not_exists] = .delete(:if_exists) if .key?(:if_exists) end super end
#invert_remove_column(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 215
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 220
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_exclusion_constraint(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 330
def invert_remove_exclusion_constraint(args) raise ActiveRecord::IrreversibleMigration, "remove_exclusion_constraint is only reversible if given an expression." if args.size < 2 super end
#invert_remove_foreign_key(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 279
def invert_remove_foreign_key(args) = args. from_table, to_table = args to_table ||= .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 << unless .empty? [:add_foreign_key, reversed_args] end
#invert_remove_index(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 238
def invert_remove_index(args) = args. table, columns = args columns ||= .delete(:column) unless columns raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option." end .delete(:if_exists) args = [table, columns] args << unless .empty? [:add_index, args] end
#invert_remove_unique_constraint(args)
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 342
def invert_remove_unique_constraint(args) _table, columns = args.dup.tap(&: ) raise ActiveRecord::IrreversibleMigration, "remove_unique_constraint is only reversible if given an column_name." if columns.blank? super end
#invert_rename_column(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 233
def invert_rename_column(args) table_name, old_name, new_name = args [:rename_column, [table_name, new_name, old_name]] end
#invert_rename_enum(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 355
def invert_rename_enum(args) name, = args unless .is_a?(Hash) && .has_key?(:to) raise ActiveRecord::IrreversibleMigration, "rename_enum is only reversible if given a :to option." end [:rename_enum, [ [:to], to: name]] end
#invert_rename_enum_value(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 365
def invert_rename_enum_value(args) type_name, = args unless .is_a?(Hash) && .has_key?(:from) && .has_key?(:to) raise ActiveRecord::IrreversibleMigration, "rename_enum_value is only reversible if given a :from and :to option." end [:rename_enum_value, [type_name, from: [:to], to: [:from]]] end
#invert_rename_index(args)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 228
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 208
def invert_rename_table(args) old_name, new_name, = args args = [new_name, old_name] args << if [:rename_table, args] end
#invert_transaction(args, &block)
[ GitHub ]#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 90
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 130
alias :remove_belongs_to :remove_reference
#replay(migration)
[ GitHub ]# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 144
def replay(migration) commands.each do |cmd, args, block| migration.send(cmd, *args, &block) end end
#respond_to_missing?(method, _) ⇒ Boolean
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 375
def respond_to_missing?(method, _) super || delegate.respond_to?(method) end
#revert
# File 'activerecord/lib/active_record/migration/command_recorder.rb', line 76
def revert @reverting = !@reverting previous = @commands @commands = [] yield ensure @commands = previous.concat(@commands.reverse) @reverting = !@reverting end