Commit 239a6f18 by Matjaz Gregoric Committed by GitHub

Merge pull request #143 from open-craft/mtyaka/copy-course-ids-management

Add management command to manually migrate course_id field.
parents a4cc464c fa2cb87c
...@@ -11,7 +11,7 @@ dependencies: ...@@ -11,7 +11,7 @@ dependencies:
- "pip install -r requirements.txt" - "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/base.txt"
- "pip install -r $VIRTUAL_ENV/src/xblock-sdk/requirements/test.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.6.6.tar.gz" - "pip uninstall -y xblock-problem-builder && python setup.py sdist && pip install dist/xblock-problem-builder-2.6.7.tar.gz"
- "pip install -r test_requirements.txt" - "pip install -r test_requirements.txt"
- "mkdir var" - "mkdir var"
test: test:
......
import math
import time
from django.core.management.base import BaseCommand
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 into 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(
'--sleep',
help='Number of seconds to sleep before processing the next batch (default: 5).',
type=int,
default=5
)
def handle(self, *args, **options):
batch_size = options['batch_size']
sleep_time = options['sleep']
queryset = Answer.objects.filter(course_key__isnull=True)
batch_count = int(math.ceil(queryset.count() / float(batch_size)))
self.stdout.write(
"Copying Answer.course_id field into Answer.course_key in batches of {}".format(batch_size)
)
for batch in xrange(1, batch_count + 1):
for answer in queryset[:batch_size]:
answer.course_key = answer.course_id
answer.save()
self.stdout.write("Processed batch {} of {}".format(batch, batch_count))
if batch != batch_count:
time.sleep(sleep_time)
self.stdout.write("Successfully copied Answer.course_id into Answer.course_key")
...@@ -71,7 +71,7 @@ BLOCKS = [ ...@@ -71,7 +71,7 @@ BLOCKS = [
setup( setup(
name='xblock-problem-builder', name='xblock-problem-builder',
version='2.6.6', version='2.6.7',
description='XBlock - Problem Builder', description='XBlock - Problem Builder',
packages=['problem_builder', 'problem_builder.v1'], packages=['problem_builder', 'problem_builder.v1'],
install_requires=[ install_requires=[
......
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