summary refs log tree commit diff
path: root/synapse/storage/__init__.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-03-04 13:43:17 +0000
committerErik Johnston <erik@matrix.org>2015-03-04 13:43:17 +0000
commit640e53935dc2585c7a6365980cd288e5ed176b38 (patch)
tree2ea02e936edf680c3efe3efc5f5550ea4921a869 /synapse/storage/__init__.py
parentActually add full_schemas dir (diff)
downloadsynapse-640e53935dc2585c7a6365980cd288e5ed176b38.tar.xz
Use context manager with db conn to correctly commit and rollback
Diffstat (limited to 'synapse/storage/__init__.py')
-rw-r--r--synapse/storage/__init__.py22
1 files changed, 11 insertions, 11 deletions
diff --git a/synapse/storage/__init__.py b/synapse/storage/__init__.py
index a22a0e2f39..d637e8c4f7 100644
--- a/synapse/storage/__init__.py
+++ b/synapse/storage/__init__.py
@@ -55,7 +55,7 @@ import re
 logger = logging.getLogger(__name__)
 
 
-# Remember to update this number every time an change is made to database
+# Remember to update this number every time a change is made to database
 # schema files, so the users will be informed on server restarts.
 SCHEMA_VERSION = 14
 
@@ -583,19 +583,19 @@ def prepare_database(db_conn):
     """Prepares a database for usage. Will either create all necessary tables
     or upgrade from an older schema version.
     """
-    cur = db_conn.cursor()
-    version_info = _get_or_create_schema_state(cur)
+    with db_conn:
+        cur = db_conn.cursor()
+        version_info = _get_or_create_schema_state(cur)
 
-    if version_info:
-        user_version, delta_files, upgraded = version_info
-        _upgrade_existing_database(cur, user_version, delta_files, upgraded)
-    else:
-        _setup_new_database(cur)
+        if version_info:
+            user_version, delta_files, upgraded = version_info
+            _upgrade_existing_database(cur, user_version, delta_files, upgraded)
+        else:
+            _setup_new_database(cur)
 
-    cur.execute("PRAGMA user_version = %d" % (SCHEMA_VERSION,))
-    db_conn.commit()
+        cur.execute("PRAGMA user_version = %d" % (SCHEMA_VERSION,))
 
-    cur.close()
+        cur.close()
 
 
 def _setup_new_database(cur):