From f90e16fc68b753cd74f60417f76f7ea273593c25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 9 Feb 2024 20:28:34 +0000 Subject: [PATCH] Deprecated `ENV["SCHEMA_CACHE"]` in favor of `schema_cache_path` in the databse configuration The config in the yaml allows for more complex configuration. --- activerecord/CHANGELOG.md | 4 ++++ .../lib/active_record/tasks/database_tasks.rb | 15 +++++++++++++-- .../test/cases/tasks/database_tasks_test.rb | 8 +++++--- railties/test/application/rake/dbs_test.rb | 11 ----------- 4 files changed, 22 insertions(+), 16 deletions(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 33a6ed99f59cc..78a778feb49b7 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Deprecated `ENV["SCHEMA_CACHE"]` in favor of `schema_cache_path` in the databse configuration. + + *Rafael Mendonça França* + * Add `active_record.config.validate_migration_timestamps` option for validating migration timestamps. When set, validates that the timestamp prefix for a migration is no more than a day ahead of diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index efd154abab2ca..1dc08990d1e3d 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -441,7 +441,7 @@ def cache_dump_filename(db_config_or_name, schema_cache_path: nil) if db_config_or_name.is_a?(DatabaseConfigurations::DatabaseConfig) schema_cache_path || db_config_or_name.schema_cache_path || - ENV["SCHEMA_CACHE"] || + schema_cache_env || db_config_or_name.default_schema_cache_path(ActiveRecord::Tasks::DatabaseTasks.db_dir) else ActiveRecord.deprecator.warn(<<~MSG.squish) @@ -455,7 +455,7 @@ def cache_dump_filename(db_config_or_name, schema_cache_path: nil) "#{db_config_or_name}_schema_cache.yml" end - schema_cache_path || ENV["SCHEMA_CACHE"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename) + schema_cache_path || schema_cache_env || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, filename) end end @@ -523,6 +523,17 @@ def migration_connection # :nodoc: end private + def schema_cache_env + if ENV["SCHEMA_CACHE"] + ActiveRecord.deprecator.warn(<<~MSG.squish) + Setting `ENV["SCHEMA_CACHE"]` is deprecated and will be removed in Rails 7.3. + Configure the `:schema_cache_path` in the database configuration instead. + MSG + + nil + end + end + def with_temporary_pool(db_config, clobber: false) original_db_config = migration_class.connection_db_config pool = migration_class.connection_handler.establish_connection(db_config, clobber: clobber) diff --git a/activerecord/test/cases/tasks/database_tasks_test.rb b/activerecord/test/cases/tasks/database_tasks_test.rb index 70cb0b1e86e7b..d05dd08a447e8 100644 --- a/activerecord/test/cases/tasks/database_tasks_test.rb +++ b/activerecord/test/cases/tasks/database_tasks_test.rb @@ -388,8 +388,10 @@ def test_cache_dump_filename_with_env_override config = DatabaseConfigurations::HashConfig.new("development", "primary", {}) ActiveRecord::Tasks::DatabaseTasks.stub(:db_dir, "db") do - path = ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config) - assert_equal "tmp/something.yml", path + path = assert_deprecated(/Setting `ENV\["SCHEMA_CACHE"\]` is deprecated and will be removed in Rails 7\.3\. Configure the `:schema_cache_path` in the database configuration instead\. \(/, ActiveRecord.deprecator) do + ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename(config) + end + assert_equal "db/schema_cache.yml", path end ensure ENV["SCHEMA_CACHE"] = old_path @@ -403,7 +405,7 @@ def test_deprecated_cache_dump_filename_with_env_override path = assert_deprecated(/Passing a database name to `cache_dump_filename` is deprecated and will be removed in Rails 7\.3\. Pass a `ActiveRecord::DatabaseConfigurations::DatabaseConfig` object instead\. \(/, ActiveRecord.deprecator) do ActiveRecord::Tasks::DatabaseTasks.cache_dump_filename("primary") end - assert_equal "tmp/something.yml", path + assert_equal "db/schema_cache.yml", path end ensure ENV["SCHEMA_CACHE"] = old_path diff --git a/railties/test/application/rake/dbs_test.rb b/railties/test/application/rake/dbs_test.rb index 30cf17b028e4b..4b24d186a3326 100644 --- a/railties/test/application/rake/dbs_test.rb +++ b/railties/test/application/rake/dbs_test.rb @@ -439,17 +439,6 @@ def db_schema_cache_dump db_schema_cache_dump end - test "db:schema:cache:dump custom env" do - @old_schema_cache_env = ENV["SCHEMA_CACHE"] - filename = "db/special_schema_cache.yml" - ENV["SCHEMA_CACHE"] = filename - - db_schema_dump - db_schema_cache_dump - ensure - ENV["SCHEMA_CACHE"] = @old_schema_cache_env - end - test "db:schema:cache:dump first config wins" do Dir.chdir(app_path) do File.open("#{app_path}/config/database.yml", "w") do |f|