~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
perf(db): short-circuit run_migrations when already at head (#3485)
LearningCircuitecc19acec470
diff --git a/src/local_deep_research/database/alembic_runner.py b/src/local_deep_research/database/alembic_runner.pyindex 9b1c243882..8e83bcac33 100644--- a/src/local_deep_research/database/alembic_runner.py+++ b/src/local_deep_research/database/alembic_runner.py@@ -200,9 +200,14 @@ def run_migrations(engine: Engine, target: str = "head") -> None:Run pending migrations on a database.The initial migration is idempotent (only creates tables that don't exist),- so this function always runs migrations rather than just stamping existing+ so this function runs migrations rather than just stamping existingdatabases. This ensures any missing tables are created.+ When ``target == "head"`` and the database is already at head, the call+ short-circuits without opening a write transaction — calling+ ``command.upgrade()`` unconditionally would take a RESERVED lock under+ SQLCipher's ``isolation_level="IMMEDIATE"`` for no work.+Security validations performed before running migrations:- Migration directory path is within expected package boundary- Migration files are not world-writable@@ -232,6 +237,17 @@ def run_migrations(engine: Engine, target: str = "head") -> None:current = get_current_revision(engine)+ # Short-circuit when the database is already at head. Calling+ # command.upgrade() unconditionally opens a write transaction via+ # engine.begin() even when there is nothing to apply — with+ # isolation_level="IMMEDIATE" on SQLCipher engines that means a+ # RESERVED lock on every cold engine reopen, serialising concurrent+ # readers behind a no-op. The fresh-DB path (current is None) still+ # runs the upgrade so tables and the alembic_version row get created.+ if current is not None and current == head and target == "head":+ logger.info(f"Database already at revision {head}; skipping upgrade")+ return+if current is None:logger.warning("Database has no migration history — applying migrations "diff --git a/tests/database/test_alembic_migrations.py b/tests/database/test_alembic_migrations.pyindex 9a4d53421d..83521b3f6e 100644--- a/tests/database/test_alembic_migrations.py+++ b/tests/database/test_alembic_migrations.py@@ -219,6 +219,48 @@ class TestAlembicRunner:run_migrations(fresh_engine)assert not needs_migration(fresh_engine)+ def test_run_migrations_skips_upgrade_when_at_head(+ self, fresh_engine, loguru_caplog+ ):+ """When the database is already at head, run_migrations must not+ call command.upgrade() — that would open a no-op write transaction+ and hold a RESERVED lock under isolation_level="IMMEDIATE", blocking+ concurrent readers for no benefit.+ """+ # Migrate to head first+ run_migrations(fresh_engine)+ assert get_current_revision(fresh_engine) == get_head_revision()++ # 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:+ with loguru_caplog.at_level("INFO"):+ run_migrations(fresh_engine)+ assert mock_upgrade.call_count == 0+ assert "skipping upgrade" in loguru_caplog.text++ def test_run_migrations_runs_upgrade_on_fresh_db(self, tmp_path):+ """Fresh DB (current revision None) must NOT short-circuit;+ command.upgrade() must run so tables and alembic_version get created.+ """+ db_path = tmp_path / "fresh_for_upgrade.db"+ engine = create_engine(f"sqlite:///{db_path}")++ try:+ assert get_current_revision(engine) is None++ run_migrations(engine)++ # If the short-circuit had applied (it must not), revision+ # would still be None and alembic_version would not exist.+ assert get_current_revision(engine) == get_head_revision()+ assert "alembic_version" in inspect(engine).get_table_names()+ finally:+ engine.dispose()+def test_imports_without_errors(self):"""All migration modules import without side effects."""@@ -3644,6 +3686,26 @@ class TestCryptographicConsistency:assert isinstance(key1, bytes)+def _insert_witness_then_fail(config, target):+ """side_effect for patched ``command.upgrade``.++ Inserts a witness row through the transaction's connection, then+ raises. If ``engine.begin()``'s rollback works, the row must not+ be present after the raise — this is what makes the rollback+ tests non-tautological. A plain ``side_effect=RuntimeError(...)``+ fires before any DB write, so the post-failure "revision+ preserved" assertion would be trivially true.++ Uses DML (INSERT) rather than DDL (CREATE TABLE) because pysqlite+ does not reliably roll back DDL statements on default SQLite. The+ caller must pre-create a ``rollback_witness`` table before+ patching ``command.upgrade`` with this side_effect.+ """+ conn = config.attributes["connection"]+ conn.execute(text("INSERT INTO rollback_witness (x) VALUES (1)"))+ raise RuntimeError("simulated failure")++class TestMigrationErrorSanitization:"""Tests for migration error message sanitization."""@@ -3685,22 +3747,84 @@ class TestMigrationErrorSanitization:engine.dispose()def test_migration_raises_on_failure_and_preserves_revision(self, tmp_path):- """Failed migration must raise and leave DB at previous revision."""+ """Failed migration must raise, leave DB at previous revision,+ AND roll back any writes made inside the transaction.++ Uses a fresh DB so the short-circuit for already-at-head does not+ bypass the patched failure. A fresh DB has current revision None,+ which falls through to command.upgrade().++ The patched side_effect inserts a witness row before raising,+ so the rollback assertion is a real rollback proof — not a+ tautology.+ """db_path = tmp_path / "failure_test.db"engine = create_engine(f"sqlite:///{db_path}")try:- run_migrations(engine)+ # Pre-create the witness table outside the transaction the+ # patched call will run in. DML (INSERT) rollback is+ # reliable on default SQLite; DDL (CREATE TABLE) is not.+ with engine.begin() as conn:+ conn.execute(text("CREATE TABLE rollback_witness (x INTEGER)"))+rev_before = get_current_revision(engine)+ assert rev_before is Nonewith patch("local_deep_research.database.alembic_runner.command.upgrade",- side_effect=RuntimeError("simulated failure"),+ side_effect=_insert_witness_then_fail,):with pytest.raises(RuntimeError, match="simulated failure"):run_migrations(engine)assert get_current_revision(engine) == rev_before+ with engine.begin() as conn:+ rows = list(+ conn.execute(text("SELECT x FROM rollback_witness"))+ )+ assert rows == []+ finally:+ engine.dispose()++ def test_migration_failure_preserves_concrete_prior_revision(+ self, tmp_path+ ):+ """Failed upgrade from a concrete prior revision must roll back+ cleanly — the DB must still report that concrete revision, and+ any write made inside the transaction must be undone.++ Passes an explicit revision target (not "head") so the at-head+ short-circuit does not skip the patched upgrade call. The+ patched side_effect inserts a witness row before raising, so+ the rollback assertion actually exercises rollback.+ """+ db_path = tmp_path / "failure_concrete_test.db"+ engine = create_engine(f"sqlite:///{db_path}")++ try:+ run_migrations(engine)+ rev_before = get_current_revision(engine)+ assert rev_before is not None++ # Pre-create the witness table outside the transaction the+ # patched call will run in.+ with engine.begin() as conn:+ conn.execute(text("CREATE TABLE rollback_witness (x INTEGER)"))++ with patch(+ "local_deep_research.database.alembic_runner.command.upgrade",+ side_effect=_insert_witness_then_fail,+ ):+ with pytest.raises(RuntimeError, match="simulated failure"):+ run_migrations(engine, target=rev_before)++ assert get_current_revision(engine) == rev_before+ with engine.begin() as conn:+ rows = list(+ conn.execute(text("SELECT x FROM rollback_witness"))+ )+ assert rows == []finally:engine.dispose()