Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #135 from matrix-org/erikj/postgres_charset_check
Browse files Browse the repository at this point in the history
Check that postgres database has correct charset set
  • Loading branch information
NegativeMjark committed Apr 29, 2015
2 parents 9c4ea42 + cd08641 commit 19167fd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 8 deletions.
4 changes: 4 additions & 0 deletions scripts/port_from_sqlite_to_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,10 @@ def run(self):
self.sqlite_store = Store(sqlite_db_pool, sqlite_engine)
self.postgres_store = Store(postgres_db_pool, postgres_engine)

yield self.postgres_store.execute(
postgres_engine.check_database
)

# Step 1. Set up databases.
self.progress.set_state("Preparing SQLite3")
self.setup_db(sqlite_config, sqlite_engine)
Expand Down
28 changes: 20 additions & 8 deletions synapse/app/homeserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import sys
sys.dont_write_bytecode = True

from synapse.storage.engines import create_engine
from synapse.storage.engines import create_engine, IncorrectDatabaseSetup
from synapse.storage import (
are_all_users_on_domain, UpgradeDatabaseException,
)
Expand Down Expand Up @@ -245,15 +245,27 @@ def run_startup_checks(self, db_conn, database_engine):
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:
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():
Expand Down
4 changes: 4 additions & 0 deletions synapse/storage/engines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from ._base import IncorrectDatabaseSetup
from .postgres import PostgresEngine
from .sqlite3 import Sqlite3Engine

Expand All @@ -35,3 +36,6 @@ def create_engine(name):
raise RuntimeError(
"Unsupported database engine '%s'" % (name,)
)


__all__ = ["create_engine", "IncorrectDatabaseSetup"]
18 changes: 18 additions & 0 deletions synapse/storage/engines/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Copyright 2015 OpenMarket Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


class IncorrectDatabaseSetup(RuntimeError):
pass
12 changes: 12 additions & 0 deletions synapse/storage/engines/postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,24 @@

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'\n"
"See docs/postgres.rst for more information."
% (rows[0][0],)
)

def convert_param_style(self, sql):
return sql.replace("?", "%s")

Expand Down
3 changes: 3 additions & 0 deletions synapse/storage/engines/sqlite3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class Sqlite3Engine(object):
def __init__(self, database_module):
self.module = database_module

def check_database(self, txn):
pass

def convert_param_style(self, sql):
return sql

Expand Down

0 comments on commit 19167fd

Please sign in to comment.