From 0537ba7f6e7cac2ba992cb91bf0ae8075d16aa8c Mon Sep 17 00:00:00 2001 From: Matt Riley Date: Thu, 12 Dec 2024 11:55:19 -0500 Subject: [PATCH] Remove extraneous column and have better checks on data validity --- .../cleanup/management/commands/hard_delete_forms.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/corehq/apps/cleanup/management/commands/hard_delete_forms.py b/corehq/apps/cleanup/management/commands/hard_delete_forms.py index b97f42109e69..e9a33cd6655d 100644 --- a/corehq/apps/cleanup/management/commands/hard_delete_forms.py +++ b/corehq/apps/cleanup/management/commands/hard_delete_forms.py @@ -1,11 +1,11 @@ -from django.core.management.base import BaseCommand +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 = 1 +INDEX_FORM_ID = 0 CHUNK_SIZE = 100 @@ -17,12 +17,16 @@ def add_arguments(self, parser): 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) as csvfile: + 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): - next(rows) # skip header line + 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