迁移命令记录器
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
- 我
- 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 ] |
属性
[RW] | commands | |
[RW] | delegate | |
[RW] | reverting |
类公共方法
new(delegate = nil) 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 67 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
异常。
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 114 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])
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 94 def record(*command, &block) if @reverting @commands << inverse_of(*command, &block) else @commands << (command << block) end end
replay(migration) 链接
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 148 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])
来源:显示 | 在 GitHub 上
# File activerecord/lib/active_record/migration/command_recorder.rb, line 80 def revert @reverting = !@reverting previous = @commands @commands = [] yield ensure @commands = previous.concat(@commands.reverse) @reverting = !@reverting end