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

1320: fix deletion of reused files #1696

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kkoehn
Copy link
Contributor

@kkoehn kkoehn commented Oct 2, 2024

unsolved problem of deleting reused files, when parent gets deleted
This PR continues to solve a problem that occurred when file referenced changed for reimported tasks (file belonged to a test before, but now it belongs to a model_solution).
The particular problem here was that the test referenced before got deleted. Even when first moving the file and the deleting the test, the file got deleted.

see problem description in #1320

@kkoehn kkoehn requested a review from MrSerth October 2, 2024 22:05
@kkoehn kkoehn self-assigned this Oct 2, 2024
@kkoehn kkoehn linked an issue Oct 2, 2024 that may be closed by this pull request
18 tasks
@kkoehn kkoehn changed the title 1320: add example tests for file overwriting 1320: fix deletion of reused files Oct 2, 2024
Copy link

codecov bot commented Oct 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.87%. Comparing base (e1b9021) to head (cc487bd).
Report is 4 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1696      +/-   ##
==========================================
+ Coverage   94.79%   94.87%   +0.07%     
==========================================
  Files         133      133              
  Lines        3345     3374      +29     
==========================================
+ Hits         3171     3201      +30     
+ Misses        174      173       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@MrSerth
Copy link
Member

MrSerth commented Oct 9, 2024

I've spent further time debugging and looking for a solution. Finally, I wasn't able to do so with the current architecture and, hence, need to suggest a major architectural change:

Could you try to perform the necessary changes directly by updating and saving the changes (either with the @task.update method or by first assigning new values and then using @task.save)?

Of course, this means the service will return an altered object, persisted object already and not a modified version (that needs to be save). However, the big advantage is that we can more fine granular control which object gets saved how and don't need to wrap around the Rails magic.

Then, you should wrap the execute block into a database transaction. This allows you to perform the changes (i.e., on files) and they "look" persisted in this transaction even if they are not. You may then also use something like @task.reload to get an updated version of the record and move over to the next parts (i.e., tests, model solutions). If you only want to reload associations, you can do so with @task.association(:tests).reload (to force, use @task.association(:tests).reload(true)).

I am rather confident that we will be able to navigate around the current issue with this approach, even if it changes the API. Hence, I would suggest to follow with this approach.

@kkoehn kkoehn force-pushed the 1320-fix-destroying-files-while-overwriting branch 6 times, most recently from 8fcdde2 to 34309fd Compare January 15, 2025 19:37
@kkoehn kkoehn force-pushed the 1320-fix-destroying-files-while-overwriting branch from 34309fd to cc487bd Compare January 15, 2025 21:20
@kkoehn kkoehn marked this pull request as ready for review January 15, 2025 21:20
@kkoehn kkoehn added bug ruby Pull requests that update Ruby code labels Jan 15, 2025
@kkoehn kkoehn requested a review from Dome-GER January 15, 2025 21:31
Copy link
Member

@MrSerth MrSerth left a comment

Choose a reason for hiding this comment

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

In an effort to ease domain-specific review, I have executed the code locally and performed my well-known test with the previously-shared set of new and old exercise files (for ID 845). It worked flawlessly and as expected, thank you.

Still, I identified an unnecessary N+1 query caused by the autosave option. It might no longer be necessary (at least in my tests, everything worked without the option, too). Hence, we might be able to drop it:

Subject: [PATCH] Remove autosave causing unnecessary N+1 queries
---
Index: app/models/task_file.rb
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/models/task_file.rb b/app/models/task_file.rb
--- a/app/models/task_file.rb	(revision cc487bdb571f5ce4cda4cf42132a76f85e72ef11)
+++ b/app/models/task_file.rb	(date 1737201720489)
@@ -4,7 +4,7 @@
   include ParentValidation
   include TransferValues
 
-  belongs_to :fileable, polymorphic: true, autosave: true, inverse_of: :files
+  belongs_to :fileable, polymorphic: true, inverse_of: :files
   belongs_to :parent, class_name: 'TaskFile', optional: true
   has_one_attached :attachment
   validates :name, presence: true
Index: app/models/model_solution.rb
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/models/model_solution.rb b/app/models/model_solution.rb
--- a/app/models/model_solution.rb	(revision cc487bdb571f5ce4cda4cf42132a76f85e72ef11)
+++ b/app/models/model_solution.rb	(date 1737201720486)
@@ -5,7 +5,7 @@
   include TransferValues
   include ParentValidation
 
-  belongs_to :task, autosave: true, inverse_of: :model_solutions
+  belongs_to :task, inverse_of: :model_solutions
   belongs_to :parent, class_name: 'ModelSolution', optional: true
   has_many :files, as: :fileable, class_name: 'TaskFile', dependent: :destroy
   accepts_nested_attributes_for :files, allow_destroy: true
Index: app/models/test.rb
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/models/test.rb b/app/models/test.rb
--- a/app/models/test.rb	(revision cc487bdb571f5ce4cda4cf42132a76f85e72ef11)
+++ b/app/models/test.rb	(date 1737201720494)
@@ -5,7 +5,7 @@
   include TransferValues
   include ParentValidation
 
-  belongs_to :task, autosave: true, inverse_of: :tests
+  belongs_to :task, inverse_of: :tests
   belongs_to :testing_framework, optional: true
   belongs_to :parent, class_name: 'Test', optional: true
   validates :title, presence: true
Index: app/models/concerns/file_concern.rb
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/app/models/concerns/file_concern.rb b/app/models/concerns/file_concern.rb
--- a/app/models/concerns/file_concern.rb	(revision cc487bdb571f5ce4cda4cf42132a76f85e72ef11)
+++ b/app/models/concerns/file_concern.rb	(date 1737201720483)
@@ -4,7 +4,7 @@
   extend ActiveSupport::Concern
 
   included do
-    has_many :files, as: :fileable, class_name: 'TaskFile', dependent: :destroy, autosave: true, inverse_of: :fileable
+    has_many :files, as: :fileable, class_name: 'TaskFile', dependent: :destroy, inverse_of: :fileable
     accepts_nested_attributes_for :files, allow_destroy: true
   end

Please keep in mind that I haven't reviewed the code 😇.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug ruby Pull requests that update Ruby code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check task overwriting through ProFormA
2 participants