Skip to content

Commit

Permalink
Add ActiveRecord.deprecator
Browse files Browse the repository at this point in the history
This commit adds `ActiveRecord.deprecator` and replaces all usages of
`ActiveSupport::Deprecation.warn` in `activerecord/lib` with
`ActiveRecord.deprecator`.

Additionally, this commit adds `ActiveRecord.deprecator` to
`Rails.application.deprecators` so that it can be configured using e.g.
`config.active_support.report_deprecations`.
  • Loading branch information
jonathanhefner committed Oct 25, 2022
1 parent b4344c8 commit 682353e
Show file tree
Hide file tree
Showing 33 changed files with 72 additions and 48 deletions.
5 changes: 3 additions & 2 deletions activerecord/lib/active_record.rb
Expand Up @@ -30,6 +30,7 @@
require "yaml"

require "active_record/version"
require "active_record/deprecator"
require "active_model/attribute_set"
require "active_record/errors"

Expand Down Expand Up @@ -337,14 +338,14 @@ def self.global_executor_concurrency # :nodoc:
self.dump_schemas = :schema_search_path

def self.suppress_multiple_database_warning
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
config.active_record.suppress_multiple_database_warning is deprecated and will be removed in Rails 7.2.
It no longer has any effect and should be removed from the configuration file.
MSG
end

def self.suppress_multiple_database_warning=(value)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
config.active_record.suppress_multiple_database_warning= is deprecated and will be removed in Rails 7.2.
It no longer has any effect and should be removed from the configuration file.
MSG
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/attribute_methods/dirty.rb
Expand Up @@ -29,23 +29,23 @@ module Dirty

module ClassMethods
def partial_writes
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
ActiveRecord::Base.partial_writes is deprecated and will be removed in Rails 7.1.
Use `partial_updates` and `partial_inserts` instead.
MSG
partial_updates && partial_inserts
end

def partial_writes?
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
`ActiveRecord::Base.partial_writes?` is deprecated and will be removed in Rails 7.1.
Use `partial_updates?` and `partial_inserts?` instead.
MSG
partial_updates? && partial_inserts?
end

def partial_writes=(value)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
`ActiveRecord::Base.partial_writes=` is deprecated and will be removed in Rails 7.1.
Use `partial_updates=` and `partial_inserts=` instead.
MSG
Expand Down
Expand Up @@ -93,7 +93,7 @@ def connection_pool_names # :nodoc:
end

def all_connection_pools
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
The `all_connection_pools` method is deprecated in favor of `connection_pool_list`.
Call `connection_pool_list(:all)` to get the same behavior as `all_connection_pools`.
MSG
Expand Down Expand Up @@ -288,7 +288,7 @@ def deprecation_for_pool_handling(method)
end

if roles.flatten.uniq.count > 1
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
`#{method}` currently only applies to connection pools in the current
role (`#{ActiveRecord::Base.current_role}`). In Rails 7.1, this method
will apply to all known pools, regardless of role. To affect only those
Expand Down
Expand Up @@ -182,7 +182,7 @@ def connection_class # :nodoc:
pool_config.connection_class
end
alias :connection_klass :connection_class
deprecate :connection_klass
deprecate :connection_klass, deprecator: ActiveRecord.deprecator

# Returns true if there is an open connection being used for the current thread.
#
Expand Down
Expand Up @@ -221,7 +221,7 @@ def lookup_cast_type(sql_type)
end

def warn_quote_duration_deprecated
ActiveSupport::Deprecation.warn(<<~MSG)
ActiveRecord.deprecator.warn(<<~MSG)
Using ActiveSupport::Duration as an interpolated bind parameter in a SQL
string template is deprecated. To avoid this warning, you should explicitly
convert the duration to a more specific database type. For example, if you
Expand Down
Expand Up @@ -487,7 +487,7 @@ def within_new_transaction(isolation: nil, joinable: true)
if !completed && transaction.written_indirectly
# This is the case that was missed in the 6.1 deprecation, so we have to
# do it now
ActiveSupport::Deprecation.warn(<<~EOW)
ActiveRecord.deprecator.warn(<<~EOW)
Using `return`, `break` or `throw` to exit a transaction block is
deprecated without replacement. If the `throw` came from
`Timeout.timeout(duration)`, pass an exception class as a second
Expand Down
Expand Up @@ -172,7 +172,7 @@ def translate_exception(exception, message:, sql:, binds:)
end

def default_prepared_statements
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
The default value of `prepared_statements` for the mysql2 adapter will be changed from +false+ to +true+ in Rails 7.2.
MSG
false
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/connection_handling.rb
Expand Up @@ -330,7 +330,7 @@ def flush_idle_connections!(role = nil)

private
def deprecation_for_delegation(method)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
Calling `ActiveRecord::Base.#{method} is deprecated. Please
call the method directly on the connection handler; for
example: `ActiveRecord::Base.connection_handler.#{method}`.
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/core.rb
Expand Up @@ -309,15 +309,15 @@ def find_by!(*args) # :nodoc:
).each do |attr|
module_eval(<<~RUBY, __FILE__, __LINE__ + 1)
def #{attr}
ActiveSupport::Deprecation.warn(<<~MSG)
ActiveRecord.deprecator.warn(<<~MSG)
ActiveRecord::Base.#{attr} is deprecated and will be removed in Rails 7.1.
Use `ActiveRecord.#{attr}` instead.
MSG
ActiveRecord.#{attr}
end
def #{attr}=(value)
ActiveSupport::Deprecation.warn(<<~MSG)
ActiveRecord.deprecator.warn(<<~MSG)
ActiveRecord::Base.#{attr}= is deprecated and will be removed in Rails 7.1.
Use `ActiveRecord.#{attr}=` instead.
MSG
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/database_configurations.rb
Expand Up @@ -45,7 +45,7 @@ def initialize(configurations = {})
def configs_for(env_name: nil, name: nil, include_replicas: false, include_hidden: false)
if include_replicas
include_hidden = include_replicas
ActiveSupport::Deprecation.warn("The kwarg `include_replicas` is deprecated in favor of `include_hidden`. When `include_hidden` is passed, configurations with `replica: true` or `database_tasks: false` will be returned. `include_replicas` will be removed in Rails 7.1.")
ActiveRecord.deprecator.warn("The kwarg `include_replicas` is deprecated in favor of `include_hidden`. When `include_hidden` is passed, configurations with `replica: true` or `database_tasks: false` will be returned. `include_replicas` will be removed in Rails 7.1.")
end

env_name ||= default_env if name
Expand Down
7 changes: 7 additions & 0 deletions activerecord/lib/active_record/deprecator.rb
@@ -0,0 +1,7 @@
# frozen_string_literal: true

module ActiveRecord
def self.deprecator # :nodoc:
@deprecator ||= ActiveSupport::Deprecation.new
end
end
3 changes: 2 additions & 1 deletion activerecord/lib/active_record/errors.rb
Expand Up @@ -12,7 +12,8 @@ class ActiveRecordError < StandardError
include ActiveSupport::Deprecation::DeprecatedConstantAccessor
DeprecatedActiveJobRequiredError = Class.new(ActiveRecordError) # :nodoc:
deprecate_constant "ActiveJobRequiredError", "ActiveRecord::DeprecatedActiveJobRequiredError",
message: "ActiveRecord::ActiveJobRequiredError has been deprecated. If Active Job is not present, a NameError will be raised instead."
message: "ActiveRecord::ActiveJobRequiredError has been deprecated. If Active Job is not present, a NameError will be raised instead.",
deprecator: ActiveRecord.deprecator

# Raised when the single-table inheritance mechanism fails to locate the subclass
# (for example due to improper usage of column that
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/log_subscriber.rb
Expand Up @@ -7,21 +7,21 @@ class LogSubscriber < ActiveSupport::LogSubscriber
class_attribute :backtrace_cleaner, default: ActiveSupport::BacktraceCleaner.new

def self.runtime=(value)
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
ActiveRecord::LogSubscriber.runtime= is deprecated and will be removed in Rails 7.2.
MSG
ActiveRecord::RuntimeRegistry.sql_runtime = value
end

def self.runtime
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
ActiveRecord::LogSubscriber.runtime is deprecated and will be removed in Rails 7.2.
MSG
ActiveRecord::RuntimeRegistry.sql_runtime
end

def self.reset_runtime
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
ActiveRecord::LogSubscriber.reset_runtime is deprecated and will be removed in Rails 7.2.
MSG
ActiveRecord::RuntimeRegistry.reset
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -1097,7 +1097,7 @@ class MigrationContext

def initialize(migrations_paths, schema_migration = nil, internal_metadata = nil)
if schema_migration == SchemaMigration
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
SchemaMigration no longer inherits from ActiveRecord::Base. If you want
to use the default connection, remove this argument. If you want to use a
specific connection, instaniate MigrationContext with the connection's schema
Expand All @@ -1108,7 +1108,7 @@ def initialize(migrations_paths, schema_migration = nil, internal_metadata = nil
end

if internal_metadata == InternalMetadata
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
SchemaMigration no longer inherits from ActiveRecord::Base. If you want
to use the default connection, remove this argument. If you want to use a
specific connection, instaniate MigrationContext with the connection's internal
Expand Down
4 changes: 4 additions & 0 deletions activerecord/lib/active_record/railtie.rb
Expand Up @@ -73,6 +73,10 @@ class Railtie < Rails::Railtie # :nodoc:
require "active_record/base"
end

initializer "active_record.deprecator" do |app|
app.deprecators[:active_record] = ActiveRecord.deprecator
end

initializer "active_record.initialize_timezone" do
ActiveSupport.on_load(:active_record) do
self.time_zone_aware_attributes = true
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -151,7 +151,7 @@ def sum(identity_or_column = nil, &block)
end

if identity_or_column.nil?
ActiveSupport::Deprecation.warn(<<-MSG.squish)
ActiveRecord.deprecator.warn(<<-MSG.squish)
Rails 7.0 has deprecated Enumerable.sum in favor of Ruby's native implementation available since 2.4.
Sum of non-numeric elements requires an initial argument.
MSG
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/table_metadata.rb
Expand Up @@ -26,7 +26,7 @@ def associated_with?(table_name)
if reflection = klass&._reflect_on_association(table_name)
reflection
elsif ActiveRecord.allow_deprecated_singular_associations_name && reflection = klass&._reflect_on_association(table_name.singularize)
ActiveSupport::Deprecation.warn(<<~MSG)
ActiveRecord.deprecator.warn(<<~MSG)
Referring to a singular association (e.g. `#{reflection.name}`) by its plural name (e.g. `#{reflection.plural_name}`) is deprecated.
To convert this deprecation warning to an error and enable more performant behavior, set config.active_record.allow_deprecated_singular_associations_name = false.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/tasks/database_tasks.rb
Expand Up @@ -443,7 +443,7 @@ def schema_file_type(format = ActiveRecord.schema_format)
"structure.sql"
end
end
deprecate :schema_file_type
deprecate :schema_file_type, deprecator: ActiveRecord.deprecator

def schema_dump_path(db_config, format = ActiveRecord.schema_format)
return ENV["SCHEMA"] if ENV["SCHEMA"]
Expand Down
Expand Up @@ -40,7 +40,7 @@ class DestroyAssociationAsyncJobTest < ActiveRecord::TestCase
end

test "deprecation of rescuing ActiveJobRequiredError which has been replaced by a NameError" do
assert_deprecated { ActiveRecord::ActiveJobRequiredError }
assert_deprecated(ActiveRecord.deprecator) { ActiveRecord::ActiveJobRequiredError }
end

test "belong_to dependent destroy_async requires destroy_association_async_job" do
Expand Down
Expand Up @@ -79,7 +79,7 @@ def test_where_with_rational_for_string_column_using_bind_parameters
end

def test_where_with_duration_for_string_column_using_bind_parameters
count = assert_deprecated { Post.where("title = ?", 0.seconds).count }
count = assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
assert_equal 0, count
end
end
Expand Down
Expand Up @@ -52,7 +52,7 @@ def close
end
end.new

assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
ActiveRecord::ConnectionAdapters::Mysql2Adapter.new(
fake_connection,
ActiveRecord::Base.logger,
Expand Down Expand Up @@ -84,7 +84,7 @@ def close
end
end.new

adapter = ActiveSupport::Deprecation.silence do
adapter = ActiveRecord.deprecator.silence do
ActiveRecord::ConnectionAdapters::Mysql2Adapter.new(
fake_connection,
ActiveRecord::Base.logger,
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/adapters/mysql2/quoting_test.rb
Expand Up @@ -21,7 +21,7 @@ def test_quote_bound_rational
end

def test_quote_bound_duration
expected = assert_deprecated { @conn.quote_bound_value(42.seconds) }
expected = assert_deprecated(ActiveRecord.deprecator) { @conn.quote_bound_value(42.seconds) }
assert_equal "'42'", expected
end

Expand Down
Expand Up @@ -46,7 +46,7 @@ def test_where_with_rational_for_string_column_using_bind_parameters

def test_where_with_duration_for_string_column_using_bind_parameters
assert_raises ActiveRecord::StatementInvalid do
assert_deprecated { Post.where("title = ?", 0.seconds).count }
assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
end
end
end
Expand Down
Expand Up @@ -40,7 +40,7 @@ def test_where_with_rational_for_string_column_using_bind_parameters
end

def test_where_with_duration_for_string_column_using_bind_parameters
count = assert_deprecated { Post.where("title = ?", 0.seconds).count }
count = assert_deprecated(ActiveRecord.deprecator) { Post.where("title = ?", 0.seconds).count }
assert_equal 0, count
end
end
Expand Down
Expand Up @@ -764,12 +764,12 @@ def test_push_with_invalid_join_record
firm = companies(:first_firm)
lifo = Developer.new(name: "lifo")
assert_raises(ActiveRecord::RecordInvalid) do
assert_deprecated { firm.developers << lifo }
assert_deprecated(ActiveRecord.deprecator) { firm.developers << lifo }
end

lifo = Developer.create!(name: "lifo")
assert_raises(ActiveRecord::RecordInvalid) do
assert_deprecated { firm.developers << lifo }
assert_deprecated(ActiveRecord.deprecator) { firm.developers << lifo }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/calculations_test.rb
Expand Up @@ -525,7 +525,7 @@ def test_should_not_overshadow_enumerable_sum
assert_equal 6, [1, 2, 3].sum(&:abs)
assert_equal 15, some_companies.sum(&:id)
assert_equal 25, some_companies.sum(10, &:id)
assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
assert_equal "LeetsoftJadedpixel", some_companies.sum(&:name)
end
assert_equal "companies: LeetsoftJadedpixel", some_companies.sum("companies: ", &:name)
Expand Down
Expand Up @@ -323,13 +323,13 @@ def test_connection_pool_list
assert_equal([@rw_pool], @handler.connection_pool_list(:writing))
assert_equal([@ro_pool], @handler.connection_pool_list(:reading))

assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
@handler.connection_pool_list
end
end

def test_all_connection_pools
assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
assert_equal([@rw_pool, @ro_pool], @handler.all_connection_pools)
end
end
Expand All @@ -340,20 +340,20 @@ def test_retrieve_connection
end

def test_active_connections?
assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
assert_not_predicate @handler, :active_connections?
end

assert @handler.retrieve_connection(@connection_name)
assert @handler.retrieve_connection(@connection_name, role: :reading)

assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
assert_predicate @handler, :active_connections?
end

@handler.clear_active_connections!(:all)

assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
assert_not_predicate @handler, :active_connections?
end
end
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/database_configurations_test.rb
Expand Up @@ -111,7 +111,7 @@ def test_hidden_returns_replicas
end

def test_include_replicas_is_deprecated
assert_deprecated do
assert_deprecated(ActiveRecord.deprecator) do
db_config = ActiveRecord::Base.configurations.configs_for(env_name: "arunit", name: "primary", include_replicas: true)

assert_equal "primary", db_config.name
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/helper.rb
Expand Up @@ -19,7 +19,7 @@
Thread.abort_on_exception = true

# Show backtraces for deprecated behavior for quicker cleanup.
ActiveSupport::Deprecation.debug = true
ActiveRecord.deprecator.debug = true

# Disable available locale checks to avoid warnings running the test suite.
I18n.enforce_available_locales = false
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/migration_test.rb
Expand Up @@ -81,7 +81,7 @@ def setup

def test_passing_a_schema_migration_class_to_migration_context_is_deprecated
migrations_path = MIGRATIONS_ROOT + "/valid"
migrator = assert_deprecated { ActiveRecord::MigrationContext.new(migrations_path, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata) }
migrator = assert_deprecated(ActiveRecord.deprecator) { ActiveRecord::MigrationContext.new(migrations_path, ActiveRecord::SchemaMigration, ActiveRecord::InternalMetadata) }
migrator.up

assert_equal 3, migrator.current_version
Expand Down

0 comments on commit 682353e

Please sign in to comment.