aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/migrator_test.rb
blob: aeba8e1d14b51d468cf9e74cbd01274a0abd1b48 (plain) (blame)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
# frozen_string_literal: true

require "cases/helper"
require "cases/migration/helper"

class MigratorTest < ActiveRecord::TestCase
  self.use_transactional_tests = false

  # Use this class to sense if migrations have gone
  # up or down.
  class Sensor < ActiveRecord::Migration::Current
    attr_reader :went_up, :went_down

    def initialize(name = self.class.name, version = nil)
      super
      @went_up = false
      @went_down = false
    end

    def up; @went_up = true; end
    def down; @went_down = true; end
  end

  def setup
    super
    @schema_migration = ActiveRecord::Base.connection.schema_migration
    @schema_migration.create_table
    @schema_migration.delete_all rescue nil
    @verbose_was = ActiveRecord::Migration.verbose
    ActiveRecord::Migration.message_count = 0
    ActiveRecord::Migration.class_eval do
      undef :puts
      def puts(*)
        ActiveRecord::Migration.message_count += 1
      end
    end
  end

  teardown do
    @schema_migration.delete_all rescue nil
    ActiveRecord::Migration.verbose = @verbose_was
    ActiveRecord::Migration.class_eval do
      undef :puts
      def puts(*)
        super
      end
    end
  end

  def test_migrator_with_duplicate_names
    e = assert_raises(ActiveRecord::DuplicateMigrationNameError) do
      list = [ActiveRecord::Migration.new("Chunky"), ActiveRecord::Migration.new("Chunky")]
      ActiveRecord::Migrator.new(:up, list, @schema_migration)
    end
    assert_match(/Multiple migrations have the name Chunky/, e.message)
  end

  def test_migrator_with_duplicate_versions
    assert_raises(ActiveRecord::DuplicateMigrationVersionError) do
      list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 1)]
      ActiveRecord::Migrator.new(:up, list, @schema_migration)
    end
  end

  def test_migrator_with_missing_version_numbers
    assert_raises(ActiveRecord::UnknownMigrationVersionError) do
      list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
      ActiveRecord::Migrator.new(:up, list, @schema_migration, 3).run
    end

    assert_raises(ActiveRecord::UnknownMigrationVersionError) do
      list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
      ActiveRecord::Migrator.new(:up, list, @schema_migration, -1).run
    end

    assert_raises(ActiveRecord::UnknownMigrationVersionError) do
      list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
      ActiveRecord::Migrator.new(:up, list, @schema_migration, 0).run
    end

    assert_raises(ActiveRecord::UnknownMigrationVersionError) do
      list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
      ActiveRecord::Migrator.new(:up, list, @schema_migration, 3).migrate
    end

    assert_raises(ActiveRecord::UnknownMigrationVersionError) do
      list = [ActiveRecord::Migration.new("Foo", 1), ActiveRecord::Migration.new("Bar", 2)]
      ActiveRecord::Migrator.new(:up, list, @schema_migration, -1).migrate
    end
  end

  def test_finds_migrations
    schema_migration = ActiveRecord::Base.connection.schema_migration
    migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid", schema_migration).migrations

    [[1, "ValidPeopleHaveLastNames"], [2, "WeNeedReminders"], [3, "InnocentJointable"]].each_with_index do |pair, i|
      assert_equal migrations[i].version, pair.first
      assert_equal migrations[i].name, pair.last
    end
  end

  def test_finds_migrations_in_subdirectories
    schema_migration = ActiveRecord::Base.connection.schema_migration
    migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid_with_subdirectories", schema_migration).migrations

    [[1, "ValidPeopleHaveLastNames"], [2, "WeNeedReminders"], [3, "InnocentJointable"]].each_with_index do |pair, i|
      assert_equal migrations[i].version, pair.first
      assert_equal migrations[i].name, pair.last
    end
  end

  def test_finds_migrations_from_two_directories
    schema_migration = ActiveRecord::Base.connection.schema_migration
    directories = [MIGRATIONS_ROOT + "/valid_with_timestamps", MIGRATIONS_ROOT + "/to_copy_with_timestamps"]
    migrations = ActiveRecord::MigrationContext.new(directories, schema_migration).migrations

    [[20090101010101, "PeopleHaveHobbies"],
     [20090101010202, "PeopleHaveDescriptions"],
     [20100101010101, "ValidWithTimestampsPeopleHaveLastNames"],
     [20100201010101, "ValidWithTimestampsWeNeedReminders"],
     [20100301010101, "ValidWithTimestampsInnocentJointable"]].each_with_index do |pair, i|
       assert_equal pair.first, migrations[i].version
       assert_equal pair.last, migrations[i].name
     end
  end

  def test_finds_migrations_in_numbered_directory
    schema_migration = ActiveRecord::Base.connection.schema_migration
    migrations = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/10_urban", schema_migration).migrations
    assert_equal 9, migrations[0].version
    assert_equal "AddExpressions", migrations[0].name
  end

  def test_relative_migrations
    schema_migration = ActiveRecord::Base.connection.schema_migration
    list = Dir.chdir(MIGRATIONS_ROOT) do
      ActiveRecord::MigrationContext.new("valid", schema_migration).migrations
    end

    migration_proxy = list.find { |item|
      item.name == "ValidPeopleHaveLastNames"
    }
    assert migration_proxy, "should find pending migration"
  end

  def test_finds_pending_migrations
    @schema_migration.create!(version: "1")
    migration_list = [ActiveRecord::Migration.new("foo", 1), ActiveRecord::Migration.new("bar", 3)]
    migrations = ActiveRecord::Migrator.new(:up, migration_list, @schema_migration).pending_migrations

    assert_equal 1, migrations.size
    assert_equal migration_list.last, migrations.first
  end

  def test_migrations_status
    path = MIGRATIONS_ROOT + "/valid"
    schema_migration = ActiveRecord::Base.connection.schema_migration

    @schema_migration.create(version: 2)
    @schema_migration.create(version: 10)

    assert_equal [
      ["down", "001", "Valid people have last names"],
      ["up",   "002", "We need reminders"],
      ["down", "003", "Innocent jointable"],
      ["up",   "010", "********** NO FILE **********"],
    ], ActiveRecord::MigrationContext.new(path, schema_migration).migrations_status
  end

  def test_migrations_status_in_subdirectories
    path = MIGRATIONS_ROOT + "/valid_with_subdirectories"
    schema_migration = ActiveRecord::Base.connection.schema_migration

    @schema_migration.create(version: 2)
    @schema_migration.create(version: 10)

    assert_equal [
      ["down", "001", "Valid people have last names"],
      ["up",   "002", "We need reminders"],
      ["down", "003", "Innocent jointable"],
      ["up",   "010", "********** NO FILE **********"],
    ], ActiveRecord::MigrationContext.new(path, schema_migration).migrations_status
  end

  def test_migrations_status_with_schema_define_in_subdirectories
    path = MIGRATIONS_ROOT + "/valid_with_subdirectories"
    prev_paths = ActiveRecord::Migrator.migrations_paths
    schema_migration = ActiveRecord::Base.connection.schema_migration
    ActiveRecord::Migrator.migrations_paths = path

    ActiveRecord::Schema.define(version: 3) do
    end

    assert_equal [
      ["up", "001", "Valid people have last names"],
      ["up", "002", "We need reminders"],
      ["up", "003", "Innocent jointable"],
    ], ActiveRecord::MigrationContext.new(path, schema_migration).migrations_status
  ensure
    ActiveRecord::Migrator.migrations_paths = prev_paths
  end

  def test_migrations_status_from_two_directories
    paths = [MIGRATIONS_ROOT + "/valid_with_timestamps", MIGRATIONS_ROOT + "/to_copy_with_timestamps"]
    schema_migration = ActiveRecord::Base.connection.schema_migration

    @schema_migration.create(version: "20100101010101")
    @schema_migration.create(version: "20160528010101")

    assert_equal [
      ["down", "20090101010101", "People have hobbies"],
      ["down", "20090101010202", "People have descriptions"],
      ["up",   "20100101010101", "Valid with timestamps people have last names"],
      ["down", "20100201010101", "Valid with timestamps we need reminders"],
      ["down", "20100301010101", "Valid with timestamps innocent jointable"],
      ["up",   "20160528010101", "********** NO FILE **********"],
    ], ActiveRecord::MigrationContext.new(paths, schema_migration).migrations_status
  end

  def test_migrator_interleaved_migrations
    pass_one = [Sensor.new("One", 1)]

    ActiveRecord::Migrator.new(:up, pass_one, @schema_migration).migrate
    assert pass_one.first.went_up
    assert_not pass_one.first.went_down

    pass_two = [Sensor.new("One", 1), Sensor.new("Three", 3)]
    ActiveRecord::Migrator.new(:up, pass_two, @schema_migration).migrate
    assert_not pass_two[0].went_up
    assert pass_two[1].went_up
    assert pass_two.all? { |x| !x.went_down }

    pass_three = [Sensor.new("One", 1),
                  Sensor.new("Two", 2),
                  Sensor.new("Three", 3)]

    ActiveRecord::Migrator.new(:down, pass_three, @schema_migration).migrate
    assert pass_three[0].went_down
    assert_not pass_three[1].went_down
    assert pass_three[2].went_down
  end

  def test_up_calls_up
    migrations = [Sensor.new(nil, 0), Sensor.new(nil, 1), Sensor.new(nil, 2)]
    migrator = ActiveRecord::Migrator.new(:up, migrations, @schema_migration)
    migrator.migrate
    assert migrations.all?(&:went_up)
    assert migrations.all? { |m| !m.went_down }
    assert_equal 2, migrator.current_version
  end

  def test_down_calls_down
    test_up_calls_up

    migrations = [Sensor.new(nil, 0), Sensor.new(nil, 1), Sensor.new(nil, 2)]
    migrator = ActiveRecord::Migrator.new(:down, migrations, @schema_migration)
    migrator.migrate
    assert migrations.all? { |m| !m.went_up }
    assert migrations.all?(&:went_down)
    assert_equal 0, migrator.current_version
  end

  def test_current_version
    @schema_migration.create!(version: "1000")
    schema_migration = ActiveRecord::Base.connection.schema_migration
    migrator = ActiveRecord::MigrationContext.new("db/migrate", schema_migration)
    assert_equal 1000, migrator.current_version
  end

  def test_migrator_one_up
    calls, migrations = sensors(3)

    ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 1).migrate
    assert_equal [[:up, 1]], calls
    calls.clear

    ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 2).migrate
    assert_equal [[:up, 2]], calls
  end

  def test_migrator_one_down
    calls, migrations = sensors(3)

    ActiveRecord::Migrator.new(:up, migrations, @schema_migration).migrate
    assert_equal [[:up, 1], [:up, 2], [:up, 3]], calls
    calls.clear

    ActiveRecord::Migrator.new(:down, migrations, @schema_migration, 1).migrate

    assert_equal [[:down, 3], [:down, 2]], calls
  end

  def test_migrator_one_up_one_down
    calls, migrations = sensors(3)

    ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 1).migrate
    assert_equal [[:up, 1]], calls
    calls.clear

    ActiveRecord::Migrator.new(:down, migrations, @schema_migration, 0).migrate
    assert_equal [[:down, 1]], calls
  end

  def test_migrator_double_up
    calls, migrations = sensors(3)
    migrator = ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 1)
    assert_equal(0, migrator.current_version)

    migrator.migrate
    assert_equal [[:up, 1]], calls
    calls.clear

    migrator.migrate
    assert_equal [], calls
  end

  def test_migrator_double_down
    calls, migrations = sensors(3)
    migrator = ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 1)

    assert_equal 0, migrator.current_version

    migrator.run
    assert_equal [[:up, 1]], calls
    calls.clear

    migrator = ActiveRecord::Migrator.new(:down, migrations, @schema_migration, 1)
    migrator.run
    assert_equal [[:down, 1]], calls
    calls.clear

    migrator.run
    assert_equal [], calls

    assert_equal 0, migrator.current_version
  end

  def test_migrator_verbosity
    _, migrations = sensors(3)

    ActiveRecord::Migration.verbose = true
    ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 1).migrate
    assert_not_equal 0, ActiveRecord::Migration.message_count

    ActiveRecord::Migration.message_count = 0

    ActiveRecord::Migrator.new(:down, migrations, @schema_migration, 0).migrate
    assert_not_equal 0, ActiveRecord::Migration.message_count
  end

  def test_migrator_verbosity_off
    _, migrations = sensors(3)

    ActiveRecord::Migration.verbose = false
    ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 1).migrate
    assert_equal 0, ActiveRecord::Migration.message_count
    ActiveRecord::Migrator.new(:down, migrations, @schema_migration, 0).migrate
    assert_equal 0, ActiveRecord::Migration.message_count
  end

  def test_target_version_zero_should_run_only_once
    calls, migrations = sensors(3)

    # migrate up to 1
    ActiveRecord::Migrator.new(:up, migrations, @schema_migration, 1).migrate
    assert_equal [[:up, 1]], calls
    calls.clear

    # migrate down to 0
    ActiveRecord::Migrator.new(:down, migrations, @schema_migration, 0).migrate
    assert_equal [[:down, 1]], calls
    calls.clear

    # migrate down to 0 again
    ActiveRecord::Migrator.new(:down, migrations, @schema_migration, 0).migrate
    assert_equal [], calls
  end

  def test_migrator_going_down_due_to_version_target
    schema_migration = ActiveRecord::Base.connection.schema_migration
    calls, migrator = migrator_class(3)
    migrator = migrator.new("valid", schema_migration)

    migrator.up(1)
    assert_equal [[:up, 1]], calls
    calls.clear

    migrator.migrate(0)
    assert_equal [[:down, 1]], calls
    calls.clear

    migrator.migrate
    assert_equal [[:up, 1], [:up, 2], [:up, 3]], calls
  end

  def test_migrator_output_when_running_multiple_migrations
    schema_migration = ActiveRecord::Base.connection.schema_migration
    _, migrator = migrator_class(3)
    migrator = migrator.new("valid", schema_migration)

    result = migrator.migrate
    assert_equal(3, result.count)

    # Nothing migrated from duplicate run
    result = migrator.migrate
    assert_equal(0, result.count)

    result = migrator.rollback
    assert_equal(1, result.count)
  end

  def test_migrator_output_when_running_single_migration
    schema_migration = ActiveRecord::Base.connection.schema_migration
    _, migrator = migrator_class(1)
    migrator = migrator.new("valid", schema_migration)

    result = migrator.run(:up, 1)

    assert_equal(1, result.version)
  end

  def test_migrator_rollback
    schema_migration = ActiveRecord::Base.connection.schema_migration
    _, migrator = migrator_class(3)
    migrator = migrator.new("valid", schema_migration)

    migrator.migrate
    assert_equal(3, migrator.current_version)

    migrator.rollback
    assert_equal(2, migrator.current_version)

    migrator.rollback
    assert_equal(1, migrator.current_version)

    migrator.rollback
    assert_equal(0, migrator.current_version)

    migrator.rollback
    assert_equal(0, migrator.current_version)
  end

  def test_migrator_db_has_no_schema_migrations_table
    schema_migration = ActiveRecord::Base.connection.schema_migration
    _, migrator = migrator_class(3)
    migrator = migrator.new("valid", schema_migration)

    ActiveRecord::SchemaMigration.drop_table
    assert_not_predicate ActiveRecord::SchemaMigration, :table_exists?
    migrator.migrate(1)
    assert_predicate ActiveRecord::SchemaMigration, :table_exists?
  end

  def test_migrator_forward
    schema_migration = ActiveRecord::Base.connection.schema_migration
    _, migrator = migrator_class(3)
    migrator = migrator.new("/valid", schema_migration)
    migrator.migrate(1)
    assert_equal(1, migrator.current_version)

    migrator.forward(2)
    assert_equal(3, migrator.current_version)

    migrator.forward
    assert_equal(3, migrator.current_version)
  end

  def test_only_loads_pending_migrations
    # migrate up to 1
    @schema_migration.create!(version: "1")

    schema_migration = ActiveRecord::Base.connection.schema_migration
    calls, migrator = migrator_class(3)
    migrator = migrator.new("valid", schema_migration)
    migrator.migrate

    assert_equal [[:up, 2], [:up, 3]], calls
  end

  def test_get_all_versions
    schema_migration = ActiveRecord::Base.connection.schema_migration
    _, migrator = migrator_class(3)
    migrator = migrator.new("valid", schema_migration)

    migrator.migrate
    assert_equal([1, 2, 3], migrator.get_all_versions)

    migrator.rollback
    assert_equal([1, 2], migrator.get_all_versions)

    migrator.rollback
    assert_equal([1], migrator.get_all_versions)

    migrator.rollback
    assert_equal([], migrator.get_all_versions)
  end

  private
    def m(name, version)
      x = Sensor.new name, version
      x.extend(Module.new {
        define_method(:up) { yield(:up, x); super() }
        define_method(:down) { yield(:down, x); super() }
      }) if block_given?
    end

    def sensors(count)
      calls = []
      migrations = count.times.map { |i|
        m(nil, i + 1) { |c, migration|
          calls << [c, migration.version]
        }
      }
      [calls, migrations]
    end

    def migrator_class(count)
      calls, migrations = sensors(count)

      migrator = Class.new(ActiveRecord::MigrationContext) {
        define_method(:migrations) { |*|
          migrations
        }
      }
      [calls, migrator]
    end
end