From 4caab0e95e6972e9c0533bc1897073bda5a464bf Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Fri, 20 Dec 2019 10:46:46 +0000 Subject: [PATCH] Backport fixes to sqlite upgrade from develop (#6578) Only run prepare_database on connection for in-memory databases. Fixes #6569. --- changelog.d/6578.bugfix | 1 + synapse/storage/engines/sqlite.py | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelog.d/6578.bugfix diff --git a/changelog.d/6578.bugfix b/changelog.d/6578.bugfix new file mode 100644 index 000000000..fae55a445 --- /dev/null +++ b/changelog.d/6578.bugfix @@ -0,0 +1 @@ +Fix a bug introduced in Synapse 1.7.0 which caused an error on startup when upgrading from versions before 1.3.0. diff --git a/synapse/storage/engines/sqlite.py b/synapse/storage/engines/sqlite.py index ddad17dc5..cbc74cd30 100644 --- a/synapse/storage/engines/sqlite.py +++ b/synapse/storage/engines/sqlite.py @@ -25,6 +25,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 +62,12 @@ class Sqlite3Engine(object): return sql def on_new_connection(self, db_conn): - prepare_database(db_conn, self, config=None) + 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):