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.

7.3 KB
Raw Blame
"""Legacy ownership recovery must not guess between physical configurations."""

from alembic import command
import pytest
from sqlalchemy import create_engine, inspect, text

from local_deep_research.database.alembic_runner import (
    get_alembic_config,
    stamp_database,
)


@pytest.mark.parametrize(
    "encrypted", [False, True], ids=["sqlite", "sqlcipher"]
)
def test_upgrade_preserves_ambiguous_rows_and_downgrade_preserves_ids(
    tmp_path, encrypted
):
    kwargs = {}
    if encrypted:
        from sqlcipher3 import dbapi2

        kwargs["module"] = dbapi2

        def connect():
            connection = dbapi2.connect(str(tmp_path / "ownership.db"))
            connection.execute("PRAGMA key='test-only'")  # gitleaks:allow
            return connection

        kwargs["creator"] = connect
    engine = create_engine(f"sqlite:///{tmp_path / 'ownership.db'}", **kwargs)
    try:
        with engine.begin() as connection:
            connection.execute(
                text(
                    "CREATE TABLE document_chunks (id INTEGER PRIMARY KEY AUTOINCREMENT, collection_name TEXT, embedding_model TEXT, embedding_model_type TEXT, chunk_text TEXT)"
                )
            )
            connection.execute(
                text(
                    "CREATE TABLE rag_indices (collection_name TEXT, embedding_model TEXT, embedding_model_type TEXT, index_hash TEXT)"
                )
            )
            # Each "*-mismatch" collection has exactly one index, but for a
            # different model or provider, so it must stay NULL.
            for name, model, provider, hashes in (
                ("unique", "model", "sentence_transformers", ["one"]),
                ("ambiguous", "model", "sentence_transformers", ["old", "new"]),
                ("missing", "model", "sentence_transformers", []),
                ("model-mismatch", "other", "sentence_transformers", ["x"]),
                ("provider-mismatch", "model", "ollama", ["y"]),
            ):
                connection.execute(
                    text(
                        "INSERT INTO document_chunks (collection_name, embedding_model, embedding_model_type, chunk_text) VALUES (:name, :model, :provider, 'preserved')"
                    ),
                    {"name": name, "model": model, "provider": provider},
                )
                for value in hashes:
                    connection.execute(
                        text(
                            "INSERT INTO rag_indices VALUES (:name, 'model', 'sentence_transformers', :value)"
                        ),
                        {"name": name, "value": value},
                    )
        stamp_database(engine, "0034")
        config = get_alembic_config(engine)
        with engine.begin() as connection:
            config.attributes["connection"] = connection
            command.upgrade(config, "0042")
            values = dict(
                connection.execute(
                    text(
                        "SELECT collection_name, vector_index_key FROM document_chunks"
                    )
                ).all()
            )
            assert values == {
                "unique": "one",
                "ambiguous": None,
                "missing": None,
                "model-mismatch": None,
                "provider-mismatch": None,
            }
            assert (
                connection.execute(
                    text(
                        "SELECT COUNT(*) FROM document_chunks WHERE chunk_text='preserved'"
                    )
                ).scalar_one()
                == 5
            )
        with engine.begin() as connection:
            config.attributes["connection"] = connection
            # Delete the highest id BEFORE the downgrade rebuilds the table, so
            # a rebuilt AUTOINCREMENT counter would restart below it.
            connection.execute(text("DELETE FROM document_chunks WHERE id=5"))
            sequence = connection.execute(
                text(
                    "SELECT seq FROM sqlite_sequence WHERE name='document_chunks'"
                )
            ).scalar_one()
            assert sequence == 5
            command.downgrade(config, "0034")
            assert "vector_index_key" not in {
                column["name"]
                for column in inspect(connection).get_columns("document_chunks")
            }
            connection.execute(
                text("INSERT INTO document_chunks (chunk_text) VALUES ('new')")
            )
            assert (
                connection.execute(
                    text(
                        "SELECT id FROM document_chunks WHERE chunk_text='new'"
                    )
                ).scalar_one()
                > sequence
            )
            assert (
                connection.execute(
                    text(
                        "SELECT COUNT(*) FROM document_chunks WHERE chunk_text='preserved'"
                    )
                ).scalar_one()
                == 4
            )
    finally:
        engine.dispose()


def test_upgrade_keeps_existing_owner_and_downgrade_tolerates_missing_index(
    tmp_path,
):
    """A key already recorded is never overwritten by the backfill, and a
    column without its index (e.g. a hand-repaired database) downgrades."""
    engine = create_engine(f"sqlite:///{tmp_path / 'preset.db'}")
    try:
        with engine.begin() as connection:
            connection.execute(
                text(
                    "CREATE TABLE document_chunks (id INTEGER PRIMARY KEY AUTOINCREMENT, collection_name TEXT, embedding_model TEXT, embedding_model_type TEXT, chunk_text TEXT, vector_index_key VARCHAR(64))"
                )
            )
            connection.execute(
                text(
                    "CREATE TABLE rag_indices (collection_name TEXT, embedding_model TEXT, embedding_model_type TEXT, index_hash TEXT)"
                )
            )
            connection.execute(
                text(
                    "INSERT INTO document_chunks (collection_name, embedding_model, embedding_model_type, chunk_text, vector_index_key) VALUES ('unique', 'model', 'sentence_transformers', 'preserved', 'adopted')"
                )
            )
            connection.execute(
                text(
                    "INSERT INTO rag_indices VALUES ('unique', 'model', 'sentence_transformers', 'one')"
                )
            )
        stamp_database(engine, "0034")
        config = get_alembic_config(engine)
        with engine.begin() as connection:
            config.attributes["connection"] = connection
            command.upgrade(config, "0042")
            assert (
                connection.execute(
                    text("SELECT vector_index_key FROM document_chunks")
                ).scalar_one()
                == "adopted"
            )
        with engine.begin() as connection:
            config.attributes["connection"] = connection
            command.downgrade(config, "0034")
            assert "vector_index_key" not in {
                column["name"]
                for column in inspect(connection).get_columns("document_chunks")
            }
            assert (
                connection.execute(
                    text("SELECT chunk_text FROM document_chunks")
                ).scalar_one()
                == "preserved"
            )
    finally:
        engine.dispose()