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(migration): recreate idx_papers_* indexes on rerun with existing table (#3614)

LearningCircuit4144a738c85e
diff --git a/src/local_deep_research/database/migrations/versions/0006_journal_quality_system.py b/src/local_deep_research/database/migrations/versions/0006_journal_quality_system.py
index 033558bdaf..3e7048db15 100644
--- a/src/local_deep_research/database/migrations/versions/0006_journal_quality_system.py
+++ b/src/local_deep_research/database/migrations/versions/0006_journal_quality_system.py
@@ -150,14 +150,22 @@ def upgrade() -> None:
sa.Column("created_at", UtcDateTime(), nullable=False),
sa.Column("updated_at", UtcDateTime(), nullable=False),
)
- # doi / arxiv_id / pmid each carry UNIQUE, which already creates a
- # backing unique index — no separate non-unique index needed.
- op.create_index("idx_papers_journal", "papers", ["journal_id"])
+ # Index creation runs unconditionally (guarded by _index_exists) so a
+ # rerun where ``papers`` already exists but is missing one of the
+ # named indexes — e.g. a partially-applied prior migration or a DB
+ # that was manually patched — still converges to the full schema.
+ # doi / arxiv_id / pmid each carry UNIQUE, which already creates a
+ # backing unique index — no separate non-unique index needed.
+ if _table_exists("papers"):
+ if not _index_exists("papers", "idx_papers_journal"):
+ op.create_index("idx_papers_journal", "papers", ["journal_id"])
# container_title is the dashboard's GROUP BY key — indexed.
- op.create_index(
- "idx_papers_container_title", "papers", ["container_title"]
- )
- op.create_index("idx_papers_year", "papers", ["year"])
+ if not _index_exists("papers", "idx_papers_container_title"):
+ op.create_index(
+ "idx_papers_container_title", "papers", ["container_title"]
+ )
+ if not _index_exists("papers", "idx_papers_year"):
+ op.create_index("idx_papers_year", "papers", ["year"])
# --- paper_appearances table (links papers to research resources) ---
if not _table_exists("paper_appearances"):
diff --git a/tests/database/test_migration_0006.py b/tests/database/test_migration_0006.py
index 66e50d93af..c9b07ba288 100644
--- a/tests/database/test_migration_0006.py
+++ b/tests/database/test_migration_0006.py
@@ -531,6 +531,40 @@ class TestMigration0006Idempotency:
run_migrations(fresh_engine)
assert get_head_revision() == "0006"
+ def test_rerun_recreates_dropped_papers_indexes(
+ self, migrated_to_0005_engine
+ ):
+ """Regression: if ``papers`` exists but one of its named indexes
+ was dropped (partial migration, manual intervention, etc.), a
+ rerun of 0006 must recreate the missing index rather than
+ silently skip it. Prior behaviour gated all three
+ ``idx_papers_*`` creations on ``if not _table_exists('papers')``,
+ so the rerun path could never converge.
+ """
+ engine = migrated_to_0005_engine
+ _run_upgrade_to(engine, "0006")
+
+ # Simulate drift: drop one of the named indexes.
+ with engine.begin() as conn:
+ conn.execute(text("DROP INDEX idx_papers_year"))
+ assert "idx_papers_year" not in _get_indexes_by_name(engine, "papers")
+
+ # Stamp back to 0005 without touching the schema, then re-upgrade.
+ # The table still exists, so the rerun must hit the index-guard path.
+ config = get_alembic_config(engine)
+ with engine.begin() as conn:
+ config.attributes["connection"] = conn
+ command.stamp(config, "0005")
+ _run_upgrade_to(engine, "0006")
+
+ indexes = _get_indexes_by_name(engine, "papers")
+ assert "idx_papers_year" in indexes
+ assert indexes["idx_papers_year"] == ["year"]
+ # The other two indexes must also still be present (not re-created
+ # twice, not dropped).
+ assert "idx_papers_journal" in indexes
+ assert "idx_papers_container_title" in indexes
+
class TestMigration0006Roundtrip:
"""Upgrade 0005 → 0006 → downgrade → upgrade again.