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.

3.7 KB
Raw Blame
"""Tests for migration 0026's ordered app_logs index."""

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

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

INDEX_NAME = "ix_app_logs_research_id_timestamp_id"
TABLE_NAME = "app_logs"


def _migrate(engine, revision, *, downgrade=False):
    config = get_alembic_config(engine)
    with engine.begin() as conn:
        config.attributes["connection"] = conn
        (command.downgrade if downgrade else command.upgrade)(config, revision)


def _index_names(engine):
    inspector = inspect(engine)
    if not inspector.has_table(TABLE_NAME):
        return set()
    return {
        index["name"]
        for index in inspector.get_indexes(TABLE_NAME)
        if index["name"]
    }


@pytest.fixture
def legacy_engine(tmp_path):
    """Simulate a database stamped at 0025 without the model-level index."""
    engine = create_engine(f"sqlite:///{tmp_path / 'pre_0026.db'}")
    with engine.begin() as conn:
        conn.execute(
            text(
                "CREATE TABLE app_logs ("
                "id INTEGER PRIMARY KEY AUTOINCREMENT, "
                "research_id VARCHAR(36), "
                "timestamp TEXT NOT NULL, "
                "message TEXT NOT NULL"
                ")"
            )
        )
        conn.execute(
            text(
                "CREATE INDEX ix_app_logs_research_id ON app_logs (research_id)"
            )
        )
        conn.execute(
            text(
                "INSERT INTO app_logs "
                "(research_id, timestamp, message) "
                "VALUES ('research-1', '2026-01-01T00:00:00+00:00', 'log')"
            )
        )
    stamp_database(engine, "0025")
    yield engine
    engine.dispose()


def test_upgrade_adds_ordered_log_index(legacy_engine):
    assert INDEX_NAME not in _index_names(legacy_engine)
    _migrate(legacy_engine, "0026")
    assert INDEX_NAME in _index_names(legacy_engine)

    index = next(
        index
        for index in inspect(legacy_engine).get_indexes(TABLE_NAME)
        if index["name"] == INDEX_NAME
    )
    assert index["column_names"] == ["research_id", "timestamp", "id"]


def test_upgrade_preserves_existing_logs(legacy_engine):
    _migrate(legacy_engine, "0026")
    with legacy_engine.begin() as conn:
        row = conn.execute(
            text("SELECT research_id, message FROM app_logs")
        ).one()
    assert row == ("research-1", "log")


def test_upgrade_is_idempotent(legacy_engine):
    _migrate(legacy_engine, "0026")
    _migrate(legacy_engine, "0026")
    assert INDEX_NAME in _index_names(legacy_engine)


def test_upgrade_without_app_logs_is_noop(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'no_logs.db'}")
    try:
        stamp_database(engine, "0025")
        _migrate(engine, "0026")
        assert not inspect(engine).has_table(TABLE_NAME)
    finally:
        engine.dispose()


def test_downgrade_removes_only_composite_index(legacy_engine):
    _migrate(legacy_engine, "0026")
    _migrate(legacy_engine, "0025", downgrade=True)

    names = _index_names(legacy_engine)
    assert INDEX_NAME not in names
    assert "ix_app_logs_research_id" in names
    with legacy_engine.begin() as conn:
        assert conn.execute(text("SELECT COUNT(*) FROM app_logs")).scalar() == 1


def test_0026_chains_to_0025():
    from alembic.config import Config
    from alembic.script import ScriptDirectory

    from local_deep_research.database.alembic_runner import get_migrations_dir

    config = Config()
    config.set_main_option("script_location", str(get_migrations_dir()))
    script = ScriptDirectory.from_config(config)
    assert script.get_revision("0026").down_revision == "0025"