Skip to content

Commit

Permalink
Deprecated ENV["SCHEMA_CACHE"] in favor of schema_cache_path in t…
Browse files Browse the repository at this point in the history
…he databse configuration

The config in the yaml allows for more complex configuration.
  • Loading branch information
rafaelfranca committed Feb 9, 2024
1 parent f09f21e commit f90e16f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
4 changes: 4 additions & 0 deletions 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
Expand Down
15 changes: 13 additions & 2 deletions activerecord/lib/active_record/tasks/database_tasks.rb
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions activerecord/test/cases/tasks/database_tasks_test.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 0 additions & 11 deletions railties/test/application/rake/dbs_test.rb
Expand Up @@ -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|
Expand Down

0 comments on commit f90e16f

Please sign in to comment.