Prepare the database whenever a connection is opened from the db_pool so that in-memory databases will work

This commit is contained in:
Mark Haines 2015-02-13 14:29:49 +00:00
parent 6375abcdac
commit 183b3d4e47
2 changed files with 7 additions and 10 deletions

View File

@ -90,7 +90,9 @@ class SynapseHomeServer(HomeServer):
"sqlite3", self.get_db_name(), "sqlite3", self.get_db_name(),
check_same_thread=False, check_same_thread=False,
cp_min=1, cp_min=1,
cp_max=1 cp_max=1,
cp_openfun=prepare_database, # Prepare the database for each conn
# so that :memory: sqlite works
) )
def create_resource_tree(self, web_client, redirect_root_to_web_client): def create_resource_tree(self, web_client, redirect_root_to_web_client):
@ -252,14 +254,6 @@ def setup():
logger.info("Database prepared in %s.", db_name) logger.info("Database prepared in %s.", db_name)
db_pool = hs.get_db_pool()
if db_name == ":memory:":
# Memory databases will need to be setup each time they are opened.
reactor.callWhenRunning(
db_pool.runWithConnection, prepare_database
)
if config.manhole: if config.manhole:
f = twisted.manhole.telnet.ShellFactory() f = twisted.manhole.telnet.ShellFactory()
f.username = "matrix" f.username = "matrix"
@ -270,10 +264,10 @@ def setup():
bind_port = config.bind_port bind_port = config.bind_port
if config.no_tls: if config.no_tls:
bind_port = None bind_port = None
hs.start_listening(bind_port, config.unsecure_port) hs.start_listening(bind_port, config.unsecure_port)
hs.get_pusherpool().start() hs.get_pusherpool().start()
hs.get_state_handler().start_caching() hs.get_state_handler().start_caching()
hs.get_datastore().start_profiling() hs.get_datastore().start_profiling()

View File

@ -637,10 +637,13 @@ def prepare_database(db_conn):
c.executescript(sql_script) c.executescript(sql_script)
db_conn.commit() db_conn.commit()
else:
logger.info("Database is at version %r", user_version)
else: else:
sql_script = "BEGIN TRANSACTION;\n" sql_script = "BEGIN TRANSACTION;\n"
for sql_loc in SCHEMAS: for sql_loc in SCHEMAS:
logger.debug("Applying schema %r", sql_loc)
sql_script += read_schema(sql_loc) sql_script += read_schema(sql_loc)
sql_script += "\n" sql_script += "\n"
sql_script += "COMMIT TRANSACTION;" sql_script += "COMMIT TRANSACTION;"