-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
command_recorder.rb
291 lines (245 loc) · 10.2 KB
/
command_recorder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# frozen_string_literal: true
module ActiveRecord
class Migration
# <tt>ActiveRecord::Migration::CommandRecorder</tt> 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_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_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 supply a +:type+ option)
# * remove_foreign_key (must supply a second table)
# * remove_check_constraint
# * remove_index
# * remove_reference
# * remove_timestamps
# * rename_column
# * rename_index
# * rename_table
class CommandRecorder
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
]
include JoinTable
attr_accessor :commands, :delegate, :reverting
def initialize(delegate = nil)
@commands = []
@delegate = delegate
@reverting = false
end
# 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])
def revert
@reverting = !@reverting
previous = @commands
@commands = []
yield
ensure
@commands = previous.concat(@commands.reverse)
@reverting = !@reverting
end
# Record +command+. +command+ should be a method name and arguments.
# For example:
#
# recorder.record(:method_name, [:arg1, :arg2])
def record(*command, &block)
if @reverting
@commands << inverse_of(*command, &block)
else
@commands << (command << block)
end
end
# 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, :bar, type: :string])
# # => [[:add_column, :some_table, :foo, :string], [:add_column, :some_table, :bar, :string]]
#
# This method will raise an +IrreversibleMigration+ exception if it cannot
# invert the +command+.
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
ReversibleAndIrreversibleMethods.each do |method|
class_eval <<-EOV, __FILE__, __LINE__ + 1
def #{method}(*args, &block) # def create_table(*args, &block)
record(:"#{method}", args, &block) # record(:create_table, args, &block)
end # end
EOV
ruby2_keywords(method)
end
alias :add_belongs_to :add_reference
alias :remove_belongs_to :remove_reference
def change_table(table_name, **options) # :nodoc:
yield delegate.update_table_definition(table_name, self)
end
def replay(migration)
commands.each do |cmd, args, block|
migration.send(cmd, *args, &block)
end
end
private
module StraightReversions # :nodoc:
private
{
execute_block: :execute_block,
create_table: :drop_table,
create_join_table: :drop_join_table,
add_column: :remove_column,
add_index: :remove_index,
add_timestamps: :remove_timestamps,
add_reference: :remove_reference,
add_foreign_key: :remove_foreign_key,
add_check_constraint: :remove_check_constraint,
enable_extension: :disable_extension
}.each do |cmd, inv|
[[inv, cmd], [cmd, inv]].uniq.each do |method, inverse|
class_eval <<-EOV, __FILE__, __LINE__ + 1
def invert_#{method}(args, &block) # def invert_create_table(args, &block)
[:#{inverse}, args, block] # [:drop_table, args, block]
end # end
EOV
end
end
end
include StraightReversions
def invert_transaction(args, &block)
sub_recorder = CommandRecorder.new(delegate)
sub_recorder.revert(&block)
invertions_proc = proc {
sub_recorder.replay(self)
}
[:transaction, args, invertions_proc]
end
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
def invert_rename_table(args)
[:rename_table, args.reverse]
end
def invert_remove_column(args)
raise ActiveRecord::IrreversibleMigration, "remove_column is only reversible if given a type." if args.size <= 2
super
end
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
def invert_rename_index(args)
table_name, old_name, new_name = args
[:rename_index, [table_name, new_name, old_name]]
end
def invert_rename_column(args)
table_name, old_name, new_name = args
[:rename_column, [table_name, new_name, old_name]]
end
def invert_remove_index(args)
options = args.extract_options!
table, columns = args
columns ||= options.delete(:column)
unless columns
raise ActiveRecord::IrreversibleMigration, "remove_index is only reversible if given a :column option."
end
options.delete(:if_exists)
args = [table, columns]
args << options unless options.empty?
[:add_index, args]
end
alias :invert_add_belongs_to :invert_add_reference
alias :invert_remove_belongs_to :invert_remove_reference
def invert_change_column_default(args)
table, column, options = args
unless options.is_a?(Hash) && options.has_key?(:from) && options.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: options[:to], to: options[:from]]]
end
def invert_change_column_null(args)
args[2] = !args[2]
[:change_column_null, args]
end
def invert_remove_foreign_key(args)
options = args.extract_options!
from_table, to_table = args
to_table ||= options.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 << options unless options.empty?
[:add_foreign_key, reversed_args]
end
def invert_change_column_comment(args)
table, column, options = args
unless options.is_a?(Hash) && options.has_key?(:from) && options.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: options[:to], to: options[:from]]]
end
def invert_change_table_comment(args)
table, options = args
unless options.is_a?(Hash) && options.has_key?(:from) && options.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: options[:to], to: options[:from]]]
end
def invert_remove_check_constraint(args)
raise ActiveRecord::IrreversibleMigration, "remove_check_constraint is only reversible if given an expression." if args.size < 2
super
end
def respond_to_missing?(method, _)
super || delegate.respond_to?(method)
end
# Forwards any missing method call to the \target.
def method_missing(method, *args, &block)
if delegate.respond_to?(method)
delegate.public_send(method, *args, &block)
else
super
end
end
ruby2_keywords(:method_missing)
end
end
end