Skip to content

Commit f15cef6

Browse files
committed
schema rake tasks are specific about the configuration to act on.
The rake tasks and the `DatabaseTakss` adapter classes used to assume a configuration at some places. This forced the rake tasks to establish a specific connection before calling into `load_schema`. After #15394 this started to cause issues because it could `purge` the wrong database before loading the schema.
1 parent 53dba73 commit f15cef6

4 files changed

Lines changed: 39 additions & 22 deletions

File tree

activerecord/CHANGELOG.md

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
* Deprecate `DatabaseTasks.load_schema` to act on the current connection.
2+
Use `.load_schema_current` instead. In the future `load_schema` will
3+
require the `configuration` to act on as an argument.
4+
5+
*Yves Senn*
6+
7+
* Fixed automatic maintaining test schema to properly handle sql structure
8+
schema format.
9+
10+
Fixes #15394.
11+
12+
*Wojciech Wnętrzak*
13+
114
* Fix type casting to Decimal from Float with large precision.
215

316
*Tomohiro Hashidate*
@@ -297,14 +310,7 @@
297310

298311
Fixes #8328.
299312

300-
Sean Griffin
301-
302-
* Fixed automatic maintaining test schema to properly handle sql structure
303-
schema format.
304-
305-
Fixes #15394.
306-
307-
*Wojciech Wnętrzak*
313+
*Sean Griffin*
308314

309315
* Pluck now works when selecting columns from different tables with the same
310316
name.

activerecord/lib/active_record/migration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ def check_pending!(connection = Base.connection)
399399

400400
def load_schema_if_pending!
401401
if ActiveRecord::Migrator.needs_migration?
402-
ActiveRecord::Tasks::DatabaseTasks.load_schema
402+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current
403403
check_pending!
404404
end
405405
end

activerecord/lib/active_record/railties/databases.rake

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ db_namespace = namespace :db do
240240

241241
desc 'Load a schema.rb file into the database'
242242
task :load => [:environment, :load_config] do
243-
ActiveRecord::Tasks::DatabaseTasks.load_schema(:ruby, ENV['SCHEMA'])
243+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
244244
end
245245

246246
task :load_if_ruby => ['db:create', :environment] do
@@ -286,7 +286,7 @@ db_namespace = namespace :db do
286286

287287
desc "Recreate the databases from the structure.sql file"
288288
task :load => [:environment, :load_config] do
289-
ActiveRecord::Tasks::DatabaseTasks.load_schema(:sql, ENV['DB_STRUCTURE'])
289+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV['DB_STRUCTURE'])
290290
end
291291

292292
task :load_if_sql => ['db:create', :environment] do
@@ -317,9 +317,8 @@ db_namespace = namespace :db do
317317
task :load_schema => %w(db:test:deprecated db:test:purge) do
318318
begin
319319
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
320-
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
321320
ActiveRecord::Schema.verbose = false
322-
db_namespace["schema:load"].invoke
321+
ActiveRecord::Tasks::DatabaseTasks.load_schema_for ActiveRecord::Base.configurations['test'], :ruby, ENV['SCHEMA']
323322
ensure
324323
if should_reconnect
325324
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
@@ -329,12 +328,7 @@ db_namespace = namespace :db do
329328

330329
# desc "Recreate the test database from an existent structure.sql file"
331330
task :load_structure => %w(db:test:deprecated db:test:purge) do
332-
begin
333-
ActiveRecord::Tasks::DatabaseTasks.current_config(:config => ActiveRecord::Base.configurations['test'])
334-
db_namespace["structure:load"].invoke
335-
ensure
336-
ActiveRecord::Tasks::DatabaseTasks.current_config(:config => nil)
337-
end
331+
ActiveRecord::Tasks::DatabaseTasks.load_schema_for ActiveRecord::Base.configurations['test'], :sql, ENV['SCHEMA']
338332
end
339333

340334
# desc "Recreate the test database from a fresh schema"

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,22 +184,39 @@ def structure_load(*arguments)
184184
end
185185

186186
def load_schema(format = ActiveRecord::Base.schema_format, file = nil)
187+
ActiveSupport::Deprecation.warn(<<-MESSAGE.strip_heredoc)
188+
This method will act on a specific connection in the future.
189+
To act on the current connection, use `load_schema_current` instead.
190+
MESSAGE
191+
load_schema_current(format, file)
192+
end
193+
194+
# This method is the successor of +load_schema+. We should rename it
195+
# after +load_schema+ went through a deprecation cycle. (Rails > 4.2)
196+
def load_schema_for(configuration, format = ActiveRecord::Base.schema_format, file = nil) # :nodoc:
187197
case format
188198
when :ruby
189199
file ||= File.join(db_dir, "schema.rb")
190200
check_schema_file(file)
191-
purge(current_config)
201+
purge(configuration)
202+
ActiveRecord::Base.establish_connection(configuration)
192203
load(file)
193204
when :sql
194205
file ||= File.join(db_dir, "structure.sql")
195206
check_schema_file(file)
196-
purge(current_config)
197-
structure_load(current_config, file)
207+
purge(configuration)
208+
structure_load(configuration, file)
198209
else
199210
raise ArgumentError, "unknown format #{format.inspect}"
200211
end
201212
end
202213

214+
def load_schema_current(format = ActiveRecord::Base.schema_format, file = nil, environment = env)
215+
each_current_configuration(environment) { |configuration|
216+
load_schema_for configuration, format, file
217+
}
218+
end
219+
203220
def check_schema_file(filename)
204221
unless File.exist?(filename)
205222
message = %{#{filename} doesn't exist yet. Run `rake db:migrate` to create it, then try again.}

0 commit comments

Comments
 (0)