Add "commit" argument to Base model delete()
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Wed, 28 Nov 2012 12:50:31 +0000 (13:50 +0100)
committerSebastian Spaeth <Sebastian@SSpaeth.de>
Wed, 28 Nov 2012 12:52:41 +0000 (13:52 +0100)
In case we want to bundle db actions into a single transaction, we
can now use delete(commit=False) to prevent the transaction from being
committed immediately. This is useful when e.g. deleting a User() and
thousands of his MediaEntries in a single commit.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
mediagoblin/db/sql/base.py

index 838080b03f921c1ecafb812c1493ba2b8f44933c..e10e7739d4194dedf54a389deb3b8133e3d7a0a8 100644 (file)
@@ -79,11 +79,13 @@ class GMGTableBase(object):
         sess.add(self)
         sess.commit()
 
-    def delete(self):
+    def delete(self, commit=True):
+        """Delete the object and commit the change immediately by default"""
         sess = object_session(self)
         assert sess is not None, "Not going to delete detached %r" % self
         sess.delete(self)
-        sess.commit()
+        if commit:
+            sess.commit()
 
 
 Base = declarative_base(cls=GMGTableBase)