-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
Added a form deletion script for genie requests #35514
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b000b30
Add the ability to return the deleted form ids during deletion
mjriley 9ca88fd
Added form deletion script
mjriley 4fccff9
Renamed deletion script to indicate hard deletion
mjriley 0537ba7
Remove extraneous column and have better checks on data validity
mjriley 620c669
make hard deletion actions atomic
mjriley 1d208a7
fixed comment
mjriley 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
56 changes: 56 additions & 0 deletions
56
corehq/apps/cleanup/management/commands/hard_delete_forms.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,56 @@ | ||
from django.core.management.base import BaseCommand, CommandError | ||
import csv | ||
import itertools | ||
from dimagi.utils.chunked import chunked | ||
from corehq.form_processor.models import XFormInstance | ||
|
||
|
||
INDEX_FORM_ID = 0 | ||
CHUNK_SIZE = 100 | ||
|
||
|
||
class Command(BaseCommand): | ||
def add_arguments(self, parser): | ||
parser.add_argument('domain', help='Domain name that owns the forms to be deleted') | ||
parser.add_argument('filename', help='path to the CSV file') | ||
parser.add_argument('--resume_id', help='form ID to start at, within the CSV file') | ||
|
||
def handle(self, domain, filename, resume_id=None, **options): | ||
# expects the filename to have a CSV with a header containing "Deletion Status" and "Form ID" fields | ||
with open(filename, mode='r', encoding='utf-8-sig') as csvfile: | ||
reader = csv.reader(csvfile, delimiter=',') | ||
self._process_rows(reader, domain, resume_id) | ||
|
||
def _process_rows(self, rows, domain, resume_id): | ||
header_row = next(rows) # skip header line | ||
if header_row[INDEX_FORM_ID] != 'Form ID': | ||
raise CommandError( | ||
f'Expected Column {INDEX_FORM_ID} to be "Form ID", found "{header_row[INDEX_FORM_ID]}". Exiting' | ||
) | ||
|
||
num_deleted = 0 | ||
|
||
if resume_id: | ||
print('resuming at: ', resume_id) | ||
rows = itertools.dropwhile(lambda row: row[INDEX_FORM_ID] != resume_id, rows) | ||
|
||
print('Starting form deletion') | ||
for chunk in chunked(rows, CHUNK_SIZE): | ||
form_ids = [row[INDEX_FORM_ID] for row in chunk] | ||
|
||
try: | ||
deleted_form_ids = set(XFormInstance.objects.hard_delete_forms( | ||
domain, form_ids, return_ids=True)) | ||
except Exception: | ||
print('failed during processing of: ', form_ids) | ||
raise | ||
|
||
for form_id in form_ids: | ||
if form_id in deleted_form_ids: | ||
print('Deleted: ', form_id) | ||
else: | ||
print('Not found:', form_id) | ||
|
||
num_deleted += len(deleted_form_ids) | ||
|
||
print(f'Complete -- removed {num_deleted} forms') |
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
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
Oops, something went wrong.
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.
I think this comment is wrong now?