Commit 94e98e9c by Ali Mohammad

Merge pull request #8257 from edx/alawibaba/plat589

Make clean_history.py use the StudentHistoryModule django model.
parents 416dd82d 36e5fcf4
...@@ -16,7 +16,9 @@ import time ...@@ -16,7 +16,9 @@ import time
import traceback import traceback
from django.core.management.base import NoArgsCommand from django.core.management.base import NoArgsCommand
from django.db import connection from django.db import transaction
from django.db.models import Max
from courseware.models import StudentModuleHistory
class Command(NoArgsCommand): class Command(NoArgsCommand):
...@@ -72,7 +74,7 @@ class StudentModuleHistoryCleaner(object): ...@@ -72,7 +74,7 @@ class StudentModuleHistoryCleaner(object):
batch_size = batch_size or self.BATCH_SIZE batch_size = batch_size or self.BATCH_SIZE
connection.enter_transaction_management() transaction.enter_transaction_management()
self.last_student_module_id = self.get_last_student_module_id() self.last_student_module_id = self.get_last_student_module_id()
self.load_state() self.load_state()
...@@ -104,7 +106,7 @@ class StudentModuleHistoryCleaner(object): ...@@ -104,7 +106,7 @@ class StudentModuleHistoryCleaner(object):
Commit the transaction. Commit the transaction.
""" """
self.say("Committing") self.say("Committing")
connection.commit() transaction.commit()
def load_state(self): def load_state(self):
""" """
...@@ -140,11 +142,8 @@ class StudentModuleHistoryCleaner(object): ...@@ -140,11 +142,8 @@ class StudentModuleHistoryCleaner(object):
""" """
Return the id of the last student_module. Return the id of the last student_module.
""" """
cursor = connection.cursor() last = StudentModuleHistory.objects.all() \
cursor.execute(""" .aggregate(Max('student_module'))['student_module__max']
SELECT max(student_module_id) FROM courseware_studentmodulehistory
""")
last = cursor.fetchone()[0]
self.say("Last student_module_id is {}".format(last)) self.say("Last student_module_id is {}".format(last))
return last return last
...@@ -176,17 +175,11 @@ class StudentModuleHistoryCleaner(object): ...@@ -176,17 +175,11 @@ class StudentModuleHistoryCleaner(object):
Return a list: [(id, created), ...], all the rows of history. Return a list: [(id, created), ...], all the rows of history.
""" """
cursor = connection.cursor() history = StudentModuleHistory.objects \
cursor.execute( .filter(student_module=student_module_id) \
""" .order_by('created', 'id')
SELECT id, created FROM courseware_studentmodulehistory
WHERE student_module_id = %s return [(row.id, row.created) for row in history]
ORDER BY created, id
""",
[student_module_id]
)
history = cursor.fetchall()
return history
def delete_history(self, ids_to_delete): def delete_history(self, ids_to_delete):
""" """
...@@ -196,13 +189,7 @@ class StudentModuleHistoryCleaner(object): ...@@ -196,13 +189,7 @@ class StudentModuleHistoryCleaner(object):
""" """
assert ids_to_delete assert ids_to_delete
cursor = connection.cursor() StudentModuleHistory.objects.filter(id__in=ids_to_delete).delete()
cursor.execute(
"""
DELETE FROM courseware_studentmodulehistory
WHERE id IN ({ids})
""".format(ids=",".join(str(i) for i in ids_to_delete))
)
def clean_one_student_module(self, student_module_id): def clean_one_student_module(self, student_module_id):
"""Clean one StudentModule's-worth of history. """Clean one StudentModule's-worth of history.
......
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