Skip to content

Commit 1cf7025

Browse files
committed
Allow passing a DatabaseConfig object to cache_dump_filename
1 parent e3efad9 commit 1cf7025

File tree

5 files changed

+113
-19
lines changed

5 files changed

+113
-19
lines changed

activerecord/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
* Deprecate passing strings to `ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename`.
2+
3+
A `ActiveRecord::DatabaseConfigurations::DatabaseConfig` object should be passed instead.
4+
5+
*Rafael Mendonça França*
6+
17
* Add row_count field to sql.active_record notification
28

39
This field returns the amount of rows returned by the query that emitted the notification.

activerecord/lib/active_record/railtie.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ class Railtie < Rails::Railtie # :nodoc:
148148
ActiveSupport.on_load(:active_record) do
149149
db_config = ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first
150150

151-
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
152-
db_config.name,
153-
schema_cache_path: db_config.schema_cache_path
154-
)
151+
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
155152

156153
cache = ActiveRecord::ConnectionAdapters::SchemaCache._load_from(filename)
157154
next if cache.nil?

activerecord/lib/active_record/railties/databases.rake

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ db_namespace = namespace :db do
509509
task dump: :load_config do
510510
ActiveRecord::Tasks::DatabaseTasks.with_temporary_connection_for_each do |conn|
511511
db_config = conn.pool.db_config
512-
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config.name, schema_cache_path: db_config.schema_cache_path)
512+
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
513513

514514
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(conn, filename)
515515
end
@@ -518,10 +518,7 @@ db_namespace = namespace :db do
518518
desc "Clear a db/schema_cache.yml file."
519519
task clear: :load_config do
520520
ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env).each do |db_config|
521-
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(
522-
db_config.name,
523-
schema_cache_path: db_config.schema_cache_path,
524-
)
521+
filename = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(db_config)
525522
ActiveRecord::Tasks::DatabaseTasks.clear_schema_cache(
526523
filename,
527524
)

activerecord/lib/active_record/tasks/database_tasks.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,29 @@ def schema_dump_path(db_config, format = ActiveRecord.schema_format)
437437
end
438438
end
439439

440-
def cache_dump_filename(db_config_name, schema_cache_path: nil)
441-
filename = if ActiveRecord::Base.configurations.primary?(db_config_name)
442-
"schema_cache.yml"
440+
def cache_dump_filename(db_config_or_name, schema_cache_path: nil)
441+
if db_config_or_name.is_a?(DatabaseConfigurations::DatabaseConfig)
442+
filename = if db_config_or_name.primary?
443+
"schema_cache.yml"
444+
else
445+
"#{db_config_or_name.name}_schema_cache.yml"
446+
end
447+
448+
schema_cache_path || db_config_or_name.schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
443449
else
444-
"#{db_config_name}_schema_cache.yml"
445-
end
450+
ActiveRecord.deprecator.deprecation_warning(<<~MSG.squish)
451+
Passing a database name to `cache_dump_filename` is deprecated and will be removed in Rails 7.3. Pass a
452+
`ActiveRecord::DatabaseConfigurations::DatabaseConfig` object instead.
453+
MSG
446454

447-
schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
455+
filename = if ActiveRecord::Base.configurations.primary?(db_config_or_name)
456+
"schema_cache.yml"
457+
else
458+
"#{db_config_or_name}_schema_cache.yml"
459+
end
460+
461+
schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename)
462+
end
448463
end
449464

450465
def load_schema_current(format = ActiveRecord.schema_format, file = nil, environment = env)

activerecord/test/cases/tasks/database_tasks_test.rb

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,24 @@ def test_cache_dump_default_filename
315315
old_path = ENV["SCHEMA_CACHE"]
316316
ENV.delete("SCHEMA_CACHE")
317317

318+
config = DatabaseConfigurations::HashConfig.new("development", "primary", {})
319+
318320
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
319-
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary")
321+
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
322+
assert_equal "db/schema_cache.yml", path
323+
end
324+
ensure
325+
ENV["SCHEMA_CACHE"] = old_path
326+
end
327+
328+
def test_deprecated_cache_dump_default_filename
329+
old_path = ENV["SCHEMA_CACHE"]
330+
ENV.delete("SCHEMA_CACHE")
331+
332+
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
333+
path = assert_deprecated(ActiveRecord.deprecator) do
334+
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary")
335+
end
320336
assert_equal "db/schema_cache.yml", path
321337
end
322338
ensure
@@ -327,8 +343,24 @@ def test_cache_dump_alternate_filename
327343
old_path = ENV["SCHEMA_CACHE"]
328344
ENV.delete("SCHEMA_CACHE")
329345

346+
config = DatabaseConfigurations::HashConfig.new("development", "alternate", {})
347+
348+
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
349+
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
350+
assert_equal "db/alternate_schema_cache.yml", path
351+
end
352+
ensure
353+
ENV["SCHEMA_CACHE"] = old_path
354+
end
355+
356+
def test_deprecated_cache_dump_alternate_filename
357+
old_path = ENV["SCHEMA_CACHE"]
358+
ENV.delete("SCHEMA_CACHE")
359+
330360
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
331-
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("alternate")
361+
path = assert_deprecated(ActiveRecord.deprecator) do
362+
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("alternate")
363+
end
332364
assert_equal "db/alternate_schema_cache.yml", path
333365
end
334366
ensure
@@ -339,8 +371,24 @@ def test_cache_dump_filename_with_env_override
339371
old_path = ENV["SCHEMA_CACHE"]
340372
ENV["SCHEMA_CACHE"] = "tmp/something.yml"
341373

374+
config = DatabaseConfigurations::HashConfig.new("development", "primary", {})
375+
342376
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
343-
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary")
377+
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
378+
assert_equal "tmp/something.yml", path
379+
end
380+
ensure
381+
ENV["SCHEMA_CACHE"] = old_path
382+
end
383+
384+
def test_deprecated_cache_dump_filename_with_env_override
385+
old_path = ENV["SCHEMA_CACHE"]
386+
ENV["SCHEMA_CACHE"] = "tmp/something.yml"
387+
388+
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
389+
path = assert_deprecated(ActiveRecord.deprecator) do
390+
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary")
391+
end
344392
assert_equal "tmp/something.yml", path
345393
end
346394
ensure
@@ -351,8 +399,39 @@ def test_cache_dump_filename_with_path_from_db_config
351399
old_path = ENV["SCHEMA_CACHE"]
352400
ENV.delete("SCHEMA_CACHE")
353401

402+
config = DatabaseConfigurations::HashConfig.new("development", "primary", { schema_cache_path: "tmp/something.yml" })
403+
354404
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
355-
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary", schema_cache_path: "tmp/something.yml")
405+
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config)
406+
assert_equal "tmp/something.yml", path
407+
end
408+
ensure
409+
ENV["SCHEMA_CACHE"] = old_path
410+
end
411+
412+
413+
def test_cache_dump_filename_with_path_from_the_argument_has_precedence
414+
old_path = ENV["SCHEMA_CACHE"]
415+
ENV.delete("SCHEMA_CACHE")
416+
417+
config = DatabaseConfigurations::HashConfig.new("development", "primary", { schema_cache_path: "tmp/something.yml" })
418+
419+
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
420+
path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config, schema_cache_path: "tmp/another.yml")
421+
assert_equal "tmp/another.yml", path
422+
end
423+
ensure
424+
ENV["SCHEMA_CACHE"] = old_path
425+
end
426+
427+
def test_deprecated_cache_dump_filename_with_path_from_the_argument
428+
old_path = ENV["SCHEMA_CACHE"]
429+
ENV.delete("SCHEMA_CACHE")
430+
431+
ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do
432+
path = assert_deprecated(ActiveRecord.deprecator) do
433+
ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary", schema_cache_path: "tmp/something.yml")
434+
end
356435
assert_equal "tmp/something.yml", path
357436
end
358437
ensure

0 commit comments

Comments
 (0)