Commit 5ad00f0e by Eric Fischer Committed by GitHub

Merge pull request #70 from edx/efischer/slice_all

Use sliced all() in management command
parents 110a6e0b 3906fa7e
...@@ -33,7 +33,7 @@ def load_requirements(*requirements_paths): ...@@ -33,7 +33,7 @@ def load_requirements(*requirements_paths):
setup( setup(
name='edx-submissions', name='edx-submissions',
version='2.0.10', version='2.0.11',
author='edX', author='edX',
description='An API for creating submissions and scores.', description='An API for creating submissions and scores.',
url='http://github.com/edx/edx-submissions.git', url='http://github.com/edx/edx-submissions.git',
......
...@@ -14,11 +14,13 @@ import time ...@@ -14,11 +14,13 @@ import time
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.db import transaction from django.db import transaction
from django.db.models import Max
from submissions.models import Submission from submissions.models import Submission
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
class Command(BaseCommand): class Command(BaseCommand):
""" """
Example usage: ./manage.py lms --settings=devstack update_submissions_uuids.py Example usage: ./manage.py lms --settings=devstack update_submissions_uuids.py
...@@ -35,16 +37,19 @@ class Command(BaseCommand): ...@@ -35,16 +37,19 @@ class Command(BaseCommand):
parser.add_argument( parser.add_argument(
'--start', '-s', '--start', '-s',
default=0, default=0,
type=int,
help=u"The Submission.id at which to begin updating rows. 0 by default." help=u"The Submission.id at which to begin updating rows. 0 by default."
) )
parser.add_argument( parser.add_argument(
'--chunk', '-c', '--chunk', '-c',
default=1000, default=1000,
type=int,
help=u"Batch size, how many rows to update in a given transaction. Default 1000.", help=u"Batch size, how many rows to update in a given transaction. Default 1000.",
) )
parser.add_argument( parser.add_argument(
'--wait', '-w', '--wait', '-w',
default=2, default=2,
type=int,
help=u"Wait time between transactions, in seconds. Default 2.", help=u"Wait time between transactions, in seconds. Default 2.",
) )
...@@ -53,15 +58,17 @@ class Command(BaseCommand): ...@@ -53,15 +58,17 @@ class Command(BaseCommand):
By default, we're going to do this in chunks. This way, if there ends up being an error, By default, we're going to do this in chunks. This way, if there ends up being an error,
we can check log messages and continue from that point after fixing the issue. we can check log messages and continue from that point after fixing the issue.
""" """
total_len = Submission.objects.count() # Note that by taking last_id here, we're going to miss any submissions created *during* the command execution
log.info("Beginning uuid update, {} rows exist in total".format(total_len)) # But that's okay! All new entries have already been created using the new style, no acion needed there
last_id = Submission._objects.all().aggregate(Max('id'))['id__max']
log.info("Beginning uuid update")
current = options['start']; current = options['start']
while current < total_len: while current < last_id:
end_chunk = current + options['chunk'] if total_len - options['chunk'] >= current else total_len end_chunk = current + options['chunk'] if last_id - options['chunk'] >= current else last_id
log.info("Updating entries in range [{}, {})".format(current, end_chunk)) log.info("Updating entries in range [{}, {}]".format(current, end_chunk))
with transaction.atomic(): with transaction.atomic():
for submission in Submission.objects.filter(id__gte=current, id__lt=end_chunk).iterator(): for submission in Submission._objects.filter(id__gte=current, id__lte=end_chunk).iterator():
submission.save(update_fields=['uuid']) submission.save(update_fields=['uuid'])
time.sleep(options['wait']) time.sleep(options['wait'])
current = current + options['chunk'] current = end_chunk + 1
...@@ -140,6 +140,7 @@ class Submission(models.Model): ...@@ -140,6 +140,7 @@ class Submission(models.Model):
return super(Submission.SoftDeletedManager, self).get_queryset().exclude(status=Submission.DELETED) return super(Submission.SoftDeletedManager, self).get_queryset().exclude(status=Submission.DELETED)
objects = SoftDeletedManager() objects = SoftDeletedManager()
_objects = models.Manager() # Don't use this unless you know and can explain why objects doesn't work for you
@staticmethod @staticmethod
def get_cache_key(sub_uuid): def get_cache_key(sub_uuid):
......
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