From 28d5458a1cfb4d424b8014f3cebe5fbe446000d2 Mon Sep 17 00:00:00 2001
From: hunytalk <ahannan@edx.org>
Date: Wed, 11 Dec 2019 20:14:01 +0500
Subject: [PATCH] Custom management command for data migration in Schedule

---
 .../commands/schedules_data_migration.py      | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)
 create mode 100644 openedx/core/djangoapps/schedules/management/commands/schedules_data_migration.py

diff --git a/openedx/core/djangoapps/schedules/management/commands/schedules_data_migration.py b/openedx/core/djangoapps/schedules/management/commands/schedules_data_migration.py
new file mode 100644
index 00000000000..84dd5cd1002
--- /dev/null
+++ b/openedx/core/djangoapps/schedules/management/commands/schedules_data_migration.py
@@ -0,0 +1,30 @@
+"""
+Management command to perform data migration for copying values between date fields of Schedule Model
+"""
+import time
+
+from django.core.management.base import BaseCommand
+from django.db import transaction
+from openedx.core.djangoapps.schedules.models import Schedule
+
+
+class Command(BaseCommand):
+    """
+    Command to perform data migration for Schedule Model
+    """
+    help = 'Copy values from start to start_date in Schedule model'
+
+    def add_arguments(self, parser):
+        parser.add_argument('--delay', type=float, default=0.2, help='Time delay in each iteration')
+        parser.add_argument('--size', type=int, default=1000, help='Batch size for atomic migration')
+
+    def handle(self, *args, **kwargs):
+        delay = kwargs['delay']
+        size = kwargs['size']
+        while Schedule.objects.filter(start_date__isnull=True).exists():
+            time.sleep(delay)
+            with transaction.atomic():
+                for row in Schedule.objects.filter(start_date__isnull=True)[:size]:
+                    time.sleep(delay)
+                    row.start_date = row.start
+                    row.save()
-- 
GitLab