Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

perform deletion of associated FileLinks on ProjectStorage deletion in DeleteService instead of model callback

Christophe Bliard9441fa907cd9
diff --git a/modules/storages/app/models/storages/project_storage.rb b/modules/storages/app/models/storages/project_storage.rb
index d16b8909d6b..c6a735f47e5 100644
--- a/modules/storages/app/models/storages/project_storage.rb
+++ b/modules/storages/app/models/storages/project_storage.rb
@@ -42,22 +42,4 @@ class Storages::ProjectStorage < ApplicationRecord
# There should be only one ProjectStorage per project and storage.
validates :project, uniqueness: { scope: :storage }
-
- # Use a callback to delete dependent FileLinks.
- before_destroy :delete_associated_file_links
-
- private
-
- # Delete FileLinks with the same Storage as the ProjectStorage.
- # Also, they are attached to WorkPackages via the Project.
- def delete_associated_file_links
- # work_packages is an ActiveRecord::Relation, not an array of objects!
- work_packages = WorkPackage.where(project_id: project_id)
- file_links = Storages::FileLink.where(storage_id: storage_id, container: work_packages)
- # use file_links.to_sql to check the SQL generated by the lines above.
- # It uses a fast SQL "container_id in (select * from work_packages...)", so the
- # delete_all below is executed using a single query.
- # Reference: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html
- file_links.delete_all
- end
end
diff --git a/modules/storages/app/services/storages/project_storages/delete_service.rb b/modules/storages/app/services/storages/project_storages/delete_service.rb
index 19625639897..9dfc77bc82e 100644
--- a/modules/storages/app/services/storages/project_storages/delete_service.rb
+++ b/modules/storages/app/services/storages/project_storages/delete_service.rb
@@ -30,5 +30,32 @@ module Storages::ProjectStorages
# Performs the deletion in the superclass. Associated FileLinks are deleted
# by the model before_destroy hook.
class DeleteService < ::BaseServices::Delete
+ # "persist" is a callback from BaseContracted.perform
+ # that is supposed to do the actual work in a contract.
+ # So in a DeleteService it performs the actual delete,
+ # except for the @object.destroy that is already performed
+ # by ::BaseServices::Delete
+ def persist(service_result)
+ # Perform the @object.destroy etc. in the super-class
+ super(service_result).tap do |deletion_result|
+ delete_associated_file_links if deletion_result.success?
+ end
+ end
+
+ private
+
+ # Delete FileLinks with the same Storage as the ProjectStorage.
+ # Also, they are attached to WorkPackages via the Project.
+ def delete_associated_file_links
+ # work_packages is an ActiveRecord::Relation, not an array of objects!
+ work_packages = WorkPackage.where(project_id: model.project_id)
+ file_links = Storages::FileLink.where(storage_id: model.storage_id,
+ container: work_packages)
+ # use file_links.to_sql to check the SQL generated by the lines above.
+ # It uses a fast SQL "container_id in (select * from work_packages...)", so the
+ # delete_all below is executed using a single query.
+ # Reference: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html
+ file_links.delete_all
+ end
end
end
diff --git a/modules/storages/spec/models/project_storage_spec.rb b/modules/storages/spec/models/project_storage_spec.rb
index d6301ebddf3..70bf8d2a849 100644
--- a/modules/storages/spec/models/project_storage_spec.rb
+++ b/modules/storages/spec/models/project_storage_spec.rb
@@ -71,9 +71,9 @@ describe ::Storages::ProjectStorage, type: :model do
project_storage_to_destroy.destroy
end
- it "destroys all associated FileLink records" do
- expect(Storages::ProjectStorage.count).to be 0
- expect(Storages::FileLink.count).to be 0
+ it "does not destroy associated FileLink records" do
+ expect(Storages::ProjectStorage.count).to eq 0
+ expect(Storages::FileLink.count).not_to eq 0
end
end
end