diff --git a/openedx_course_enrollment_audit/management/commands/backfill_course_enrollment_audit.py b/openedx_course_enrollment_audit/management/commands/backfill_course_enrollment_audit.py index 02ca061..af82be2 100644 --- a/openedx_course_enrollment_audit/management/commands/backfill_course_enrollment_audit.py +++ b/openedx_course_enrollment_audit/management/commands/backfill_course_enrollment_audit.py @@ -12,14 +12,11 @@ def handle(self, *args, **kwargs): total_count = ManualEnrollmentAudit.objects.count() self.stdout.write(self.style.NOTICE(f'Starting backfill of {total_count} records from ManualEnrollmentAudit.')) - for manual_enrollment in ManualEnrollmentAudit.objects.all().iterator(): - course_enrollment_audit, created = CourseEnrollmentAudit.create_from_manual_enrollment(manual_enrollment) - - if created: - self.stdout.write(self.style.SUCCESS( - f'Created new CourseEnrollmentAudit for email: {manual_enrollment.enrolled_email}, course_id: {course_enrollment_audit.course_id}.')) - else: - self.stdout.write(self.style.WARNING( - f'Updated existing CourseEnrollmentAudit for email: {manual_enrollment.enrolled_email}, course_id: {course_enrollment_audit.course_id}.')) + batch_size = 10000 + for i, manual_enrollment in enumerate(ManualEnrollmentAudit.objects.all().iterator(), 1): + CourseEnrollmentAudit.create_from_manual_enrollment(manual_enrollment) + + if i % batch_size == 0: + self.stdout.write(self.style.SUCCESS(f'Processed {i} records...')) self.stdout.write(self.style.SUCCESS('Backfill completed successfully.')) diff --git a/openedx_course_enrollment_audit/models.py b/openedx_course_enrollment_audit/models.py index 829e41c..6f4dd3d 100644 --- a/openedx_course_enrollment_audit/models.py +++ b/openedx_course_enrollment_audit/models.py @@ -71,7 +71,8 @@ def create_from_manual_enrollment(cls, manual_enrollment): parsed_role = parsed_data.get('role') parsed_reason = parsed_data.get('reason') except JSONDecodeError: - pass # If JSON is invalid or missing keys, fields remain None + # If the reason field is not a valid JSON, store it as is. + parsed_reason = manual_enrollment.reason return cls.objects.update_or_create( enrolled_email=manual_enrollment.enrolled_email,