@@ -6,11 +6,16 @@ module ActiveRecord
6
6
class Migration
7
7
if current_adapter? ( :SQLite3Adapter ) && !in_memory_db?
8
8
class PendingMigrationsTest < ActiveRecord ::TestCase
9
+ self . use_transactional_tests = false
10
+
9
11
setup do
10
12
@migration_dir = Dir . mktmpdir ( "activerecord-migrations-" )
13
+ @secondary_migration_dir = Dir . mktmpdir ( "activerecord-migrations-secondary-" )
14
+ @original_configurations = ActiveRecord ::Base . configurations
15
+ ActiveRecord ::Base . configurations = base_config
16
+ ActiveRecord ::Base . establish_connection ( :primary )
11
17
12
18
file = ActiveRecord ::Base . connection . raw_connection . filename
13
- @conn = ActiveRecord ::Base . establish_connection adapter : "sqlite3" , database : ":memory:" , migrations_paths : @migration_dir
14
19
source_db = SQLite3 ::Database . new file
15
20
dest_db = ActiveRecord ::Base . connection . raw_connection
16
21
backup = SQLite3 ::Backup . new ( dest_db , "main" , source_db , "main" )
@@ -23,19 +28,20 @@ class PendingMigrationsTest < ActiveRecord::TestCase
23
28
end
24
29
25
30
teardown do
26
- @conn . release_connection if @conn
27
- ActiveRecord ::Base . establish_connection :arunit
31
+ ActiveRecord :: Base . configurations = @original_configurations
32
+ ActiveRecord ::Base . establish_connection ( :arunit )
28
33
FileUtils . rm_rf ( @migration_dir )
34
+ FileUtils . rm_rf ( @secondary_migration_dir )
29
35
end
30
36
31
37
def run_migrations
32
38
migrator = Base . connection . migration_context
33
39
capture ( :stdout ) { migrator . migrate }
34
40
end
35
41
36
- def create_migration ( number , name )
42
+ def create_migration ( number , name , migration_dir : nil )
37
43
filename = "#{ number } _#{ name . underscore } .rb"
38
- File . write ( File . join ( @migration_dir , filename ) , <<~RUBY )
44
+ File . write ( File . join ( migration_dir || @migration_dir , filename ) , <<~RUBY )
39
45
class #{ name . classify } < ActiveRecord::Migration::Current
40
46
end
41
47
RUBY
@@ -86,7 +92,7 @@ def test_okay_with_no_migrations
86
92
def test_understands_migrations_created_out_of_order
87
93
# With a prior file before even initialization
88
94
create_migration "05" , "create_bar"
89
- run_migrations
95
+ quietly { run_migrations }
90
96
91
97
check_pending = CheckPending . new ( @app )
92
98
@@ -100,6 +106,65 @@ def test_understands_migrations_created_out_of_order
100
106
check_pending . call ( { } )
101
107
end
102
108
end
109
+
110
+ def test_with_multiple_database
111
+ create_migration "01" , "create_bar" , migration_dir : @secondary_migration_dir
112
+
113
+ assert_raises ActiveRecord ::PendingMigrationError do
114
+ CheckPending . new ( @app ) . call ( { } )
115
+ end
116
+
117
+ begin
118
+ ActiveRecord ::Base . establish_connection ( :secondary )
119
+ quietly { run_migrations }
120
+ end
121
+ ActiveRecord ::Base . establish_connection ( :primary )
122
+
123
+ @app . expect :call , nil , [ { } ]
124
+ CheckPending . new ( @app ) . call ( { } )
125
+ @app . verify
126
+
127
+ # Now check exclusion if database_tasks is set to false for the db_config
128
+ create_migration "02" , "create_foo" , migration_dir : @secondary_migration_dir
129
+ assert_raises ActiveRecord ::PendingMigrationError do
130
+ CheckPending . new ( @app ) . call ( { } )
131
+ end
132
+
133
+ new_config = base_config
134
+ new_config [ ActiveRecord ::ConnectionHandling ::DEFAULT_ENV . call ] [ :secondary ] [ :database_tasks ] = false
135
+ ActiveRecord ::Base . configurations = new_config
136
+
137
+ @app . expect :call , nil , [ { } ]
138
+ CheckPending . new ( @app ) . call ( { } )
139
+ @app . verify
140
+ end
141
+
142
+ def test_with_stdlib_logger
143
+ old , ActiveRecord ::Base . logger = ActiveRecord ::Base . logger , ::Logger . new ( $stdout)
144
+ quietly do
145
+ assert_nothing_raised { ActiveRecord ::Migration ::CheckPending . new ( Proc . new { } ) . call ( { } ) }
146
+ end
147
+ ensure
148
+ ActiveRecord ::Base . logger = old
149
+ end
150
+
151
+ private
152
+ def base_config
153
+ {
154
+ ActiveRecord ::ConnectionHandling ::DEFAULT_ENV . call => {
155
+ primary : {
156
+ adapter : "sqlite3" ,
157
+ database : "test/fixtures/fixture_database.sqlite3" ,
158
+ migrations_paths : @migration_dir
159
+ } ,
160
+ secondary : {
161
+ adapter : "sqlite3" ,
162
+ database : "test/fixtures/fixture_database.sqlite3" ,
163
+ migrations_paths : @secondary_migration_dir
164
+ }
165
+ }
166
+ }
167
+ end
103
168
end
104
169
end
105
170
end
0 commit comments