@@ -55,7 +55,17 @@ def assert_called_for_configs(method_name, configs, &block)
55
55
sqlite3 : :sqlite_tasks
56
56
}
57
57
58
- class DatabaseTasksUtilsTask < ActiveRecord ::TestCase
58
+ class DatabaseTasksCheckProtectedEnvironmentsTest < ActiveRecord ::TestCase
59
+ self . use_transactional_tests = false
60
+
61
+ def setup
62
+ recreate_metadata_tables
63
+ end
64
+
65
+ def teardown
66
+ recreate_metadata_tables
67
+ end
68
+
59
69
def test_raises_an_error_when_called_with_protected_environment
60
70
protected_environments = ActiveRecord ::Base . protected_environments
61
71
current_env = ActiveRecord ::Base . connection . migration_context . current_environment
@@ -70,12 +80,12 @@ def test_raises_an_error_when_called_with_protected_environment
70
80
) do
71
81
assert_not_includes protected_environments , current_env
72
82
# Assert no error
73
- ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments!
83
+ ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments! ( "arunit" )
74
84
75
85
ActiveRecord ::Base . protected_environments = [ current_env ]
76
86
77
87
assert_raise ( ActiveRecord ::ProtectedEnvironmentError ) do
78
- ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments!
88
+ ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments! ( "arunit" )
79
89
end
80
90
end
81
91
ensure
@@ -96,11 +106,11 @@ def test_raises_an_error_when_called_with_protected_environment_which_name_is_a_
96
106
) do
97
107
assert_not_includes protected_environments , current_env
98
108
# Assert no error
99
- ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments!
109
+ ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments! ( "arunit" )
100
110
101
111
ActiveRecord ::Base . protected_environments = [ current_env . to_sym ]
102
112
assert_raise ( ActiveRecord ::ProtectedEnvironmentError ) do
103
- ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments!
113
+ ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments! ( "arunit" )
104
114
end
105
115
end
106
116
ensure
@@ -119,12 +129,89 @@ def test_raises_an_error_if_no_migrations_have_been_made
119
129
assert_not_predicate internal_metadata , :table_exists?
120
130
121
131
assert_raises ( ActiveRecord ::NoEnvironmentInSchemaError ) do
122
- ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments!
132
+ ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments! ( "arunit" )
123
133
end
124
134
ensure
125
135
schema_migration . delete_version ( "1" )
126
136
internal_metadata . create_table
127
137
end
138
+
139
+ private
140
+ def recreate_metadata_tables
141
+ schema_migration = ActiveRecord ::Base . connection . schema_migration
142
+ schema_migration . drop_table
143
+ schema_migration . create_table
144
+
145
+ internal_metadata = ActiveRecord ::Base . connection . internal_metadata
146
+ internal_metadata . drop_table
147
+ internal_metadata . create_table
148
+ end
149
+ end
150
+
151
+ if current_adapter? ( :SQLite3Adapter ) && !in_memory_db?
152
+ class DatabaseTasksCheckProtectedEnvironmentsMultiDatabaseTest < ActiveRecord ::TestCase
153
+ self . use_transactional_tests = false
154
+
155
+ def test_with_multiple_databases
156
+ env = ActiveRecord ::ConnectionHandling ::DEFAULT_ENV . call
157
+ with_multi_db_configurations ( env ) do
158
+ protected_environments = ActiveRecord ::Base . protected_environments
159
+ current_env = ActiveRecord ::Base . connection . migration_context . current_environment
160
+ assert_equal current_env , env
161
+
162
+ ActiveRecord ::Base . establish_connection ( :primary )
163
+ ActiveRecord ::Base . connection . internal_metadata . create_table_and_set_flags ( current_env )
164
+
165
+ ActiveRecord ::Base . establish_connection ( :secondary )
166
+ ActiveRecord ::Base . connection . internal_metadata . create_table_and_set_flags ( current_env )
167
+
168
+ assert_not_includes protected_environments , current_env
169
+ # Assert not raises
170
+ ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments! ( "test" )
171
+
172
+ ActiveRecord ::Base . establish_connection ( :secondary )
173
+ connection = ActiveRecord ::Base . connection
174
+ schema_migration = connection . schema_migration
175
+ schema_migration . create_table
176
+ schema_migration . create_version ( "1" )
177
+
178
+ ActiveRecord ::Base . protected_environments = [ current_env . to_sym ]
179
+ assert_raise ( ActiveRecord ::ProtectedEnvironmentError ) do
180
+ ActiveRecord ::Tasks ::DatabaseTasks . check_protected_environments! ( "test" )
181
+ end
182
+ ensure
183
+ ActiveRecord ::Base . protected_environments = protected_environments
184
+ end
185
+ end
186
+
187
+ private
188
+ def with_multi_db_configurations ( env )
189
+ old_configurations = ActiveRecord ::Base . configurations
190
+ ActiveRecord ::Base . configurations = {
191
+ env => {
192
+ primary : {
193
+ adapter : "sqlite3" ,
194
+ database : "test/fixtures/fixture_database.sqlite3" ,
195
+ } ,
196
+ secondary : {
197
+ adapter : "sqlite3" ,
198
+ database : "test/fixtures/fixture_database_2.sqlite3" ,
199
+ }
200
+ }
201
+ }
202
+
203
+ ActiveRecord ::Base . establish_connection ( :primary )
204
+ yield
205
+ ensure
206
+ [ :primary , :secondary ] . each do |db |
207
+ ActiveRecord ::Base . establish_connection ( db )
208
+ ActiveRecord ::Base . connection . schema_migration . drop_table
209
+ ActiveRecord ::Base . connection . internal_metadata . drop_table
210
+ end
211
+ ActiveRecord ::Base . configurations = old_configurations
212
+ ActiveRecord ::Base . establish_connection ( :arunit )
213
+ end
214
+ end
128
215
end
129
216
130
217
class DatabaseTasksRegisterTask < ActiveRecord ::TestCase
0 commit comments