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

Commit 322ccac

Browse files
authored
Allow schema deltas to be engine-specific (#5911)
* Allow schema deltas to be engine-specific Signed-off-by: Olivier Wilkinson (reivilibre) <[email protected]> * Newsfile Signed-off-by: Olivier Wilkinson (reivilibre) <[email protected]> * Code style (Black) Signed-off-by: Olivier Wilkinson (reivilibre) <[email protected]>
2 parents ccb15a5 + d1e0b91 commit 322ccac

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

changelog.d/5911.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support for database engine-specific schema deltas, based on file extension.

synapse/storage/prepare_database.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ def _upgrade_existing_database(
238238

239239
logger.debug("applied_delta_files: %s", applied_delta_files)
240240

241+
if isinstance(database_engine, PostgresEngine):
242+
specific_engine_extension = ".postgres"
243+
else:
244+
specific_engine_extension = ".sqlite"
245+
246+
specific_engine_extensions = (".sqlite", ".postgres")
247+
241248
for v in range(start_ver, SCHEMA_VERSION + 1):
242249
logger.info("Upgrading schema to v%d", v)
243250

@@ -274,31 +281,38 @@ def _upgrade_existing_database(
274281
# Sometimes .pyc files turn up anyway even though we've
275282
# disabled their generation; e.g. from distribution package
276283
# installers. Silently skip it
277-
pass
284+
continue
278285
elif ext == ".sql":
279286
# A plain old .sql file, just read and execute it
280287
logger.info("Applying schema %s", relative_path)
281288
executescript(cur, absolute_path)
289+
elif ext == specific_engine_extension and root_name.endswith(".sql"):
290+
# A .sql file specific to our engine; just read and execute it
291+
logger.info("Applying engine-specific schema %s", relative_path)
292+
executescript(cur, absolute_path)
293+
elif ext in specific_engine_extensions and root_name.endswith(".sql"):
294+
# A .sql file for a different engine; skip it.
295+
continue
282296
else:
283297
# Not a valid delta file.
284-
logger.warn(
285-
"Found directory entry that did not end in .py or" " .sql: %s",
298+
logger.warning(
299+
"Found directory entry that did not end in .py or .sql: %s",
286300
relative_path,
287301
)
288302
continue
289303

290304
# Mark as done.
291305
cur.execute(
292306
database_engine.convert_param_style(
293-
"INSERT INTO applied_schema_deltas (version, file)" " VALUES (?,?)"
307+
"INSERT INTO applied_schema_deltas (version, file) VALUES (?,?)"
294308
),
295309
(v, relative_path),
296310
)
297311

298312
cur.execute("DELETE FROM schema_version")
299313
cur.execute(
300314
database_engine.convert_param_style(
301-
"INSERT INTO schema_version (version, upgraded)" " VALUES (?,?)"
315+
"INSERT INTO schema_version (version, upgraded) VALUES (?,?)"
302316
),
303317
(v, True),
304318
)

0 commit comments

Comments
 (0)