@@ -137,15 +137,15 @@ class PendingMigrationError < MigrationError # :nodoc:
137
137
ActiveRecord ::Tasks ::DatabaseTasks . migrate
138
138
139
139
if ActiveRecord . dump_schema_after_migration
140
- ActiveRecord ::Tasks ::DatabaseTasks . dump_schema (
141
- ActiveRecord ::Base . connection_db_config
142
- )
140
+ connection = ActiveRecord ::Tasks ::DatabaseTasks . migration_connection
141
+ ActiveRecord ::Tasks ::DatabaseTasks . dump_schema ( connection . pool . db_config )
143
142
end
144
143
end
145
144
146
145
def initialize ( message = nil , pending_migrations : nil )
147
146
if pending_migrations . nil?
148
- pending_migrations = ActiveRecord ::Base . connection . migration_context . open . pending_migrations
147
+ connection = ActiveRecord ::Tasks ::DatabaseTasks . migration_connection
148
+ pending_migrations = connection . migration_context . open . pending_migrations
149
149
end
150
150
151
151
super ( message || detailed_migration_message ( pending_migrations ) )
@@ -165,6 +165,10 @@ def detailed_migration_message(pending_migrations)
165
165
166
166
message
167
167
end
168
+
169
+ def connection
170
+ ActiveRecord ::Tasks ::DatabaseTasks . migration_connection
171
+ end
168
172
end
169
173
170
174
class ConcurrentMigrationError < MigrationError # :nodoc:
@@ -624,6 +628,10 @@ def build_watcher(&block)
624
628
paths = all_configs . flat_map { |config | config . migrations_paths || Migrator . migrations_paths } . uniq
625
629
@file_watcher . new ( [ ] , paths . index_with ( [ "rb" ] ) , &block )
626
630
end
631
+
632
+ def connection
633
+ ActiveRecord ::Tasks ::DatabaseTasks . migration_connection
634
+ end
627
635
end
628
636
629
637
class << self
@@ -635,7 +643,7 @@ def nearest_delegate # :nodoc:
635
643
end
636
644
637
645
# Raises <tt>ActiveRecord::PendingMigrationError</tt> error if any migrations are pending.
638
- def check_pending! ( connection = Base . connection )
646
+ def check_pending! ( connection = ActiveRecord :: Tasks :: DatabaseTasks . migration_connection )
639
647
if pending_migrations = connection . migration_context . pending_migrations
640
648
raise ActiveRecord ::PendingMigrationError . new ( pending_migrations : pending_migrations )
641
649
end
@@ -645,6 +653,7 @@ def load_schema_if_pending!
645
653
if any_schema_needs_update?
646
654
# Roundtrip to Rake to allow plugins to hook into database initialization.
647
655
root = defined? ( ENGINE_ROOT ) ? ENGINE_ROOT : Rails . root
656
+
648
657
FileUtils . cd ( root ) do
649
658
Base . connection_handler . clear_all_connections! ( :all )
650
659
system ( "bin/rails db:test:prepare" )
@@ -692,25 +701,24 @@ def any_schema_needs_update?
692
701
end
693
702
end
694
703
704
+ def db_configs_in_current_env
705
+ ActiveRecord ::Base . configurations . configs_for ( env_name : env )
706
+ end
707
+
695
708
def pending_migrations
696
- prev_db_config = Base . connection_db_config
697
709
pending_migrations = [ ]
698
710
699
- db_configs_in_current_env . each do |db_config |
700
- conn = Base . establish_connection ( db_config ) . connection
701
- if pending = conn . migration_context . open . pending_migrations
711
+ ActiveRecord ::Tasks ::DatabaseTasks . with_temporary_connection_for_each ( env : env ) do |connection |
712
+ if pending = connection . migration_context . open . pending_migrations
702
713
pending_migrations << pending
703
714
end
704
715
end
705
716
706
717
pending_migrations . flatten
707
- ensure
708
- Base . establish_connection ( prev_db_config )
709
718
end
710
719
711
- def db_configs_in_current_env
712
- current_environment = ActiveRecord ::ConnectionHandling ::DEFAULT_ENV . call
713
- ActiveRecord ::Base . configurations . configs_for ( env_name : current_environment )
720
+ def env
721
+ ActiveRecord ::ConnectionHandling ::DEFAULT_ENV . call
714
722
end
715
723
end
716
724
@@ -893,7 +901,7 @@ def migrate(direction)
893
901
end
894
902
895
903
time = nil
896
- ActiveRecord ::Base . connection_pool . with_connection do |conn |
904
+ ActiveRecord ::Tasks :: DatabaseTasks . migration_connection . pool . with_connection do |conn |
897
905
time = Benchmark . measure do
898
906
exec_migration ( conn , direction )
899
907
end
@@ -957,7 +965,7 @@ def suppress_messages
957
965
end
958
966
959
967
def connection
960
- @connection || ActiveRecord ::Base . connection
968
+ @connection || ActiveRecord ::Tasks :: DatabaseTasks . migration_connection
961
969
end
962
970
963
971
def method_missing ( method , *arguments , &block )
@@ -1138,8 +1146,8 @@ def initialize(migrations_paths, schema_migration = nil, internal_metadata = nil
1138
1146
end
1139
1147
1140
1148
@migrations_paths = migrations_paths
1141
- @schema_migration = schema_migration || SchemaMigration . new ( ActiveRecord :: Base . connection )
1142
- @internal_metadata = internal_metadata || InternalMetadata . new ( ActiveRecord :: Base . connection )
1149
+ @schema_migration = schema_migration || SchemaMigration . new ( connection )
1150
+ @internal_metadata = internal_metadata || InternalMetadata . new ( connection )
1143
1151
end
1144
1152
1145
1153
# Runs the migrations in the +migrations_path+.
@@ -1265,7 +1273,6 @@ def protected_environment? # :nodoc:
1265
1273
end
1266
1274
1267
1275
def last_stored_environment # :nodoc:
1268
- connection = ActiveRecord ::Base . connection
1269
1276
return nil unless connection . internal_metadata . enabled?
1270
1277
return nil if current_version == 0
1271
1278
raise NoEnvironmentInSchemaError unless connection . internal_metadata . table_exists?
@@ -1276,6 +1283,10 @@ def last_stored_environment # :nodoc:
1276
1283
end
1277
1284
1278
1285
private
1286
+ def connection
1287
+ ActiveRecord ::Tasks ::DatabaseTasks . migration_connection
1288
+ end
1289
+
1279
1290
def migration_files
1280
1291
paths = Array ( migrations_paths )
1281
1292
Dir [ *paths . flat_map { |path | "#{ path } /**/[0-9]*_*.rb" } ]
@@ -1311,8 +1322,9 @@ class << self
1311
1322
1312
1323
# For cases where a table doesn't exist like loading from schema cache
1313
1324
def current_version
1314
- schema_migration = SchemaMigration . new ( ActiveRecord ::Base . connection )
1315
- internal_metadata = InternalMetadata . new ( ActiveRecord ::Base . connection )
1325
+ connection = ActiveRecord ::Tasks ::DatabaseTasks . migration_connection
1326
+ schema_migration = SchemaMigration . new ( connection )
1327
+ internal_metadata = InternalMetadata . new ( connection )
1316
1328
1317
1329
MigrationContext . new ( migrations_paths , schema_migration , internal_metadata ) . current_version
1318
1330
end
@@ -1388,6 +1400,10 @@ def load_migrated
1388
1400
end
1389
1401
1390
1402
private
1403
+ def connection
1404
+ ActiveRecord ::Tasks ::DatabaseTasks . migration_connection
1405
+ end
1406
+
1391
1407
# Used for running a specific migration.
1392
1408
def run_without_lock
1393
1409
migration = migrations . detect { |m | m . version == @target_version }
@@ -1411,7 +1427,7 @@ def migrate_without_lock
1411
1427
def record_environment
1412
1428
return if down?
1413
1429
1414
- @internal_metadata [ :environment ] = ActiveRecord :: Base . connection . migration_context . current_environment
1430
+ @internal_metadata [ :environment ] = connection . migration_context . current_environment
1415
1431
end
1416
1432
1417
1433
def ran? ( migration )
@@ -1481,23 +1497,22 @@ def down?
1481
1497
# Wrap the migration in a transaction only if supported by the adapter.
1482
1498
def ddl_transaction ( migration , &block )
1483
1499
if use_transaction? ( migration )
1484
- Base . transaction ( &block )
1500
+ connection . transaction ( &block )
1485
1501
else
1486
1502
yield
1487
1503
end
1488
1504
end
1489
1505
1490
1506
def use_transaction? ( migration )
1491
- !migration . disable_ddl_transaction && Base . connection . supports_ddl_transactions?
1507
+ !migration . disable_ddl_transaction && connection . supports_ddl_transactions?
1492
1508
end
1493
1509
1494
1510
def use_advisory_lock?
1495
- Base . connection . advisory_locks_enabled?
1511
+ connection . advisory_locks_enabled?
1496
1512
end
1497
1513
1498
1514
def with_advisory_lock
1499
1515
lock_id = generate_migrator_advisory_lock_id
1500
- connection = ActiveRecord ::Base . connection
1501
1516
1502
1517
got_lock = connection . get_advisory_lock ( lock_id )
1503
1518
raise ConcurrentMigrationError unless got_lock
@@ -1513,7 +1528,7 @@ def with_advisory_lock
1513
1528
1514
1529
MIGRATOR_SALT = 2053462845
1515
1530
def generate_migrator_advisory_lock_id
1516
- db_name_hash = Zlib . crc32 ( Base . connection . current_database )
1531
+ db_name_hash = Zlib . crc32 ( connection . current_database )
1517
1532
MIGRATOR_SALT * db_name_hash
1518
1533
end
1519
1534
end
0 commit comments