summary refs log tree commit diff
path: root/synapse/storage/_base.py
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2017-11-16 15:29:10 +0000
committerRichard van der Hoff <richard@matrix.org>2017-11-16 15:29:10 +0000
commitcdc9e50a5d4cba8a7dfec32d81756eaf45f54ca5 (patch)
tree1b339318d8494e9bffb471b5f37f66a30d27df9b /synapse/storage/_base.py
parentMerge pull request #2661 from matrix-org/rav/statereadstore (diff)
downloadsynapse-cdc9e50a5d4cba8a7dfec32d81756eaf45f54ca5.tar.xz
Cleanup in _simple_upsert_txn
Bail out early to reduce indentation
Diffstat (limited to 'synapse/storage/_base.py')
-rw-r--r--synapse/storage/_base.py33
1 files changed, 17 insertions, 16 deletions
diff --git a/synapse/storage/_base.py b/synapse/storage/_base.py
index 7ebd4f189d..740400d58b 100644
--- a/synapse/storage/_base.py
+++ b/synapse/storage/_base.py
@@ -493,7 +493,7 @@ class SQLBaseStore(object):
         if lock:
             self.database_engine.lock_table(txn, table)
 
-        # Try to update
+        # First try to update.
         sql = "UPDATE %s SET %s WHERE %s" % (
             table,
             ", ".join("%s = ?" % (k,) for k in values),
@@ -502,23 +502,24 @@ class SQLBaseStore(object):
         sqlargs = values.values() + keyvalues.values()
 
         txn.execute(sql, sqlargs)
-        if txn.rowcount == 0:
-            # We didn't update and rows so insert a new one
-            allvalues = {}
-            allvalues.update(keyvalues)
-            allvalues.update(values)
-            allvalues.update(insertion_values)
+        if txn.rowcount > 0:
+            # successfully updated at least one row.
+            return False
 
-            sql = "INSERT INTO %s (%s) VALUES (%s)" % (
-                table,
-                ", ".join(k for k in allvalues),
-                ", ".join("?" for _ in allvalues)
-            )
-            txn.execute(sql, allvalues.values())
+        # We didn't update any rows so insert a new one
+        allvalues = {}
+        allvalues.update(keyvalues)
+        allvalues.update(values)
+        allvalues.update(insertion_values)
 
-            return True
-        else:
-            return False
+        sql = "INSERT INTO %s (%s) VALUES (%s)" % (
+            table,
+            ", ".join(k for k in allvalues),
+            ", ".join("?" for _ in allvalues)
+        )
+        txn.execute(sql, allvalues.values())
+        # successfully inserted
+        return True
 
     def _simple_select_one(self, table, keyvalues, retcols,
                            allow_none=False, desc="_simple_select_one"):