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

Delete xform linked resources upon xform deletion #2136

Merged
merged 1 commit into from
Sep 17, 2021

Conversation

WinnyTroy
Copy link
Contributor

Changes / Features implemented

  • Clean out Merged Datasets upon xform deletion.
  • Clean out Form Media Files upon form deletion.
  • Add documentation on clean up of filtered datasets

Before submitting this PR for review, please make sure you have:

  • Updated tests

Closes #1029

Copy link
Contributor

@DavisRayM DavisRayM left a comment

Choose a reason for hiding this comment

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

Looks good just a few concerns

for metadata in self.metadata_set.all():
# Only target media metadata
if metadata.data_type == 'media':
metadata.delete()
Copy link
Contributor

Choose a reason for hiding this comment

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

We should look into soft-deleting metadata perhaps? This seems like it would cause issues when the soft-deleted XForm is restored...

Copy link
Contributor Author

@WinnyTroy WinnyTroy Aug 20, 2021

Choose a reason for hiding this comment

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

I considered this, and I didnt see enough precedence to introduce soft_delete functionality on the MetaData model. I could be wrong on this though, hear me out.
Metadata holds form extras link enketo links and submission review enabling/disabling. These are often never deleted.
Its only now we are introducing deleting of media files associated with a form, and I dont believe we would want to restore these?,
For example, if a user creates a google sheet connection, we store the user google credentials within the form metadata. If the user requests to have the form's google sheet connection re-created, we just delete these form metadata. We wouldnt ever need to restore the old metadata.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it'll lead to a bad user experience... Users should be aware that deleting their data does have some consequences but yea I'm not entirely sure about this

Thoughts @denniswambua ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@WinnyTroy Not all metadata can be regenerated. So we will need the soft delete on metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@denniswambua made this change

@@ -953,8 +953,17 @@ def soft_delete(self, user=None):
update_fields.append('deleted_by')

self.save(update_fields=update_fields)
# Delete associated filtered datasets
for dataview in self.dataview_set.all():
dataview.soft_delete(user)
Copy link
Contributor

@DavisRayM DavisRayM Aug 20, 2021

Choose a reason for hiding this comment

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

I wonder if we have a static method that can be used to restore an XForm? Seems like a nice to have IMO since this complicates the recovery process a bit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, so these changes do increase complexity if a form is to be restored. I agree that we may need a management command that can reverse this, maybe a different issue can capture this?
To uncomplicate this though, I felt the inline comments help one understand the difference of linked datasets and merged datasets.
I also only removed the form media files, I left the other form metadata e.g enketo links, in the event a user may want to have the form restored.

Copy link
Contributor

@DavisRayM DavisRayM Aug 20, 2021

Choose a reason for hiding this comment

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

Yea, we don't have to implement a migration command for this... I think an instance method should do the trick. I'm fine with this being included in a separate PR.

I think re-creating the Enketo links is something we can easily do on the restoration of the form... The media files I'm iffy on

@WinnyTroy WinnyTroy force-pushed the xform_deletion_cleanup branch 4 times, most recently from a96c15d to ce74aeb Compare September 2, 2021 08:05
form_metadata = [
mt.data_type for mt in self.xform.metadata_set.filter(
deleted_at__isnull=True)]
self.assertNotIn('media', form_metadata)
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't we be checking that the response from the viewset in this case is empty ?

@WinnyTroy WinnyTroy force-pushed the xform_deletion_cleanup branch 2 times, most recently from da15874 to 8da71a4 Compare September 6, 2021 08:58
DavisRayM
DavisRayM previously approved these changes Sep 6, 2021
@WinnyTroy WinnyTroy force-pushed the xform_deletion_cleanup branch 2 times, most recently from 1c33192 to 8d19e16 Compare September 6, 2021 13:29
@WinnyTroy WinnyTroy force-pushed the xform_deletion_cleanup branch 2 times, most recently from 1e0ff06 to cb9ed34 Compare September 16, 2021 06:56
Comment on lines 960 to 966
for merged_dataset in self.mergedxform_ptr.all():
merged_dataset.soft_delete(user)
# Delete associated Form Media Files
for metadata in self.metadata_set.all():
metadata.soft_delete()
Copy link
Member

Choose a reason for hiding this comment

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

Since we are using soft delete, could you please filter by deleted_at___isnull=True?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch! Thanks
Made this change.

ukanga
ukanga previously approved these changes Sep 16, 2021
…iles upon form deletion. Add documentation on clean up of filtered datasets
@denniswambua denniswambua merged commit 806d125 into master Sep 17, 2021
@denniswambua denniswambua deleted the xform_deletion_cleanup branch September 17, 2021 08:29
@DavisRayM DavisRayM mentioned this pull request Oct 7, 2021
1 task
LeonRomo pushed a commit that referenced this pull request Nov 5, 2021
…iles upon form deletion. Add documentation on clean up of filtered datasets (#2136)
denniswambua pushed a commit that referenced this pull request Nov 11, 2021
* test on charts by grouping multiple fields

* test on charts with content type

* update the format to json when content type has been provided

* styling fix

* styling fix

* Update onadata/apps/api/tests/viewsets/test_charts_viewset.py

Co-authored-by: Davis Raymond <[email protected]>

* Clean out Merged Datasets upon xform deletion. Clean out Form Media Files upon form deletion. Add documentation on clean up of filtered datasets (#2136)

* Query data endpoint by submission review. Filter `NULL` review status (#2144)

* Add failing tests

* Include ability to generate sql statement for NULL query strings.

* Update data endpoint documentation

* Remove unnecessary test

* test on charts by grouping multiple fields

* test on charts with content type

* update the format to json when content type has been provided

* styling fix

* styling fix

* Update onadata/apps/api/tests/viewsets/test_charts_viewset.py

Co-authored-by: Davis Raymond <[email protected]>

* Remove unnecessary test

Co-authored-by: Davis Raymond <[email protected]>
Co-authored-by: Winnie Kiragu <[email protected]>
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.

Deleting a form should also apply to linked resources like linked media files and data views.
4 participants