From 3a49136f03ee436eef36829bc8d0891424c9d9f1 Mon Sep 17 00:00:00 2001
From: Ned Batchelder <ned@nedbatchelder.com>
Date: Mon, 15 Jul 2013 16:57:28 -0400
Subject: [PATCH] Quiet some debug output, get transactions right.

---
 lms/djangoapps/courseware/management/commands/clean_history.py | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/lms/djangoapps/courseware/management/commands/clean_history.py b/lms/djangoapps/courseware/management/commands/clean_history.py
index 2c243a4..8b4f3d4 100644
--- a/lms/djangoapps/courseware/management/commands/clean_history.py
+++ b/lms/djangoapps/courseware/management/commands/clean_history.py
@@ -10,7 +10,8 @@ This command that does that.
 
 import datetime
 import json
-from optparse import make_option
+import logging
+import optparse
 import traceback
 
 from django.core.management.base import NoArgsCommand
@@ -23,7 +24,7 @@ class Command(NoArgsCommand):
     help = "Deletes unneeded rows from the StudentModuleHistory table."
 
     option_list = NoArgsCommand.option_list + (
-        make_option(
+        optparse.make_option(
             '--dry-run',
             action='store_true',
             default=False,
@@ -32,6 +33,9 @@ class Command(NoArgsCommand):
     )
 
     def handle_noargs(self, **options):
+        # We don't want to see the SQL output from the db layer.
+        logging.getLogger("").setLevel(logging.INFO)
+
         smhc = StudentModuleHistoryCleaner(
             dry_run=options["dry_run"],
         )
@@ -55,6 +59,8 @@ class StudentModuleHistoryCleaner(object):
 
         batch_size = batch_size or self.BATCH_SIZE
 
+        connection.enter_transaction_management()
+
         self.last_student_module_id = self.get_last_student_module_id()
         self.load_state()
 
@@ -65,7 +71,8 @@ class StudentModuleHistoryCleaner(object):
                 except Exception:       # pylint: disable=W0703
                     trace = traceback.format_exc()
                     self.say("Couldn't clean student_module_id {}:\n{}".format(smid, trace))
-            self.commit()
+            if not self.dry_run:
+                self.commit()
             self.save_state()
 
     def say(self, message):
--
libgit2 0.26.0