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

Support dependent: :destroy #508

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/paranoia.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def timestamp_attributes_with_current_time
# we called #destroy
def restore_associated_records(recovery_window_range = nil)
destroyed_associations = self.class.reflect_on_all_associations.select do |association|
association.options[:dependent] == :destroy
association.options[:dependent] == :destroy || association.options[:dependent] == :destroy_async
end

destroyed_associations.each do |association|
Expand Down
43 changes: 43 additions & 0 deletions test/paranoia_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,24 @@
ActiveRecord::Base.raise_in_transactional_callbacks = true
end

if ActiveRecord::Base.respond_to?(:destroy_association_async_job=)

class MockDestroyAssociationAsyncJob
def self.perform_later(
owner_model_name: nil, owner_id: nil,
association_class: nil, association_ids: nil, association_primary_key_column: nil,
ensuring_owner_was_method: nil
)
association_model = association_class.constantize
association_model.where(association_primary_key_column => association_ids).find_each do |r|
r.destroy
end
end
end

ActiveRecord::Base.destroy_association_async_job = MockDestroyAssociationAsyncJob
end

def connect!
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', database: ':memory:'
end
Expand Down Expand Up @@ -1070,6 +1088,21 @@ def test_counter_cache_column_on_restore
end
end

if ActiveRecord::VERSION::STRING >= '6.1'
def test_restore_on_has_many_dependent_destroy_async_association
parent = ParentModelWithDependentDestroyAsync.create
child = ParanoidModelWithDependentDestroyAsync.create(parent_model: parent)

parent.destroy

assert_equal 0, child.class.count

parent.restore(recursive: true)

assert_equal 1, child.class.count
end
end

private
def get_featureful_model
FeaturefulModel.new(:name => "not empty")
Expand Down Expand Up @@ -1386,3 +1419,13 @@ class ParanoidBelongsTo < ActiveRecord::Base
belongs_to :paranoid_has_one
end
end

if ActiveRecord::VERSION::STRING >= '6.1'
class ParentModelWithDependentDestroyAsync < ParentModel
has_many :paranoid_model_with_dependent_destroy_asyncs, dependent: :destroy_async, foreign_key: 'parent_model_id'
end

class ParanoidModelWithDependentDestroyAsync < ParanoidModel
belongs_to :paranoid_model_with_dependent_destroy_asyncs, foreign_key: 'parent_model_id'
end
end