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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
# frozen_string_literal: true
require "cases/helper"
module ActiveRecord
class Migration
class CommandRecorderTest < ActiveRecord::TestCase
def setup
connection = ActiveRecord::Base.connection
@recorder = CommandRecorder.new(connection)
end
def test_respond_to_delegates
recorder = CommandRecorder.new(Class.new {
def america; end
}.new)
assert_respond_to recorder, :america
end
def test_send_calls_super
assert_raises(NoMethodError) do
@recorder.send(:non_existing_method, :horses)
end
end
def test_send_delegates_to_record
recorder = CommandRecorder.new(Class.new {
def create_table(name); end
}.new)
assert_respond_to recorder, :create_table
recorder.send(:create_table, :horses)
assert_equal [[:create_table, [:horses], nil]], recorder.commands
end
def test_unknown_commands_delegate
recorder = Struct.new(:foo)
recorder = CommandRecorder.new(recorder.new("bar"))
assert_equal "bar", recorder.foo
end
def test_inverse_of_raise_exception_on_unknown_commands
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.inverse_of :execute, ["some sql"]
end
end
def test_irreversible_commands_raise_exception
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.revert { @recorder.execute "some sql" }
end
end
def test_record
@recorder.record :create_table, [:system_settings]
assert_equal 1, @recorder.commands.length
end
def test_inverted_commands_are_reversed
@recorder.revert do
@recorder.record :create_table, [:hello]
@recorder.record :create_table, [:world]
end
tables = @recorder.commands.map { |_cmd, args, _block| args }
assert_equal [[:world], [:hello]], tables
end
def test_revert_order
block = Proc.new { |t| t.string :name }
@recorder.instance_eval do
create_table("apples", &block)
revert do
create_table("bananas", &block)
revert do
create_table("clementines", &block)
create_table("dates")
end
create_table("elderberries")
end
revert do
create_table("figs", &block)
create_table("grapes")
end
end
assert_equal [[:create_table, ["apples"], block], [:drop_table, ["elderberries"], nil],
[:create_table, ["clementines"], block], [:create_table, ["dates"], nil],
[:drop_table, ["bananas"], block], [:drop_table, ["grapes"], nil],
[:drop_table, ["figs"], block]], @recorder.commands
end
def test_invert_change_table
@recorder.revert do
@recorder.change_table :fruits do |t|
t.string :name
t.rename :kind, :cultivar
end
end
assert_equal [
[:rename_column, [:fruits, :cultivar, :kind]],
[:remove_column, [:fruits, :name, :string, {}], nil],
], @recorder.commands
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.revert do
@recorder.change_table :fruits do |t|
t.remove :kind
end
end
end
end
def test_invert_create_table
@recorder.revert do
@recorder.record :create_table, [:system_settings]
end
drop_table = @recorder.commands.first
assert_equal [:drop_table, [:system_settings], nil], drop_table
end
def test_invert_create_table_with_options_and_block
block = Proc.new { }
drop_table = @recorder.inverse_of :create_table, [:people_reminders, id: false], &block
assert_equal [:drop_table, [:people_reminders, id: false], block], drop_table
end
def test_invert_drop_table
block = Proc.new { }
create_table = @recorder.inverse_of :drop_table, [:people_reminders, id: false], &block
assert_equal [:create_table, [:people_reminders, id: false], block], create_table
end
def test_invert_drop_table_without_a_block_nor_option
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.inverse_of :drop_table, [:people_reminders]
end
end
def test_invert_create_join_table
drop_join_table = @recorder.inverse_of :create_join_table, [:musics, :artists]
assert_equal [:drop_join_table, [:musics, :artists], nil], drop_join_table
end
def test_invert_create_join_table_with_table_name
drop_join_table = @recorder.inverse_of :create_join_table, [:musics, :artists, table_name: :catalog]
assert_equal [:drop_join_table, [:musics, :artists, table_name: :catalog], nil], drop_join_table
end
def test_invert_drop_join_table
block = Proc.new { }
create_join_table = @recorder.inverse_of :drop_join_table, [:musics, :artists, table_name: :catalog], &block
assert_equal [:create_join_table, [:musics, :artists, table_name: :catalog], block], create_join_table
end
def test_invert_rename_table
rename = @recorder.inverse_of :rename_table, [:old, :new]
assert_equal [:rename_table, [:new, :old]], rename
end
def test_invert_add_column
remove = @recorder.inverse_of :add_column, [:table, :column, :type, {}]
assert_equal [:remove_column, [:table, :column, :type, {}], nil], remove
end
def test_invert_change_column
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.inverse_of :change_column, [:table, :column, :type, {}]
end
end
def test_invert_change_column_default
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.inverse_of :change_column_default, [:table, :column, "default_value"]
end
end
def test_invert_change_column_default_with_from_and_to
change = @recorder.inverse_of :change_column_default, [:table, :column, from: "old_value", to: "new_value"]
assert_equal [:change_column_default, [:table, :column, from: "new_value", to: "old_value"]], change
end
def test_invert_change_column_default_with_from_and_to_with_boolean
change = @recorder.inverse_of :change_column_default, [:table, :column, from: true, to: false]
assert_equal [:change_column_default, [:table, :column, from: false, to: true]], change
end
def test_invert_change_column_null
add = @recorder.inverse_of :change_column_null, [:table, :column, true]
assert_equal [:change_column_null, [:table, :column, false]], add
end
def test_invert_remove_column
add = @recorder.inverse_of :remove_column, [:table, :column, :type, {}]
assert_equal [:add_column, [:table, :column, :type, {}], nil], add
end
def test_invert_remove_column_without_type
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.inverse_of :remove_column, [:table, :column]
end
end
def test_invert_rename_column
rename = @recorder.inverse_of :rename_column, [:table, :old, :new]
assert_equal [:rename_column, [:table, :new, :old]], rename
end
def test_invert_add_index
remove = @recorder.inverse_of :add_index, [:table, [:one, :two]]
assert_equal [:remove_index, [:table, { column: [:one, :two] }]], remove
end
def test_invert_add_index_with_name
remove = @recorder.inverse_of :add_index, [:table, [:one, :two], name: "new_index"]
assert_equal [:remove_index, [:table, { name: "new_index" }]], remove
end
def test_invert_add_index_with_algorithm_option
remove = @recorder.inverse_of :add_index, [:table, :one, algorithm: :concurrently]
assert_equal [:remove_index, [:table, { column: :one, algorithm: :concurrently }]], remove
end
def test_invert_remove_index
add = @recorder.inverse_of :remove_index, [:table, :one]
assert_equal [:add_index, [:table, :one]], add
end
def test_invert_remove_index_with_column
add = @recorder.inverse_of :remove_index, [:table, { column: [:one, :two], options: true }]
assert_equal [:add_index, [:table, [:one, :two], options: true]], add
end
def test_invert_remove_index_with_name
add = @recorder.inverse_of :remove_index, [:table, { column: [:one, :two], name: "new_index" }]
assert_equal [:add_index, [:table, [:one, :two], name: "new_index"]], add
end
def test_invert_remove_index_with_no_special_options
add = @recorder.inverse_of :remove_index, [:table, { column: [:one, :two] }]
assert_equal [:add_index, [:table, [:one, :two], {}]], add
end
def test_invert_remove_index_with_no_column
assert_raises(ActiveRecord::IrreversibleMigration) do
@recorder.inverse_of :remove_index, [:table, name: "new_index"]
end
end
def test_invert_rename_index
rename = @recorder.inverse_of :rename_index, [:table, :old, :new]
assert_equal [:rename_index, [:table, :new, :old]], rename
end
def test_invert_add_timestamps
remove = @recorder.inverse_of :add_timestamps, [:table]
assert_equal [:remove_timestamps, [:table], nil], remove
end
def test_invert_remove_timestamps
add = @recorder.inverse_of :remove_timestamps, [:table, { null: true }]
assert_equal [:add_timestamps, [:table, { null: true }], nil], add
end
def test_invert_add_reference
remove = @recorder.inverse_of :add_reference, [:table, :taggable, { polymorphic: true }]
assert_equal [:remove_reference, [:table, :taggable, { polymorphic: true }], nil], remove
end
def test_invert_add_belongs_to_alias
remove = @recorder.inverse_of :add_belongs_to, [:table, :user]
assert_equal [:remove_reference, [:table, :user], nil], remove
end
def test_invert_remove_reference
add = @recorder.inverse_of :remove_reference, [:table, :taggable, { polymorphic: true }]
assert_equal [:add_reference, [:table, :taggable, { polymorphic: true }], nil], add
end
def test_invert_remove_reference_with_index_and_foreign_key
add = @recorder.inverse_of :remove_reference, [:table, :taggable, { index: true, foreign_key: true }]
assert_equal [:add_reference, [:table, :taggable, { index: true, foreign_key: true }], nil], add
end
def test_invert_remove_belongs_to_alias
add = @recorder.inverse_of :remove_belongs_to, [:table, :user]
assert_equal [:add_reference, [:table, :user], nil], add
end
def test_invert_enable_extension
disable = @recorder.inverse_of :enable_extension, ["uuid-ossp"]
assert_equal [:disable_extension, ["uuid-ossp"], nil], disable
end
def test_invert_disable_extension
enable = @recorder.inverse_of :disable_extension, ["uuid-ossp"]
assert_equal [:enable_extension, ["uuid-ossp"], nil], enable
end
def test_invert_add_foreign_key
enable = @recorder.inverse_of :add_foreign_key, [:dogs, :people]
assert_equal [:remove_foreign_key, [:dogs, :people]], enable
end
def test_invert_remove_foreign_key
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, :people]
assert_equal [:add_foreign_key, [:dogs, :people]], enable
end
def test_invert_add_foreign_key_with_column
enable = @recorder.inverse_of :add_foreign_key, [:dogs, :people, column: "owner_id"]
assert_equal [:remove_foreign_key, [:dogs, column: "owner_id"]], enable
end
def test_invert_remove_foreign_key_with_column
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, :people, column: "owner_id"]
assert_equal [:add_foreign_key, [:dogs, :people, column: "owner_id"]], enable
end
def test_invert_add_foreign_key_with_column_and_name
enable = @recorder.inverse_of :add_foreign_key, [:dogs, :people, column: "owner_id", name: "fk"]
assert_equal [:remove_foreign_key, [:dogs, name: "fk"]], enable
end
def test_invert_remove_foreign_key_with_column_and_name
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, :people, column: "owner_id", name: "fk"]
assert_equal [:add_foreign_key, [:dogs, :people, column: "owner_id", name: "fk"]], enable
end
def test_invert_remove_foreign_key_with_primary_key
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, :people, primary_key: "person_id"]
assert_equal [:add_foreign_key, [:dogs, :people, primary_key: "person_id"]], enable
end
def test_invert_remove_foreign_key_with_primary_key_and_to_table_in_options
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, to_table: :people, primary_key: "uuid"]
assert_equal [:add_foreign_key, [:dogs, :people, primary_key: "uuid"]], enable
end
def test_invert_remove_foreign_key_with_on_delete_on_update
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, :people, on_delete: :nullify, on_update: :cascade]
assert_equal [:add_foreign_key, [:dogs, :people, on_delete: :nullify, on_update: :cascade]], enable
end
def test_invert_remove_foreign_key_with_to_table_in_options
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, to_table: :people]
assert_equal [:add_foreign_key, [:dogs, :people]], enable
enable = @recorder.inverse_of :remove_foreign_key, [:dogs, to_table: :people, column: :owner_id]
assert_equal [:add_foreign_key, [:dogs, :people, column: :owner_id]], enable
end
def test_invert_remove_foreign_key_is_irreversible_without_to_table
assert_raises ActiveRecord::IrreversibleMigration do
@recorder.inverse_of :remove_foreign_key, [:dogs, column: "owner_id"]
end
assert_raises ActiveRecord::IrreversibleMigration do
@recorder.inverse_of :remove_foreign_key, [:dogs, name: "fk"]
end
assert_raises ActiveRecord::IrreversibleMigration do
@recorder.inverse_of :remove_foreign_key, [:dogs]
end
end
end
end
end
|