Implement atomic_update for SQL.
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 12 Feb 2012 21:58:08 +0000 (22:58 +0100)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Sun, 4 Mar 2012 20:05:06 +0000 (21:05 +0100)
On sqlalchemy most updates are atomic enough for most use
cases. Anyway, here is an atomic_update that is compatible
to the mongo version.

mediagoblin/db/sql/util.py
mediagoblin/db/util.py

index 08602414c88bab545d255412093c040b0f3eb853..13bc97e1b45b4a94ef212a7399eeffc325aeb327 100644 (file)
@@ -1,5 +1,5 @@
 # GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011 MediaGoblin contributors.  See AUTHORS.
+# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as published by
@@ -16,6 +16,8 @@
 
 
 import sys
+from mediagoblin.db.sql.base import Session
+
 
 def _simple_printer(string):
     """
@@ -269,3 +271,14 @@ def assure_migrations_table_setup(db):
     if not MigrationData.__table__.exists(db.bind):
         MigrationData.metadata.create_all(
             db.bind, tables=[MigrationData.__table__])
+
+
+##########################
+# Random utility functions
+##########################
+
+
+def atomic_update(table, query_dict, update_values):
+    table.find(query_dict).update(update_values,
+       synchronize_session=False)
+    Session.commit()
index c48c4ba64b5a7d290f6807bff6ba135de3a92c54..73aee238b832dbb880c1fad59e419777e41f7561 100644 (file)
@@ -21,6 +21,7 @@ except ImportError:
 
 if use_sql:
     from mediagoblin.db.sql.fake import ObjectId, InvalidId, DESCENDING
+    from mediagoblin.db.sql.util import atomic_update
 else:
     from mediagoblin.db.mongo.util import \
         ObjectId, InvalidId, DESCENDING, atomic_update