Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

fix(db): quote orphan migration table names (#7149)

LearningCircuit3cd4d783ca0f
diff --git a/changelog.d/+alembic-orphan-identifiers.bugfix.md b/changelog.d/+alembic-orphan-identifiers.bugfix.md
new file mode 100644
index 0000000000..21d909bc1a
--- /dev/null
+++ b/changelog.d/+alembic-orphan-identifiers.bugfix.md
@@ -0,0 +1 @@
+Quote SQLite catalog table names when cleaning orphaned Alembic temporary tables so an unusual name cannot abort a database upgrade.
diff --git a/src/local_deep_research/database/alembic_runner.py b/src/local_deep_research/database/alembic_runner.py
index 32e3aaf733..c3f6031224 100644
--- a/src/local_deep_research/database/alembic_runner.py
+++ b/src/local_deep_research/database/alembic_runner.py
@@ -256,12 +256,13 @@ def _drop_orphan_alembic_temp_tables(conn: Connection) -> None:
f"prior failed migration: {sorted(temp_tables)}. Dropping before retry."
)
for name in temp_tables:
- # Identifier is constrained to the ``_alembic_tmp_`` prefix + a
- # parent table name from ``inspector.get_table_names()``; both
- # come from the database's own catalog and cannot contain
- # injection vectors.
+ # The catalog can contain an embedded quote even with this prefix.
+ # Use the dialect's identifier quoting, which doubles that quote,
+ # before interpolating a name into DDL (identifiers cannot be bound).
+ quoted_name = conn.dialect.identifier_preparer.quote_identifier(name)
# bearer:disable python_lang_sql_injection
- conn.exec_driver_sql(f'DROP TABLE IF EXISTS "{name}"') # noqa: S608
+ # nosemgrep: semgrep.rules.sql-string-concatenation, reason: dialect quote_identifier escapes the catalog name before SQL interpolation
+ conn.exec_driver_sql(f"DROP TABLE IF EXISTS {quoted_name}") # noqa: S608
def _disable_fk_for_migration(conn: Connection) -> None:
diff --git a/tests/database/test_alembic_migrations.py b/tests/database/test_alembic_migrations.py
index dd134fdb95..2b6944f166 100644
--- a/tests/database/test_alembic_migrations.py
+++ b/tests/database/test_alembic_migrations.py
@@ -5059,6 +5059,29 @@ class TestOrphanAlembicTempTableCleanup:
transaction.
"""
+ def test_drop_orphan_temp_tables_quotes_catalog_identifiers(self):
+ """An unusual catalog name must not break cleanup or touch a peer."""
+ from local_deep_research.database.alembic_runner import (
+ _drop_orphan_alembic_temp_tables,
+ )
+
+ orphan_name = '_alembic_tmp_a"; DROP TABLE sentinel; --'
+ engine = create_engine("sqlite://")
+ try:
+ with engine.begin() as conn:
+ conn.exec_driver_sql("CREATE TABLE sentinel (id INTEGER)")
+ conn.exec_driver_sql(
+ 'CREATE TABLE "_alembic_tmp_a""; DROP TABLE sentinel; --" '
+ "(id INTEGER)"
+ )
+ assert orphan_name in inspect(conn).get_table_names()
+
+ _drop_orphan_alembic_temp_tables(conn)
+
+ assert inspect(conn).get_table_names() == ["sentinel"]
+ finally:
+ engine.dispose()
+
@pytest.fixture
def db_with_orphan_temp_table(self, tmp_path):
"""Build a buggy v1.6.x DB at revision 0005 with an orphan