Commit 7665063a by Braden MacDonald Committed by GitHub

Merge pull request #160 from open-craft/cliff/migrate-course-key

New course key migration script.
parents be723402 ddcf831b
......@@ -11,7 +11,7 @@ dependencies:
- "pip install -r requirements.txt"
- "pip install -r $VIRTUAL_ENV/src/xblock-sdk/requirements/base.txt"
- "pip install -r $VIRTUAL_ENV/src/xblock-sdk/requirements/test.txt"
- "pip uninstall -y xblock-problem-builder && python setup.py sdist && pip install dist/xblock-problem-builder-2.7.2.tar.gz"
- "pip uninstall -y xblock-problem-builder && python setup.py sdist && pip install dist/xblock-problem-builder-*.tar.gz"
- "pip install -r test_requirements.txt"
- "mkdir var"
test:
......
"""
Command to copy content of Answer.course_id to Answer.course_key.
"""
import time
from django.core.management.base import BaseCommand
from django.db.models import F, Q
from problem_builder.models import Answer
class Command(BaseCommand):
"""
Copy content of the deprecated Answer.course_id column into
Answer.course_key in batches. The batch size and sleep time between
batches are configurable.
"""
help = 'Copy content of the deprecated Answer.course_id column to Answer.course_key in batches'
def add_arguments(self, parser):
parser.add_argument(
'--batch-size',
help='The size of each batch of records to copy (default: 5000).',
type=int,
default=5000,
)
parser.add_argument(
'--offset',
help='The lowest primary key to copy (default: 0)',
type=int,
default=0,
)
parser.add_argument(
'--sleep',
help='Number of seconds to sleep before processing the next batch (default: 1).',
type=int,
default=1,
)
def handle(self, *args, **options):
batch_size = options['batch_size']
sleep_time = options['sleep']
offset = options['offset']
max_pk = Answer.objects.order_by('-pk')[0].pk
batch_start = offset
batch_stop = batch_start + batch_size
self.stdout.write(
"Copying Answer.course_id field into Answer.course_key in batches of at most {}".format(
batch_size
)
)
while batch_start <= max_pk:
queryset = Answer.objects.filter(
pk__gte=batch_start,
pk__lt=batch_stop,
).filter(
Q(course_key__isnull=True) | Q(course_key='')
)
queryset.update(course_key=F('course_id'))
self.stdout.write(
"Processed Answers through pk: {}, max pk: {}".format(batch_stop, max_pk)
)
if batch_stop < max_pk:
time.sleep(sleep_time)
batch_start = batch_stop
batch_stop += batch_size
self.stdout.write("Successfully copied Answer.course_id into Answer.course_key")
ddt
mock
unicodecsv==0.9.4
edx-opaque-keys>=0.4
-e git+https://github.com/edx/xblock-utils.git@v1.0.3#egg=xblock-utils
-e .
......@@ -21,7 +21,7 @@
# Imports ###########################################################
import os
from setuptools import setup
from setuptools import setup, find_packages
# Functions #########################################################
......@@ -73,7 +73,7 @@ setup(
name='xblock-problem-builder',
version='2.7.2',
description='XBlock - Problem Builder',
packages=['problem_builder', 'problem_builder.v1'],
packages=find_packages(),
install_requires=[
'XBlock',
'xblock-utils',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment