fix(test): skip redundant Alembic migration check for existing test databases #727
@@ -213,8 +213,13 @@ def _install_template_db_patch() -> None:
|
||||
|
||||
# Only copy template for databases that don't exist yet or are empty
|
||||
# (SQLite auto-creates a 0-byte file on first engine open).
|
||||
# If the DB already exists and is non-empty, it was either copied from
|
||||
# the template or created by a prior step — either way it is already
|
||||
# fully migrated. Skipping the Alembic check avoids redundant engine
|
||||
# creation, SQLite lock contention, and the cumulative overhead that
|
||||
# causes intermittent hangs in parallel test runs.
|
||||
if db_path.exists() and db_path.stat().st_size > 0:
|
||||
return _original_init_or_upgrade(self, **kwargs)
|
||||
return
|
||||
|
|
||||
|
||||
# Copy the template — creates a fully-migrated DB in ~1ms
|
||||
db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
Reference in New Issue
Block a user
Bug Detection — Finding 1 (Low): Replacing
_original_init_or_upgrade(self, **kwargs)with a barereturnremoves the safety net that would catch a stale template DB on subsequentinit_or_upgradecalls within the same scenario.When this matters: Only if
build/.template-migrated.dbwas cached from a previous schema version (developer adds migrations but doesn't rebuild template). The original code would have detected pending migrations on the second call and applied them.Why Low severity: The first call (which copies the template at line 226) never ran the original migration runner in either version — so this safety net was always incomplete. A stale template would cause immediate test failures, not silent data corruption.
Suggestion: Consider adding a freshness check in
_ensure_template_db()(e.g. compare the template's Alembic stamp againstScriptDirectory.get_current_head()) to proactively detect stale templates, rather than relying on the migration runner as a fallback.