summary refs log tree commit diff
path: root/synapse/storage/engines/sqlite.py
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2019-12-18 10:45:12 +0000
committerGitHub <noreply@github.com>2019-12-18 10:45:12 +0000
commit2284eb3a533a2df04784df08da28e67d6588a5ea (patch)
treedf75df4b3eba90e8299c8bae61157d075a0d423b /synapse/storage/engines/sqlite.py
parentMerge branch 'master' into develop (diff)
downloadsynapse-2284eb3a533a2df04784df08da28e67d6588a5ea.tar.xz
Add database config class (#6513)
This encapsulates config for a given database and is the way to get new
connections.
Diffstat (limited to 'synapse/storage/engines/sqlite.py')
-rw-r--r--synapse/storage/engines/sqlite.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/synapse/storage/engines/sqlite.py b/synapse/storage/engines/sqlite.py
index ddad17dc5a..df039a072d 100644
--- a/synapse/storage/engines/sqlite.py
+++ b/synapse/storage/engines/sqlite.py
@@ -16,8 +16,6 @@
 import struct
 import threading
 
-from synapse.storage.prepare_database import prepare_database
-
 
 class Sqlite3Engine(object):
     single_threaded = True
@@ -25,6 +23,9 @@ class Sqlite3Engine(object):
     def __init__(self, database_module, database_config):
         self.module = database_module
 
+        database = database_config.get("args", {}).get("database")
+        self._is_in_memory = database in (None, ":memory:",)
+
         # The current max state_group, or None if we haven't looked
         # in the DB yet.
         self._current_state_group_id = None
@@ -59,7 +60,16 @@ class Sqlite3Engine(object):
         return sql
 
     def on_new_connection(self, db_conn):
-        prepare_database(db_conn, self, config=None)
+
+        # We need to import here to avoid an import loop.
+        from synapse.storage.prepare_database import prepare_database
+
+        if self._is_in_memory:
+            # In memory databases need to be rebuilt each time. Ideally we'd
+            # reuse the same connection as we do when starting up, but that
+            # would involve using adbapi before we have started the reactor.
+            prepare_database(db_conn, self, config=None)
+
         db_conn.create_function("rank", 1, _rank)
 
     def is_deadlock(self, error):