File tree Expand file tree Collapse file tree 4 files changed +61
-7
lines changed
activerecord/lib/active_record
railties/test/application/rake Expand file tree Collapse file tree 4 files changed +61
-7
lines changed Original file line number Diff line number Diff line change @@ -1140,7 +1140,11 @@ def current_version # :nodoc:
1140
1140
end
1141
1141
1142
1142
def needs_migration? # :nodoc:
1143
- ( migrations . collect ( &:version ) - get_all_versions ) . size > 0
1143
+ pending_migration_versions . size > 0
1144
+ end
1145
+
1146
+ def pending_migration_versions # :nodoc:
1147
+ migrations . collect ( &:version ) - get_all_versions
1144
1148
end
1145
1149
1146
1150
def migrations # :nodoc:
Original file line number Diff line number Diff line change @@ -86,14 +86,23 @@ db_namespace = namespace :db do
86
86
87
87
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
88
88
task migrate : :load_config do
89
- original_db_config = ActiveRecord ::Base . connection_db_config
90
- ActiveRecord :: Base . configurations . configs_for ( env_name : ActiveRecord :: Tasks :: DatabaseTasks . env ) . each do | db_config |
91
- ActiveRecord :: Base . establish_connection ( db_config )
89
+ db_configs = ActiveRecord ::Base . configurations . configs_for ( env_name : ActiveRecord :: Tasks :: DatabaseTasks . env )
90
+
91
+ if db_configs . size == 1
92
92
ActiveRecord ::Tasks ::DatabaseTasks . migrate
93
+ else
94
+ original_db_config = ActiveRecord ::Base . connection_db_config
95
+ mapped_versions = ActiveRecord ::Tasks ::DatabaseTasks . db_configs_with_versions ( db_configs )
96
+
97
+ mapped_versions . sort . each do |version , db_config |
98
+ ActiveRecord ::Base . establish_connection ( db_config )
99
+ ActiveRecord ::Tasks ::DatabaseTasks . migrate ( version )
100
+ end
93
101
end
102
+
94
103
db_namespace [ "_dump" ] . invoke
95
104
ensure
96
- ActiveRecord ::Base . establish_connection ( original_db_config )
105
+ ActiveRecord ::Base . establish_connection ( original_db_config ) if original_db_config
97
106
end
98
107
99
108
# IMPORTANT: This task won't dump the schema if ActiveRecord.dump_schema_after_migration is set to false
Original file line number Diff line number Diff line change @@ -268,13 +268,13 @@ def truncate_all(environment = env)
268
268
end
269
269
end
270
270
271
- def migrate
271
+ def migrate ( version = nil )
272
272
check_target_version
273
273
274
274
scope = ENV [ "SCOPE" ]
275
275
verbose_was , Migration . verbose = Migration . verbose , verbose?
276
276
277
- Base . connection . migration_context . migrate ( target_version ) do |migration |
277
+ Base . connection . migration_context . migrate ( target_version || version ) do |migration |
278
278
scope . blank? || scope == migration . scope
279
279
end . tap do |migrations_ran |
280
280
Migration . write ( "No migrations ran. (using #{ scope } scope)" ) if scope . present? && migrations_ran . empty?
@@ -285,6 +285,23 @@ def migrate
285
285
Migration . verbose = verbose_was
286
286
end
287
287
288
+ def db_configs_with_versions ( db_configs ) # :nodoc:
289
+ db_configs_with_versions = { }
290
+
291
+ db_configs . each do |db_config |
292
+ ActiveRecord ::Base . establish_connection ( db_config )
293
+ versions_to_run = ActiveRecord ::Base . connection . migration_context . pending_migration_versions
294
+ target_version = ActiveRecord ::Tasks ::DatabaseTasks . target_version
295
+
296
+ versions_to_run . each do |version |
297
+ next if target_version && target_version != version
298
+ db_configs_with_versions [ version ] = db_config
299
+ end
300
+ end
301
+
302
+ db_configs_with_versions
303
+ end
304
+
288
305
def migrate_status
289
306
unless ActiveRecord ::Base . connection . schema_migration . table_exists?
290
307
Kernel . abort "Schema migrations table does not exist yet."
Original file line number Diff line number Diff line change @@ -427,6 +427,30 @@ def generate_models_for_animals
427
427
end
428
428
end
429
429
430
+ test "db:migrate respects timestamp ordering across databases" do
431
+ require "#{ app_path } /config/environment"
432
+ app_file "db/migrate/01_one_migration.rb" , <<-MIGRATION
433
+ class OneMigration < ActiveRecord::Migration::Current
434
+ end
435
+ MIGRATION
436
+
437
+ app_file "db/animals_migrate/02_two_migration.rb" , <<-MIGRATION
438
+ class TwoMigration < ActiveRecord::Migration::Current
439
+ end
440
+ MIGRATION
441
+
442
+ app_file "db/migrate/03_three_migration.rb" , <<-MIGRATION
443
+ class ThreeMigration < ActiveRecord::Migration::Current
444
+ end
445
+ MIGRATION
446
+
447
+ Dir . chdir ( app_path ) do
448
+ output = rails "db:migrate"
449
+ entries = output . scan ( /^== (\d +).+migrated/ ) . map ( &:first ) . map ( &:to_i )
450
+ assert_equal [ 1 , 2 , 3 ] , entries
451
+ end
452
+ end
453
+
430
454
test "db:migrate and db:schema:dump and db:schema:load works on all databases" do
431
455
db_migrate_and_schema_dump_and_load
432
456
end
You can’t perform that action at this time.
0 commit comments