From 329e39034b2ef2a9663fc2de16c8eae6c0313fdc Mon Sep 17 00:00:00 2001 From: Elrond Date: Wed, 28 Nov 2012 13:50:31 +0100 Subject: [PATCH 1/2] Add "commit" argument to Base model delete() 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 --- mediagoblin/db/sql/base.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/mediagoblin/db/sql/base.py b/mediagoblin/db/sql/base.py index 838080b0..e10e7739 100644 --- a/mediagoblin/db/sql/base.py +++ b/mediagoblin/db/sql/base.py @@ -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) From 9437ea4742eecb6854810fe020fe721b413da276 Mon Sep 17 00:00:00 2001 From: Sebastian Spaeth Date: Wed, 28 Nov 2012 14:36:58 +0100 Subject: [PATCH 2/2] Add commit argument to clean_orphan_tags So we can prevent the session from being committed if we don't want it. --- mediagoblin/db/sql/util.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mediagoblin/db/sql/util.py b/mediagoblin/db/sql/util.py index bd92393c..c6d8562e 100644 --- a/mediagoblin/db/sql/util.py +++ b/mediagoblin/db/sql/util.py @@ -297,17 +297,17 @@ def media_entries_for_tag_slug(dummy_db, tag_slug): & (Tag.slug == tag_slug)) -def clean_orphan_tags(): +def clean_orphan_tags(commit=True): + """Search for unused MediaTags and delete them""" q1 = Session.query(Tag).outerjoin(MediaTag).filter(MediaTag.id==None) for t in q1: Session.delete(t) - # The "let the db do all the work" version: # q1 = Session.query(Tag.id).outerjoin(MediaTag).filter(MediaTag.id==None) # q2 = Session.query(Tag).filter(Tag.id.in_(q1)) # q2.delete(synchronize_session = False) - - Session.commit() + if commit: + Session.commit() def check_collection_slug_used(dummy_db, creator_id, slug, ignore_c_id):