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.

test(migrations): pin invariants from PR #4000 multi-round review (#4033)

LearningCircuitb20786c62cfa
diff --git a/tests/database/test_alembic_migrations.py b/tests/database/test_alembic_migrations.py
index bf659a92a0..c86de225b2 100644
--- a/tests/database/test_alembic_migrations.py
+++ b/tests/database/test_alembic_migrations.py
@@ -234,12 +234,30 @@ class TestAlembicRunner:
# Second call must short-circuit — command.upgrade() should not run,
# and the short-circuit log line must be emitted (positive signal
# that the guard fired, not that the function was gutted).
- with patch(
- "local_deep_research.database.alembic_runner.command.upgrade"
- ) as mock_upgrade:
+ #
+ # Also pins that the orphan-cleanup and FK toggle introduced by
+ # PR #4000 are NOT invoked on the short-circuit path. If the
+ # guard is moved BELOW engine.connect() + _disable_fk_for_migration
+ # in a future refactor, this test fails — the existing
+ # command.upgrade mock alone would not catch that regression.
+ with (
+ patch(
+ "local_deep_research.database.alembic_runner.command.upgrade"
+ ) as mock_upgrade,
+ patch(
+ "local_deep_research.database.alembic_runner."
+ "_drop_orphan_alembic_temp_tables"
+ ) as mock_drop_orphans,
+ patch(
+ "local_deep_research.database.alembic_runner."
+ "_disable_fk_for_migration"
+ ) as mock_disable_fk,
+ ):
with loguru_caplog.at_level("INFO"):
run_migrations(fresh_engine)
assert mock_upgrade.call_count == 0
+ assert mock_drop_orphans.call_count == 0
+ assert mock_disable_fk.call_count == 0
assert "skipping upgrade" in loguru_caplog.text
def test_run_migrations_runs_upgrade_on_fresh_db(self, tmp_path):
@@ -4958,6 +4976,155 @@ class TestOrphanAlembicTempTableCleanup:
}
assert not any(t.startswith("_alembic_tmp_") for t in tables_after)
+ @pytest.fixture
+ def db_with_multiple_orphan_temp_tables(self, tmp_path):
+ """Buggy v1.6.x DB at revision 0005 with THREE orphan
+ ``_alembic_tmp_*`` tables — exercises the loop in
+ ``_drop_orphan_alembic_temp_tables`` past index 0. A regression
+ that replaces the iteration body with a single ``break`` (or
+ otherwise short-circuits) would leave the trailing orphans
+ behind and fail this test, while ``test_run_migrations_drops_orphan_temp_table``
+ with its single orphan would still pass."""
+ import sqlite3
+
+ db_path = tmp_path / "with_multi_orphans.db"
+ raw = sqlite3.connect(db_path)
+ raw.execute("PRAGMA foreign_keys = OFF")
+ raw.executescript(
+ """
+ CREATE TABLE alembic_version (version_num VARCHAR(32) PRIMARY KEY);
+ INSERT INTO alembic_version VALUES ('0005');
+ CREATE TABLE download_tracker (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ url TEXT, url_hash VARCHAR(64) NOT NULL,
+ first_resource_id INTEGER, is_downloaded BOOLEAN,
+ file_hash VARCHAR(64), file_path TEXT, file_name VARCHAR(255),
+ file_size INTEGER, is_accessible BOOLEAN,
+ first_seen TIMESTAMP, downloaded_at TIMESTAMP,
+ last_checked TIMESTAMP, library_document_id INTEGER
+ );
+ CREATE TABLE download_attempts (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ url_hash VARCHAR(64) NOT NULL,
+ attempt_number INTEGER, succeeded BOOLEAN, attempted_at TIMESTAMP,
+ FOREIGN KEY (url_hash) REFERENCES download_tracker(url_hash)
+ );
+ CREATE TABLE download_duplicates (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ url_hash VARCHAR(64) NOT NULL,
+ resource_id INTEGER, research_id VARCHAR(36), added_at TIMESTAMP,
+ FOREIGN KEY (url_hash) REFERENCES download_tracker(url_hash)
+ );
+ CREATE TABLE journals (
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
+ name VARCHAR(500) NOT NULL,
+ quality INTEGER,
+ quality_analysis_time TIMESTAMP
+ );
+ CREATE TABLE _alembic_tmp_journals (
+ id INTEGER PRIMARY KEY, stale_marker TEXT
+ );
+ CREATE TABLE _alembic_tmp_research_history (
+ id INTEGER PRIMARY KEY, stale_marker TEXT
+ );
+ CREATE TABLE _alembic_tmp_settings (
+ id INTEGER PRIMARY KEY, stale_marker TEXT
+ );
+ """
+ )
+ raw.commit()
+ raw.close()
+
+ def _create_conn():
+ conn = sqlite3.connect(
+ str(db_path), isolation_level="", check_same_thread=False
+ )
+ conn.execute("PRAGMA foreign_keys = ON")
+ return conn
+
+ engine = create_engine("sqlite://", creator=_create_conn)
+ yield engine
+ engine.dispose()
+
+ def test_run_migrations_drops_multiple_orphan_temp_tables(
+ self, db_with_multiple_orphan_temp_tables
+ ):
+ """The cleanup must process every match — not just the first."""
+ with db_with_multiple_orphan_temp_tables.connect() as conn:
+ seeded = {
+ r[0]
+ for r in conn.execute(
+ text(
+ "SELECT name FROM sqlite_master "
+ "WHERE type='table' AND name LIKE '_alembic_tmp_%'"
+ )
+ ).fetchall()
+ }
+ assert seeded == {
+ "_alembic_tmp_journals",
+ "_alembic_tmp_research_history",
+ "_alembic_tmp_settings",
+ }
+
+ run_migrations(db_with_multiple_orphan_temp_tables)
+
+ assert (
+ get_current_revision(db_with_multiple_orphan_temp_tables)
+ == get_head_revision()
+ )
+ with db_with_multiple_orphan_temp_tables.connect() as conn:
+ remaining = [
+ r[0]
+ for r in conn.execute(
+ text(
+ "SELECT name FROM sqlite_master "
+ "WHERE type='table' AND name LIKE '_alembic_tmp_%'"
+ )
+ ).fetchall()
+ ]
+ assert remaining == []
+
+ def test_drop_orphan_temp_tables_no_op_when_none_present(
+ self, tmp_path, loguru_caplog
+ ):
+ """Unit test on the cleanup helper itself: when the DB has no
+ ``_alembic_tmp_*`` tables, the function must return without
+ DDL and without emitting the ``Found N orphan alembic temp
+ table(s)`` warning. This pins the early-return guard at the
+ top of ``_drop_orphan_alembic_temp_tables`` so a future
+ refactor that drops the guard (e.g. unconditional logging)
+ would be caught immediately."""
+ from local_deep_research.database.alembic_runner import (
+ _drop_orphan_alembic_temp_tables,
+ )
+
+ db_path = tmp_path / "clean.db"
+ engine = create_engine(f"sqlite:///{db_path}")
+ try:
+ with engine.connect() as conn:
+ conn.exec_driver_sql(
+ "CREATE TABLE real_table (id INTEGER PRIMARY KEY)"
+ )
+ with loguru_caplog.at_level("WARNING"):
+ loguru_caplog.clear()
+ _drop_orphan_alembic_temp_tables(conn)
+ assert (
+ "orphan alembic temp table"
+ not in loguru_caplog.text.lower()
+ )
+ # The real table is still there — we didn't touch anything.
+ tables = {
+ r[0]
+ for r in conn.execute(
+ text(
+ "SELECT name FROM sqlite_master WHERE type='table'"
+ )
+ ).fetchall()
+ }
+ assert "real_table" in tables
+ finally:
+ engine.dispose()
+
class TestPreAlembicDatabaseHotfix:
"""Regression tests for bug #3747 — pre-Alembic DB login failure.