-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
Add data migration to purge deleted submissions #744
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
onadata/apps/logger/migrations/0019_purge_deleted_instances.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Generated by Django 2.2.14 on 2021-06-30 19:00 | ||
from django.conf import settings | ||
from django.db import migrations | ||
|
||
|
||
def purge_deleted_instances(apps, schema_editor): | ||
""" | ||
Remove all submissions that have been already marked as deleted from both | ||
PostgreSQL and MongoDB. If this is too slow, revert to a previous release | ||
and run the code in | ||
https://github.com/kobotoolbox/kobocat/issues/696#issuecomment-809622367 | ||
using `manage.py shell_plus`. | ||
""" | ||
Instance = apps.get_model('logger', 'Instance') | ||
to_purge = Instance.objects.exclude(deleted_at=None).only('pk') | ||
if not to_purge.exists(): | ||
return | ||
print( | ||
f'Purging {to_purge.count()} deleted instances...', end='', flush=True | ||
) | ||
for instance in to_purge.iterator(): | ||
# Manually delete from MongoDB because signals are not called in | ||
# migrations (that would require freezing all application code, not | ||
# just the models!) | ||
settings.MONGO_DB.instances.delete_one({'_id': instance.pk}) | ||
instance.delete() | ||
print('Done!', flush=True) | ||
|
||
|
||
def do_nothing(apps, schema_editor): | ||
pass | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
('logger', '0018_add_submission_counter'), | ||
] | ||
operations = [ | ||
migrations.RunPython( | ||
purge_deleted_instances, reverse_code=do_nothing | ||
), | ||
] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI: I cannot see the output of these
print
.In other migrations, we used
sys.stdout.write
andsys.stderr.write
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, I'm only now seeing this! I'm surprised you couldn't see it the output if count was greater than zero on your database, but
print()
certainly doesn't seem ideal to use here (although neither doessys.stdout.write
😉). I guess migrations are part of a management command and theoretically ought to have access somehow toself.stdout
provided somewhere by theBaseCommand
class.Be more insistent that I acknowledge your feedback next time 😛