From 204132a998ec3be7069a9f2dada323bcdb217908 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 29 Apr 2015 11:42:28 +0100 Subject: Check that postgres database has correct charset set --- synapse/storage/engines/postgres.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'synapse/storage/engines/postgres.py') diff --git a/synapse/storage/engines/postgres.py b/synapse/storage/engines/postgres.py index b8cca9b187..ca858daee9 100644 --- a/synapse/storage/engines/postgres.py +++ b/synapse/storage/engines/postgres.py @@ -15,12 +15,23 @@ from synapse.storage import prepare_database +from ._base import IncorrectDatabaseSetup + class PostgresEngine(object): def __init__(self, database_module): self.module = database_module self.module.extensions.register_type(self.module.extensions.UNICODE) + def check_database(self, txn): + txn.execute("SHOW SERVER_ENCODING") + rows = txn.fetchall() + if rows and rows[0][0] != "UTF8": + raise IncorrectDatabaseSetup( + "Database has incorrect encoding: '%s' instead of 'UTF8'" + % (rows[0][0],) + ) + def convert_param_style(self, sql): return sql.replace("?", "%s") -- cgit 1.4.1 From cd0864121bce3e1ab41ce432670cc87c4ffd54cd Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 29 Apr 2015 12:12:18 +0100 Subject: Make postgres database error slightly more helpful --- synapse/app/homeserver.py | 27 +++++++++++++++------------ synapse/storage/engines/postgres.py | 3 ++- 2 files changed, 17 insertions(+), 13 deletions(-) (limited to 'synapse/storage/engines/postgres.py') diff --git a/synapse/app/homeserver.py b/synapse/app/homeserver.py index cbd295c0a5..29a1bf1d70 100755 --- a/synapse/app/homeserver.py +++ b/synapse/app/homeserver.py @@ -245,24 +245,27 @@ class SynapseHomeServer(HomeServer): db_conn.cursor(), database_engine, self.hostname ) if not all_users_native: - sys.stderr.write( - "\n" - "******************************************************\n" + quit_with_error( "Found users in database not native to %s!\n" - "You cannot changed a synapse server_name after it's been configured\n" - "******************************************************\n" - "\n" % (self.hostname,) + "You cannot changed a synapse server_name after it's been configured" + % (self.hostname,) ) - sys.exit(1) try: database_engine.check_database(db_conn.cursor()) except IncorrectDatabaseSetup as e: - sys.stderr.write("*" * len(e.message) + '\n') - sys.stderr.write(e.message) - sys.stderr.write('\n') - sys.stderr.write("*" * len(e.message) + '\n') - sys.exit(2) + quit_with_error(e.message) + + +def quit_with_error(error_string): + message_lines = error_string.split("\n") + line_length = max([len(l) for l in message_lines]) + 2 + sys.stderr.write("*" * line_length + '\n') + for line in message_lines: + if line.strip(): + sys.stderr.write(" %s\n" % (line.strip(),)) + sys.stderr.write("*" * line_length + '\n') + sys.exit(1) def get_version_string(): diff --git a/synapse/storage/engines/postgres.py b/synapse/storage/engines/postgres.py index ca858daee9..7125f66f01 100644 --- a/synapse/storage/engines/postgres.py +++ b/synapse/storage/engines/postgres.py @@ -28,7 +28,8 @@ class PostgresEngine(object): rows = txn.fetchall() if rows and rows[0][0] != "UTF8": raise IncorrectDatabaseSetup( - "Database has incorrect encoding: '%s' instead of 'UTF8'" + "Database has incorrect encoding: '%s' instead of 'UTF8'\n" + "See docs/postgres.rst for more information." % (rows[0][0],) ) -- cgit 1.4.1