summary refs log tree commit diff
path: root/synapse/storage/engines
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2023-03-30 16:21:12 +0100
committerGitHub <noreply@github.com>2023-03-30 16:21:12 +0100
commit91c3f32673e0c62ea603dcc18f7f21f73c011f33 (patch)
treecd79456df5729402960c80d033dab32a24017925 /synapse/storage/engines
parentImplement MSC3984 to proxy /keys/query requests to appservices. (#15321) (diff)
downloadsynapse-91c3f32673e0c62ea603dcc18f7f21f73c011f33.tar.xz
Speed up SQLite unit test CI (#15334)
Tests now take 40% of the time.
Diffstat (limited to 'synapse/storage/engines')
-rw-r--r--synapse/storage/engines/sqlite.py17
1 files changed, 16 insertions, 1 deletions
diff --git a/synapse/storage/engines/sqlite.py b/synapse/storage/engines/sqlite.py
index 28751e89a5..ca8c59297c 100644
--- a/synapse/storage/engines/sqlite.py
+++ b/synapse/storage/engines/sqlite.py
@@ -34,6 +34,13 @@ class Sqlite3Engine(BaseDatabaseEngine[sqlite3.Connection, sqlite3.Cursor]):
             ":memory:",
         )
 
+        # A connection to a database that has already been prepared, to use as a
+        # base for an in-memory connection. This is used during unit tests to
+        # speed up setting up the DB.
+        self._prepped_conn: Optional[sqlite3.Connection] = database_config.get(
+            "_TEST_PREPPED_CONN"
+        )
+
         if platform.python_implementation() == "PyPy":
             # pypy's sqlite3 module doesn't handle bytearrays, convert them
             # back to bytes.
@@ -84,7 +91,15 @@ class Sqlite3Engine(BaseDatabaseEngine[sqlite3.Connection, sqlite3.Cursor]):
             # 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)
+            #
+            # If we have a `prepped_conn` we can use that to initialise the DB,
+            # otherwise we need to call `prepare_database`.
+            if self._prepped_conn is not None:
+                # Initialise the new DB from the pre-prepared DB.
+                assert isinstance(db_conn.conn, sqlite3.Connection)
+                self._prepped_conn.backup(db_conn.conn)
+            else:
+                prepare_database(db_conn, self, config=None)
 
         db_conn.create_function("rank", 1, _rank)
         db_conn.execute("PRAGMA foreign_keys = ON;")