迁移命令记录器¶ ↑
ActiveRecord::Migration::CommandRecorder 记录迁移过程中执行的命令,并知道如何撤销这些命令。 CommandRecorder 知道如何反转以下命令:
-
add_column
-
add_foreign_key
-
add_check_constraint
-
add_exclusion_constraint
-
add_unique_constraint
-
add_index
-
add_reference
-
add_timestamps
-
change_column_default(必须提供
:from和:to选项) -
change_column_null
-
change_column_comment(必须提供
:from和:to选项) -
change_table_comment(必须提供
:from和:to选项) -
create_enum
-
create_join_table
-
create_virtual_table
-
create_table
-
disable_extension
-
drop_enum(必须提供值列表)
-
drop_join_table
-
drop_virtual_table(必须提供选项)
-
drop_table(必须提供一个块)
-
enable_extension
-
remove_column(必须提供类型)
-
remove_columns(必须提供
:type选项) -
remove_foreign_key(必须提供第二个表)
-
remove_check_constraint
-
remove_exclusion_constraint
-
remove_unique_constraint
-
remove_index
-
remove_reference
-
remove_timestamps
-
rename_column
-
rename_enum
-
rename_enum_value(必须提供
:from和:to选项) -
rename_index
-
rename_table
-
enable_index
-
disable_index
- I
- N
- R
常量
| 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, :enable_index, :disable_index ] |
Attributes
| [RW] | commands | |
| [RW] | 委托 | |
| [RW] | reverting |
类公共方法
new(delegate = nil) 链接
Source: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 70 def initialize(delegate = nil) @commands = [] @delegate = delegate @reverting = false end
实例公共方法
inverse_of(command, args, &block) 链接
返回给定命令的反向操作。例如:
recorder.inverse_of(:rename_table, [:old, :new]) # => [:rename_table, [:new, :old]]
如果一个命令的反向操作需要多个命令,则返回命令数组。
recorder.inverse_of(:remove_columns, [:some_table, :foo, :bar, type: :string]) # => [[:add_column, :some_table, :foo, :string], [:add_column, :some_table, :bar, :string]]
如果无法反转 command,此方法将引发 IrreversibleMigration 异常。
Source: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 117 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) 链接
记录 command。command 应为方法名称和参数。例如:
recorder.record(:method_name, [:arg1, :arg2])
Source: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 97 def record(*command, &block) if @reverting @commands << inverse_of(*command, &block) else @commands << (command << block) end end
replay(migration) 链接
Source: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 151 def replay(migration) commands.each do |cmd, args, block| migration.send(cmd, *args, &block) end end
revert() 链接
在执行给定块时,记录器将处于回滚模式。所有记录的命令最终都将被记录为反转且顺序相反。例如:
recorder.revert{ recorder.record(:rename_table, [:old, :new]) } # same effect as recorder.record(:rename_table, [:new, :old])
Source: 显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 83 def revert @reverting = !@reverting previous = @commands @commands = [] yield ensure @commands = previous.concat(@commands.reverse) @reverting = !@reverting end