Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer constant loading of ActiveRecord::DestroyAssociationAsyncJob via a String instead of a class constant #45476

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions activejob/lib/active_job/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class Railtie < Rails::Railtie # :nodoc:
ActiveSupport.on_load(:action_dispatch_integration_test) do
include ActiveJob::TestHelper
end

ActiveSupport.on_load(:active_record) do
self.destroy_association_async_job ||= ActiveRecord::DestroyAssociationAsyncJob
end
end

initializer "active_job.set_reloader_hook" do |app|
Expand Down
17 changes: 15 additions & 2 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
* Allow `destroy_association_async_job=` to be configured with a class string instead of a constant.

Defers an autoloading dependency between `ActiveRecord::Base` and `ActiveJob::Base`
and moves the configuration of `ActiveRecord::DestroyAssociationAsyncJob`
from ActiveJob to ActiveRecord.

Deprecates `ActiveRecord::ActiveJobRequiredError` and now raises a `NameError`
if the job class is unloadable or an `ActiveRecord::ConfigurationError` if
`dependent: :destroy_async` is declared on an association but there is no job
class configured.

*Ben Sheldon*

* Fix `ActiveRecord::Store` to serialize as a regular Hash

Previously it would serialize as an `ActiveSupport::HashWithIndifferentAccess`
Expand All @@ -16,7 +29,7 @@
```

*Alex Ghiculescu*

* Add new `ActiveRecord::Base::generates_token_for` API.

Currently, `signed_id` fulfills the role of generating tokens for e.g.
Expand Down Expand Up @@ -49,7 +62,7 @@
user.update!(password: "new password")
User.find_by_token_for(:password_reset, token) # => nil
```

*Jonathan Hefner*

* Optimize Active Record batching for whole table iterations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def self.valid_dependent_options

def self.check_dependent_options(dependent, model)
if dependent == :destroy_async && !model.destroy_association_async_job
err_message = "ActiveJob is required to use destroy_async on associations"
raise ActiveRecord::ActiveJobRequiredError, err_message
err_message = "A valid destroy_association_async_job is required to use `dependent: :destroy_async` on associations"
raise ActiveRecord::ConfigurationError, err_message
end
unless valid_dependent_options.include? dependent
raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{dependent}"
Expand Down
20 changes: 15 additions & 5 deletions activerecord/lib/active_record/core.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "active_support/core_ext/enumerable"
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/string/filters"
require "active_support/parameter_filter"
require "concurrent/map"
Expand All @@ -19,11 +20,20 @@ module Core
# retrieved on both a class and instance level by calling +logger+.
class_attribute :logger, instance_writer: false

##
# :singleton-method:
#
# Specifies the job used to destroy associations in the background
class_attribute :destroy_association_async_job, instance_writer: false, instance_predicate: false, default: false
class_attribute :_destroy_association_async_job, instance_accessor: false, default: "ActiveRecord::DestroyAssociationAsyncJob"

# The job class used to destroy associations in the background.
def self.destroy_association_async_job
if _destroy_association_async_job.is_a?(String)
self._destroy_association_async_job = _destroy_association_async_job.constantize
end
_destroy_association_async_job
rescue NameError => error
raise NameError, "Unable to load destroy_association_async_job: #{error.message}"
end

singleton_class.alias_method :destroy_association_async_job=, :_destroy_association_async_job=
delegate :destroy_association_async_job, to: :class

##
# :singleton-method:
Expand Down
9 changes: 6 additions & 3 deletions activerecord/lib/active_record/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ module ActiveRecord
class ActiveRecordError < StandardError
end

# Raised when trying to use a feature in Active Record which requires Active Job but the gem is not present.
class ActiveJobRequiredError < ActiveRecordError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to remove this exception we need to deprecate first.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

end
# DEPRECATED: Previously raised when trying to use a feature in Active Record which
# requires Active Job but the gem is not present. Now raises a NameError.
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."

# Raised when the single-table inheritance mechanism fails to locate the subclass
# (for example due to improper usage of column that
Expand Down
66 changes: 66 additions & 0 deletions activerecord/test/activejob/destroy_association_async_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require "cases/helper"

class UndefinedConstantAsync < ActiveRecord::Base
self.destroy_association_async_job = "UndefinedConstantJob"
end

autoload :UnloadableBaseJob, "activejob/unloadable_base_job"
class UnloadableBaseAsync < ActiveRecord::Base
self.destroy_association_async_job = "UnloadableBaseJob"
end

class UnusedBelongsToAsync < ActiveRecord::Base
self.destroy_association_async_job = nil
end

class UnusedHasOneAsync < ActiveRecord::Base
self.destroy_association_async_job = nil
end

class UnusedHasManyAsync < ActiveRecord::Base
self.destroy_association_async_job = nil
end


class DestroyAssociationAsyncJobTest < ActiveRecord::TestCase
test "destroy_association_async_job requires valid job class" do
error = assert_raises NameError do
UndefinedConstantAsync.belongs_to :essay_destroy_async, dependent: :destroy_async
end
assert_match %r/destroy_association_async_job: uninitialized constant UndefinedConstantJob/, error.message
end

test "destroy_association_async_job error shows a missing parent job class, as if ActiveJob were missing" do
error = assert_raises NameError do
UnloadableBaseAsync.belongs_to :essay_destroy_async, dependent: :destroy_async
end
assert_match %r/destroy_association_async_job: uninitialized constant PretendActiveJobIsNotPresent/, error.message
end

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

test "belong_to dependent destroy_async requires destroy_association_async_job" do
error = assert_raises ActiveRecord::ConfigurationError do
UnusedBelongsToAsync.belongs_to :essay_destroy_async, dependent: :destroy_async
end
assert_match %r/destroy_association_async_job/, error.message
end

test "has_one dependent destroy_async requires destroy_association_async_job" do
error = assert_raises ActiveRecord::ConfigurationError do
UnusedHasOneAsync.has_one :essay_destroy_async, dependent: :destroy_async
end
assert_match %r/destroy_association_async_job/, error.message
end

test "has_many dependent destroy_async requires destroy_association_async_job" do
error = assert_raises ActiveRecord::ConfigurationError do
UnusedHasManyAsync.has_many :essay_destroy_asyncs, dependent: :destroy_async
end
assert_match %r/destroy_association_async_job/, error.message
end
end
31 changes: 0 additions & 31 deletions activerecord/test/activejob/destroy_async_job_not_present_test.rb

This file was deleted.

3 changes: 3 additions & 0 deletions activerecord/test/activejob/unloadable_base_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

class UnloadableBaseJob < PretendActiveJobIsNotPresent; end
4 changes: 2 additions & 2 deletions railties/test/application/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2666,12 +2666,12 @@ class ::DummySerializer < ActiveJob::Serializers::ObjectSerializer; end
assert_equal ActiveRecord::DestroyAssociationAsyncJob, ActiveRecord::Base.destroy_association_async_job
end

test "ActiveRecord::Base.destroy_association_async_job can be configured via config.active_record.destroy_association_job" do
test "ActiveRecord::Base.destroy_association_async_job can be configured via config.active_record.destroy_association_async_job" do
class ::DummyDestroyAssociationAsyncJob; end

app_file "config/environments/test.rb", <<-RUBY
Rails.application.configure do
config.active_record.destroy_association_async_job = DummyDestroyAssociationAsyncJob
config.active_record.destroy_association_async_job = "DummyDestroyAssociationAsyncJob"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! 👍

end
RUBY

Expand Down