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
|
# frozen_string_literal: true
require "isolation/abstract_unit"
module ApplicationTests
module RakeTests
class RakeMigrationsTest < ActiveSupport::TestCase
def setup
build_app
FileUtils.rm_rf("#{app_path}/config/environments")
end
def teardown
teardown_app
end
test "running migrations with given scope" do
rails "generate", "model", "user", "username:string", "password:string"
app_file "db/migrate/01_a_migration.bukkits.rb", <<-MIGRATION
class AMigration < ActiveRecord::Migration::Current
end
MIGRATION
output = rails("db:migrate", "SCOPE=bukkits")
assert_no_match(/create_table\(:users\)/, output)
assert_no_match(/CreateUsers/, output)
assert_no_match(/add_column\(:users, :email, :string\)/, output)
assert_match(/AMigration: migrated/, output)
output = rails("db:migrate", "SCOPE=bukkits", "VERSION=0")
assert_no_match(/drop_table\(:users\)/, output)
assert_no_match(/CreateUsers/, output)
assert_no_match(/remove_column\(:users, :email\)/, output)
assert_match(/AMigration: reverted/, output)
end
test "migrate with specified VERSION in different formats" do
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration::Current
end
MIGRATION
app_file "db/migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration::Current
end
MIGRATION
app_file "db/migrate/03_three_migration.rb", <<-MIGRATION
class ThreeMigration < ActiveRecord::Migration::Current
end
MIGRATION
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
assert_match(/up\s+003\s+Three migration/, output)
rails "db:migrate", "VERSION=01_one_migration.rb"
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/down\s+002\s+Two migration/, output)
assert_match(/down\s+003\s+Three migration/, output)
rails "db:migrate", "VERSION=3"
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
assert_match(/up\s+003\s+Three migration/, output)
rails "db:migrate", "VERSION=001"
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/down\s+002\s+Two migration/, output)
assert_match(/down\s+003\s+Three migration/, output)
end
test "migration with empty version" do
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration::Current
end
MIGRATION
app_file "db/migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration::Current
end
MIGRATION
rails("db:migrate", "VERSION=")
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
output = rails("db:migrate:redo", "VERSION=", allow_failure: true)
assert_match(/Empty VERSION provided/, output)
output = rails("db:migrate:up", "VERSION=", allow_failure: true)
assert_match(/VERSION is required/, output)
output = rails("db:migrate:up", allow_failure: true)
assert_match(/VERSION is required/, output)
output = rails("db:migrate:down", "VERSION=", allow_failure: true)
assert_match(/VERSION is required - To go down one migration, use db:rollback/, output)
output = rails("db:migrate:down", allow_failure: true)
assert_match(/VERSION is required - To go down one migration, use db:rollback/, output)
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
end
test "migration with 0 version" do
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration::Current
end
MIGRATION
app_file "db/migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration::Current
end
MIGRATION
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
rails "db:migrate", "VERSION=0"
output = rails("db:migrate:status")
assert_match(/down\s+001\s+One migration/, output)
assert_match(/down\s+002\s+Two migration/, output)
end
test "model and migration generator with change syntax" do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
output = rails("db:migrate")
assert_match(/create_table\(:users\)/, output)
assert_match(/CreateUsers: migrated/, output)
assert_match(/add_column\(:users, :email, :string\)/, output)
assert_match(/AddEmailToUsers: migrated/, output)
output = rails("db:rollback", "STEP=2")
assert_match(/drop_table\(:users\)/, output)
assert_match(/CreateUsers: reverted/, output)
assert_match(/remove_column\(:users, :email, :string\)/, output)
assert_match(/AddEmailToUsers: reverted/, output)
end
test "migration status when schema migrations table is not present" do
output = rails("db:migrate:status", allow_failure: true)
assert_equal "Schema migrations table does not exist yet.\n", output
end
test "migration status" do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
rails "db:rollback", "STEP=1"
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/down\s+\d{14}\s+Add email to users/, output)
end
test "migration status without timestamps" do
add_to_config("config.active_record.timestamped_migrations = false")
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
rails "db:rollback", "STEP=1"
output = rails("db:migrate:status")
assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/down\s+\d{3,}\s+Add email to users/, output)
end
test "migration status after rollback and redo" do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
rails "db:rollback", "STEP=2"
output = rails("db:migrate:status")
assert_match(/down\s+\d{14}\s+Create users/, output)
assert_match(/down\s+\d{14}\s+Add email to users/, output)
rails "db:migrate:redo"
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
end
test "migration status after rollback and forward" do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
rails "db:rollback", "STEP=2"
output = rails("db:migrate:status")
assert_match(/down\s+\d{14}\s+Create users/, output)
assert_match(/down\s+\d{14}\s+Add email to users/, output)
rails "db:forward", "STEP=2"
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+Add email to users/, output)
end
test "raise error on any move when current migration does not exist" do
Dir.chdir(app_path) do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
`rm db/migrate/*email*.rb`
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)
output = rails("db:rollback", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/ActiveRecord::UnknownMigrationVersionError:/, output)
assert_match(/No migration with version number\s\d{14}\./, output)
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)
output = rails("db:forward", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/ActiveRecord::UnknownMigrationVersionError:/, output)
assert_match(/No migration with version number\s\d{14}\./, output)
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)
end
end
test "raise error on any move when target migration does not exist" do
app_file "db/migrate/01_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration::Current
end
MIGRATION
app_file "db/migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration::Current
end
MIGRATION
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
output = rails("db:migrate", "VERSION=3", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/ActiveRecord::UnknownMigrationVersionError:/, output)
assert_match(/No migration with version number 3/, output)
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
end
test "raise error on any move when VERSION has invalid format" do
output = rails("db:migrate", "VERSION=unknown", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate", "VERSION=0.1.11", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate", "VERSION=1.1.11", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate", "VERSION='0 '", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate", "VERSION=1.", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate", "VERSION=1_", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate", "VERSION=1_name", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate:redo", "VERSION=unknown", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate:up", "VERSION=unknown", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
output = rails("db:migrate:down", "VERSION=unknown", allow_failure: true)
assert_match(/rails aborted!/, output)
assert_match(/Invalid format of target version/, output)
end
test "migration status after rollback and redo without timestamps" do
add_to_config("config.active_record.timestamped_migrations = false")
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
rails "db:rollback", "STEP=2"
output = rails("db:migrate:status")
assert_match(/down\s+\d{3,}\s+Create users/, output)
assert_match(/down\s+\d{3,}\s+Add email to users/, output)
rails "db:migrate:redo"
output = rails("db:migrate:status")
assert_match(/up\s+\d{3,}\s+Create users/, output)
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
end
test "running migrations with not timestamp head migration files" do
app_file "db/migrate/1_one_migration.rb", <<-MIGRATION
class OneMigration < ActiveRecord::Migration::Current
end
MIGRATION
app_file "db/migrate/02_two_migration.rb", <<-MIGRATION
class TwoMigration < ActiveRecord::Migration::Current
end
MIGRATION
rails "db:migrate"
output = rails("db:migrate:status")
assert_match(/up\s+001\s+One migration/, output)
assert_match(/up\s+002\s+Two migration/, output)
end
test "schema generation when dump_schema_after_migration is set" do
add_to_config("config.active_record.dump_schema_after_migration = false")
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
output = rails("generate", "model", "author", "name:string")
version = output =~ %r{[^/]+db/migrate/(\d+)_create_authors\.rb} && $1
rails "db:migrate", "db:rollback", "db:forward", "db:migrate:up", "db:migrate:down", "VERSION=#{version}"
assert !File.exist?("db/schema.rb"), "should not dump schema when configured not to"
end
add_to_config("config.active_record.dump_schema_after_migration = true")
Dir.chdir(app_path) do
rails "generate", "model", "reviews", "book_id:integer"
rails "db:migrate"
structure_dump = File.read("db/schema.rb")
assert_match(/create_table "reviews"/, structure_dump)
end
end
test "default schema generation after migration" do
Dir.chdir(app_path) do
rails "generate", "model", "book", "title:string"
rails "db:migrate"
structure_dump = File.read("db/schema.rb")
assert_match(/create_table "books"/, structure_dump)
end
end
test "migration status migrated file is deleted" do
Dir.chdir(app_path) do
rails "generate", "model", "user", "username:string", "password:string"
rails "generate", "migration", "add_email_to_users", "email:string"
rails "db:migrate"
`rm db/migrate/*email*.rb`
output = rails("db:migrate:status")
assert_match(/up\s+\d{14}\s+Create users/, output)
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)
end
end
end
end
end
|