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.0 KB
Raw Blame
from sqlalchemy import create_engine, inspect, text
from sqlalchemy.exc import IntegrityError
from alembic import command
import pytest

from local_deep_research.database.alembic_runner import (
    get_alembic_config,
    get_migrations_dir,
    stamp_database,
)
from local_deep_research.database.models.library import RAGIndex


_TABLE = "rag_indices"
_CONSTRAINT = "uix_collection_model"


def _create_rag_indices(engine, *, includes_constraint: bool) -> None:
    constraint = (
        ", CONSTRAINT uix_collection_model UNIQUE "
        "(collection_name, embedding_model, embedding_model_type)"
        if includes_constraint
        else ""
    )
    with engine.begin() as connection:
        connection.execute(
            text(
                "CREATE TABLE rag_indices ("
                "id INTEGER PRIMARY KEY, "
                "collection_name VARCHAR NOT NULL, "
                "embedding_model VARCHAR NOT NULL, "
                "embedding_model_type VARCHAR NOT NULL, "
                "index_hash VARCHAR NOT NULL UNIQUE, "
                "chunk_size INTEGER NOT NULL"
                f"{constraint}"
                ")"
            )
        )


def _upgrade(engine) -> None:
    config = get_alembic_config(engine)
    with engine.begin() as connection:
        config.attributes["connection"] = connection
        command.upgrade(config, "0028")


def _downgrade(engine) -> None:
    config = get_alembic_config(engine)
    with engine.begin() as connection:
        config.attributes["connection"] = connection
        command.downgrade(config, "0027")


def _constraint_names(engine) -> set[str]:
    return {
        constraint["name"]
        for constraint in inspect(engine).get_unique_constraints(_TABLE)
        if constraint["name"]
    }


def _insert_index(connection, *, index_hash: str, chunk_size: int) -> None:
    connection.execute(
        text(
            "INSERT INTO rag_indices "
            "(collection_name, embedding_model, embedding_model_type, index_hash, "
            "chunk_size) VALUES "
            "('collection-1', 'model', 'sentence_transformers', :index_hash, "
            ":chunk_size)"
        ),
        {"index_hash": index_hash, "chunk_size": chunk_size},
    )


def _current_revision(engine) -> str:
    with engine.connect() as connection:
        return connection.execute(
            text("SELECT version_num FROM alembic_version")
        ).scalar_one()


@pytest.fixture
def legacy_engine(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'rag_identity.db'}")
    _create_rag_indices(engine, includes_constraint=True)
    stamp_database(engine, "0027")
    yield engine
    engine.dispose()


def test_0028_chains_correctly_to_0027():
    """0028 (expand_rag_index_identity) chains directly off 0027.

    Originally this asserted ``get_head_revision() == "0028"``, but a later
    migration added 0029 (gate filesystem PDF storage) on top, so head moved
    past 0028. The substantive invariant the original test protected — that
    0028 is correctly anchored in the chain — survives by checking
    down_revision instead. Mirrors ``TestMigration0009HeadAlignment``.
    """
    from alembic.config import Config
    from alembic.script import ScriptDirectory

    config = Config()
    config.set_main_option("script_location", str(get_migrations_dir()))
    script = ScriptDirectory.from_config(config)

    assert script.get_revision("0028").down_revision == "0027"


def test_upgrade_drops_legacy_constraint_and_preserves_rows(legacy_engine):
    # Given
    with legacy_engine.begin() as connection:
        _insert_index(connection, index_hash="before-upgrade", chunk_size=100)

    # When
    _upgrade(legacy_engine)

    # Then
    assert _CONSTRAINT not in _constraint_names(legacy_engine)
    with legacy_engine.connect() as connection:
        assert (
            connection.execute(
                text("SELECT COUNT(*) FROM rag_indices")
            ).scalar_one()
            == 1
        )


def test_upgrade_allows_distinct_full_configurations(legacy_engine):
    _upgrade(legacy_engine)

    # Given
    with legacy_engine.begin() as connection:
        _insert_index(connection, index_hash="chunk-100", chunk_size=100)

        # When
        _insert_index(connection, index_hash="chunk-200", chunk_size=200)

    # Then
    with legacy_engine.connect() as connection:
        assert (
            connection.execute(
                text("SELECT COUNT(*) FROM rag_indices")
            ).scalar_one()
            == 2
        )


def test_upgrade_is_idempotent_when_constraint_is_already_absent(legacy_engine):
    # Given
    _upgrade(legacy_engine)

    # When
    _upgrade(legacy_engine)

    # Then
    assert _CONSTRAINT not in _constraint_names(legacy_engine)


def test_upgrade_skips_missing_rag_indices_table(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'missing_rag_indices.db'}")
    try:
        stamp_database(engine, "0027")

        # When
        _upgrade(engine)

        # Then
        assert not inspect(engine).has_table(_TABLE)
        assert _current_revision(engine) == "0028"
    finally:
        engine.dispose()


def test_upgrade_skips_table_without_legacy_constraint(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'no_legacy_constraint.db'}")
    try:
        _create_rag_indices(engine, includes_constraint=False)
        stamp_database(engine, "0027")

        # When
        _upgrade(engine)

        # Then
        assert _CONSTRAINT not in _constraint_names(engine)
        assert _current_revision(engine) == "0028"
    finally:
        engine.dispose()


def test_downgrade_recreates_constraint_when_data_is_lossless(legacy_engine):
    _upgrade(legacy_engine)
    with legacy_engine.begin() as connection:
        _insert_index(connection, index_hash="only-config", chunk_size=100)

    # When
    _downgrade(legacy_engine)

    # Then
    assert _CONSTRAINT in _constraint_names(legacy_engine)
    assert _current_revision(legacy_engine) == "0027"
    with pytest.raises(IntegrityError):
        with legacy_engine.begin() as connection:
            _insert_index(connection, index_hash="duplicate", chunk_size=200)


def test_downgrade_refuses_to_drop_full_configuration_rows(legacy_engine):
    _upgrade(legacy_engine)
    with legacy_engine.begin() as connection:
        _insert_index(connection, index_hash="config-one", chunk_size=100)
        _insert_index(connection, index_hash="config-two", chunk_size=200)

    # When / Then
    with pytest.raises(RuntimeError, match="Cannot downgrade past 0028"):
        _downgrade(legacy_engine)

    assert _current_revision(legacy_engine) == "0028"
    with legacy_engine.connect() as connection:
        assert (
            connection.execute(
                text("SELECT COUNT(*) FROM rag_indices")
            ).scalar_one()
            == 2
        )


def test_model_metadata_does_not_restore_legacy_constraint(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'model_metadata.db'}")
    try:
        RAGIndex.__table__.create(engine)
        assert _CONSTRAINT not in _constraint_names(engine)
    finally:
        engine.dispose()