Class: ActiveRecord::Migration::CommandRecorder
- Inherits:
-
Object
- Object
- ActiveRecord::Migration::CommandRecorder
- Includes:
- StraightReversions, JoinTable
- Defined in:
- lib/active_record/migration/command_recorder.rb
Overview
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_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
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 ]
Instance Attribute Summary collapse
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#delegate ⇒ Object
Returns the value of attribute delegate.
-
#reverting ⇒ Object
Returns the value of attribute reverting.
Instance Method Summary collapse
-
#change_table(table_name, options = {}) {|delegate.update_table_definition(table_name, self)| ... } ⇒ Object
:nodoc:.
-
#initialize(delegate = nil) ⇒ CommandRecorder
constructor
A new instance of CommandRecorder.
-
#inverse_of(command, args, &block) ⇒ Object
Returns the inverse of the given command.
-
#record(*command, &block) ⇒ Object
Record
command
. -
#respond_to_missing?(*args) ⇒ Boolean
:nodoc:.
-
#revert ⇒ Object
While executing the given block, the recorded will be in reverting mode.
Constructor Details
#initialize(delegate = nil) ⇒ CommandRecorder
Returns a new instance of CommandRecorder.
42 43 44 45 46 |
# File 'lib/active_record/migration/command_recorder.rb', line 42 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) ⇒ Object (private)
Forwards any missing method call to the target.
229 230 231 232 233 234 235 |
# File 'lib/active_record/migration/command_recorder.rb', line 229 def method_missing(method, *args, &block) if @delegate.respond_to?(method) @delegate.send(method, *args, &block) else super end end |
Instance Attribute Details
#commands ⇒ Object
Returns the value of attribute commands.
40 41 42 |
# File 'lib/active_record/migration/command_recorder.rb', line 40 def commands @commands end |
#delegate ⇒ Object
Returns the value of attribute delegate.
40 41 42 |
# File 'lib/active_record/migration/command_recorder.rb', line 40 def delegate @delegate end |
#reverting ⇒ Object
Returns the value of attribute reverting.
40 41 42 |
# File 'lib/active_record/migration/command_recorder.rb', line 40 def reverting @reverting end |
Instance Method Details
#change_table(table_name, options = {}) {|delegate.update_table_definition(table_name, self)| ... } ⇒ Object
:nodoc:
109 110 111 |
# File 'lib/active_record/migration/command_recorder.rb', line 109 def change_table(table_name, = {}) # :nodoc: yield delegate.update_table_definition(table_name, self) 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]]
This method will raise an IrreversibleMigration
exception if it cannot invert the command
.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/active_record/migration/command_recorder.rb', line 84 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 |
#record(*command, &block) ⇒ Object
Record command
. command
should be a method name and arguments. For example:
recorder.record(:method_name, [:arg1, :arg2])
69 70 71 72 73 74 75 |
# File 'lib/active_record/migration/command_recorder.rb', line 69 def record(*command, &block) if @reverting @commands << inverse_of(*command, &block) else @commands << (command << block) end end |
#respond_to_missing?(*args) ⇒ Boolean
:nodoc:
95 96 97 |
# File 'lib/active_record/migration/command_recorder.rb', line 95 def respond_to_missing?(*args) # :nodoc: super || delegate.respond_to?(*args) end |
#revert ⇒ Object
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])
55 56 57 58 59 60 61 62 63 |
# File 'lib/active_record/migration/command_recorder.rb', line 55 def revert @reverting = !@reverting previous = @commands @commands = [] yield ensure @commands = previous.concat(@commands.reverse) @reverting = !@reverting end |