Skip to content
Snippets Groups Projects
Unverified Commit a63d4cc9 authored by Sean Quah's avatar Sean Quah Committed by GitHub
Browse files

Make sqlite database migrations transactional again (#14910)


#13873 introduced a regression which causes sqlite database migrations
to no longer run inside a transaction. Wrap them in a transaction again,
to avoid database corruption when migrations are interrupted.

Fixes #14909.

Signed-off-by: default avatarSean Quah <seanq@matrix.org>
parent b15f0758
No related branches found
No related tags found
No related merge requests found
Fix a regression introduced in Synapse 1.69.0 which can result in database corruption when database migrations are interrupted on sqlite.
...@@ -132,6 +132,9 @@ class BaseDatabaseEngine(Generic[ConnectionType, CursorType], metaclass=abc.ABCM ...@@ -132,6 +132,9 @@ class BaseDatabaseEngine(Generic[ConnectionType, CursorType], metaclass=abc.ABCM
"""Execute a chunk of SQL containing multiple semicolon-delimited statements. """Execute a chunk of SQL containing multiple semicolon-delimited statements.
This is not provided by DBAPI2, and so needs engine-specific support. This is not provided by DBAPI2, and so needs engine-specific support.
Some database engines may automatically COMMIT the ongoing transaction both
before and after executing the script.
""" """
... ...
......
...@@ -135,13 +135,14 @@ class Sqlite3Engine(BaseDatabaseEngine[sqlite3.Connection, sqlite3.Cursor]): ...@@ -135,13 +135,14 @@ class Sqlite3Engine(BaseDatabaseEngine[sqlite3.Connection, sqlite3.Cursor]):
> than one statement with it, it will raise a Warning. Use executescript() if > than one statement with it, it will raise a Warning. Use executescript() if
> you want to execute multiple SQL statements with one call. > you want to execute multiple SQL statements with one call.
Though the docs for `executescript` warn: The script is wrapped in transaction control statemnets, since the docs for
`executescript` warn:
> If there is a pending transaction, an implicit COMMIT statement is executed > If there is a pending transaction, an implicit COMMIT statement is executed
> first. No other implicit transaction control is performed; any transaction > first. No other implicit transaction control is performed; any transaction
> control must be added to sql_script. > control must be added to sql_script.
""" """
cursor.executescript(script) cursor.executescript(f"BEGIN TRANSACTION;\n{script}\nCOMMIT;")
# Following functions taken from: https://github.com/coleifer/peewee # Following functions taken from: https://github.com/coleifer/peewee
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment