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

Fix cascade delete of submission step #2302

Merged

Conversation

sergei-maertens
Copy link
Member

@sergei-maertens sergei-maertens commented Nov 3, 2022

Fixes #2135

This is a bandaid with some nasty code, we definitely need to discuss this properly what it should look like for 2.0. (see #2305)

The gist is:

  • the SubmissionStep.form_step FK is now nullable
  • if the formstep would have cascade deleted the submission step, then we serialize the form step + definition (using django's serializers that power fixture dumping) and record it on the SubmissionStep instance
  • When the FK is empty, we then load the historical data from the record history (essentially load the fixture in memory)

⚠️ This will not survive the test of time - recorded fixtures cannot anticipate database schema changes, so it won't be guaranteed forwards compatible.

⚠️ This abuses Django's deletion collector API (which is private afaik) to schedule the updates on delete in a reliable manner. We have to use frozenset because of internal implementation details and I don't suspect this will be fixed in Django for this use case.

@sergei-maertens sergei-maertens force-pushed the hotfix/2135-fix-cascade-delete-submission-step branch from 0d8c6fc to 244a2d5 Compare November 3, 2022 16:30
Django requires hashable datatypes in the collector, which list and dict
aren't.
And record the formstep data on the submissionstep so that we can
fix the display from local data rather than related FK data.
Fixed select_for_update in Submission.remove_sensitive_data
@sergei-maertens sergei-maertens force-pushed the hotfix/2135-fix-cascade-delete-submission-step branch from 244a2d5 to 32a5570 Compare November 4, 2022 08:07
@sergei-maertens sergei-maertens marked this pull request as ready for review November 4, 2022 08:08
Copy link
Contributor

@SilviaAmAm SilviaAmAm left a comment

Choose a reason for hiding this comment

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

If I understand correctly, loading the form step from the history on the submission step is only needed if the form is restored from an older version while a user is filling in the form?
For all other purposes, if the submission step has no form_step FK it is not a problem?



# Note that this exists because of data loss bug #2135 - when a FormStep is being
# deleted in the form designer (or during import/export), we do not want to destroy
Copy link
Contributor

@SilviaAmAm SilviaAmAm Nov 4, 2022

Choose a reason for hiding this comment

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

Does this also happen in import export (I thought it was only deleting the form step or restoring a form from an older version)?
During the export all the form resources are turned to JSON (i don't think anything is being deleted?) and during the import, the form should be new, so there is no submission step data that could be associated with it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Does this also happen in import export (I thought it was only deleting the form step or restoring a form from an older version)? During the export all the form resources are turned to JSON (i don't think anything is being deleted?) and during the import, the form should be new, so there is no submission step data that could be associated with it?

I'll have to double check that for exactly which mechanism is in play, but it might be that you're importing over an existing form and updating it that way? The original issue reported it being a problem with imports, though I was able to reproduce with a simplified case of deleting the form step.

Copy link
Member Author

Choose a reason for hiding this comment

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

so, when restoring old versions of a form, this flow is hit - there is an existing form instance, which makes you hit this codepath (via https://github.com/open-formulieren/open-forms/blob/master/src/openforms/forms/models/form.py#L434):

https://github.com/open-formulieren/open-forms/blob/stable/1.1.x/src/openforms/forms/utils.py#L126

I'll add a testcase that essentially operates on Form.restore_old_version to ensure this is also covered by the patch.

@sergei-maertens
Copy link
Member Author

sergei-maertens commented Nov 4, 2022

If I understand correctly, loading the form step from the history on the submission step is only needed if the form is restored from an older version while a user is filling in the form? For all other purposes, if the submission step has no form_step FK it is not a problem?

Correct!

edit: no, this applies basically as soon as there is a submission, but you'll typically notice it when looking at completed submissions

@sergei-maertens sergei-maertens force-pushed the hotfix/2135-fix-cascade-delete-submission-step branch from 95ae8f4 to 7d1eb9f Compare November 4, 2022 14:59
@codecov-commenter
Copy link

Codecov Report

Base: 92.45% // Head: 92.45% // Decreases project coverage by -0.00% ⚠️

Coverage data is based on head (7d1eb9f) compared to base (e160e7e).
Patch coverage: 91.78% of modified lines in pull request are covered.

Additional details and impacted files
@@               Coverage Diff                @@
##           stable/1.1.x    #2302      +/-   ##
================================================
- Coverage         92.45%   92.45%   -0.01%     
================================================
  Files               531      531              
  Lines             16415    16483      +68     
  Branches           1471     1491      +20     
================================================
+ Hits              15177    15239      +62     
- Misses              978      981       +3     
- Partials            260      263       +3     
Impacted Files Coverage Δ
...rc/openforms/submissions/models/submission_step.py 93.25% <90.00%> (-6.75%) ⬇️
src/openforms/submissions/models/submission.py 96.07% <100.00%> (+0.14%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@sergei-maertens sergei-maertens merged commit 14c3a24 into stable/1.1.x Nov 4, 2022
@sergei-maertens sergei-maertens deleted the hotfix/2135-fix-cascade-delete-submission-step branch November 4, 2022 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants