summary refs log tree commit diff
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2020-01-09 17:33:41 +0000
committerRichard van der Hoff <richard@matrix.org>2020-01-09 18:05:59 +0000
commite48ba84e0bfe081814941b74e610ddcd168a3ce8 (patch)
tree3cd3e1104c081261fb04cb4cb0872194556000fd
parentModify check_database to take a connection rather than a cursor (diff)
downloadsynapse-e48ba84e0bfe081814941b74e610ddcd168a3ce8.tar.xz
Check postgres version in check_database
this saves doing it on each connection, and will allow us to pass extra options
in.
-rw-r--r--synapse/storage/engines/postgres.py25
1 files changed, 12 insertions, 13 deletions
diff --git a/synapse/storage/engines/postgres.py b/synapse/storage/engines/postgres.py
index ba19785fd7..2a285e018c 100644
--- a/synapse/storage/engines/postgres.py
+++ b/synapse/storage/engines/postgres.py
@@ -33,6 +33,16 @@ class PostgresEngine(object):
         self._version = None  # unknown as yet
 
     def check_database(self, db_conn):
+        # Get the version of PostgreSQL that we're using. As per the psycopg2
+        # docs: The number is formed by converting the major, minor, and
+        # revision numbers into two-decimal-digit numbers and appending them
+        # together. For example, version 8.1.5 will be returned as 80105
+        self._version = db_conn.server_version
+
+        # Are we on a supported PostgreSQL version?
+        if self._version < 90500:
+            raise RuntimeError("Synapse requires PostgreSQL 9.5+ or above.")
+
         with db_conn.cursor() as txn:
             txn.execute("SHOW SERVER_ENCODING")
             rows = txn.fetchall()
@@ -46,17 +56,6 @@ class PostgresEngine(object):
         return sql.replace("?", "%s")
 
     def on_new_connection(self, db_conn):
-
-        # Get the version of PostgreSQL that we're using. As per the psycopg2
-        # docs: The number is formed by converting the major, minor, and
-        # revision numbers into two-decimal-digit numbers and appending them
-        # together. For example, version 8.1.5 will be returned as 80105
-        self._version = db_conn.server_version
-
-        # Are we on a supported PostgreSQL version?
-        if self._version < 90500:
-            raise RuntimeError("Synapse requires PostgreSQL 9.5+ or above.")
-
         db_conn.set_isolation_level(
             self.module.extensions.ISOLATION_LEVEL_REPEATABLE_READ
         )
@@ -120,8 +119,8 @@ class PostgresEngine(object):
         Returns:
             string
         """
-        # note that this is a bit of a hack because it relies on on_new_connection
-        # having been called at least once. Still, that should be a safe bet here.
+        # note that this is a bit of a hack because it relies on check_database
+        # having been called. Still, that should be a safe bet here.
         numver = self._version
         assert numver is not None