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.

9.0 KB
Raw Blame
"""Upgrade existing collections without inventing an unfinished rebuild."""

import shutil
from datetime import UTC, datetime
from importlib import import_module

import pytest
from alembic import command
from alembic.migration import MigrationContext
from alembic.operations import Operations
from alembic.script import ScriptDirectory
from sqlalchemy import (
    Column,
    ForeignKey,
    MetaData,
    String,
    Table,
    create_engine,
    event,
    inspect,
    select,
    text,
)
from sqlalchemy.orm import Session
from sqlalchemy_utc import UtcDateTime

from local_deep_research.database.alembic_runner import get_alembic_config
from local_deep_research.database.models.library import Collection


MIGRATION = import_module(
    "local_deep_research.database.migrations.versions."
    "0033_collection_rebuild_notice"
)


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


def _run_upgrade_body(engine):
    """Exercise the revision even when the version already says 0033."""
    with engine.begin() as connection:
        with Operations.context(MigrationContext.configure(connection)):
            MIGRATION.upgrade()


@pytest.fixture(scope="module")
def database_at_0032(tmp_path_factory):
    database = tmp_path_factory.mktemp("migration_0033") / "before.db"
    engine = create_engine(f"sqlite:///{database}")
    try:
        _migrate(engine, "0032")
        # 0001 creates tables from today's ORM. Remove only the new column
        # to reproduce the collections table an existing 0032 DB has.
        with engine.begin() as connection:
            operations = Operations(MigrationContext.configure(connection))
            with operations.batch_alter_table("collections") as batch:
                batch.drop_column("rebuild_pending_since")
            connection.execute(
                text(
                    "INSERT INTO collections "
                    "(id, name, description, embedding_model, created_at, "
                    "updated_at) VALUES (:id, :name, :description, :model, "
                    ":created, :created)"
                ),
                {
                    "id": "existing",
                    "name": "Existing collection",
                    "description": "Retain this description",
                    "model": "previous-model",
                    "created": "2026-09-01 12:00:00",
                },
            )
        assert "rebuild_pending_since" not in {
            column["name"]
            for column in inspect(engine).get_columns("collections")
        }
    finally:
        engine.dispose()
    return database


@pytest.fixture
def engine_at_0032(database_at_0032, tmp_path):
    database = tmp_path / "test.db"
    shutil.copyfile(database_at_0032, database)
    engine = create_engine(f"sqlite:///{database}")
    try:
        yield engine
    finally:
        engine.dispose()


def test_upgrade_existing_collection_preserves_data_without_a_warning(
    engine_at_0032,
):
    engine = engine_at_0032
    _migrate(engine, "0033")

    with Session(engine) as session:
        collection = session.get(Collection, "existing")
        assert collection.name == "Existing collection"
        assert collection.description == "Retain this description"
        assert collection.embedding_model == "previous-model"
        assert collection.rebuild_pending_since is None
        assert collection.created_at == datetime(2026, 9, 1, 12, tzinfo=UTC)
        pending_since = datetime(2026, 10, 18, 12, 30, tzinfo=UTC)
        collection.rebuild_pending_since = pending_since
        session.commit()

    # Retrying both the runner and the actual migration body preserves a
    # live notice instead of clearing it or attempting a duplicate column.
    _migrate(engine, "0033")
    _run_upgrade_body(engine)
    with Session(engine) as session:
        assert (
            session.get(Collection, "existing").rebuild_pending_since
            == pending_since
        )


def test_fresh_database_upgrade_accepts_the_orm_column_and_one_chain():
    engine = create_engine("sqlite:///:memory:")
    try:
        script = ScriptDirectory.from_config(get_alembic_config(engine))
        assert script.get_revision("0033").down_revision == "0032"
        assert len(script.get_heads()) == 1
        _migrate(engine, "0033")
        _run_upgrade_body(engine)
        with Session(engine) as session:
            session.add(Collection(id="fresh", name="Fresh collection"))
            session.commit()
            assert (
                session.get(Collection, "fresh").rebuild_pending_since is None
            )
    finally:
        engine.dispose()


def test_downgrade_preserves_collection_data_and_can_upgrade_again(
    engine_at_0032,
):
    engine = engine_at_0032
    _migrate(engine, "0033")
    with Session(engine) as session:
        session.get(Collection, "existing").rebuild_pending_since = datetime(
            2026, 10, 18, tzinfo=UTC
        )
        session.commit()

    _migrate(engine, "0032", downgrade=True)
    assert "rebuild_pending_since" not in {
        column["name"] for column in inspect(engine).get_columns("collections")
    }
    with engine.connect() as connection:
        assert connection.execute(
            text(
                "SELECT id, name, description, embedding_model FROM collections"
            )
        ).one() == (
            "existing",
            "Existing collection",
            "Retain this description",
            "previous-model",
        )
    _migrate(engine, "0033")
    with Session(engine) as session:
        assert session.get(Collection, "existing").rebuild_pending_since is None


@pytest.mark.parametrize(
    "encrypted", [False, True], ids=["sqlite", "sqlcipher"]
)
def test_downgrade_preserves_dependent_rows_with_foreign_keys_enabled(
    tmp_path, encrypted
):
    """Dropping the notice must not recreate and cascade-delete collections."""
    engine_kwargs = {}
    if encrypted:
        from local_deep_research.database.sqlcipher_compat import (
            get_sqlcipher_module,
        )

        engine_kwargs["module"] = get_sqlcipher_module().dbapi2
    database = tmp_path / "foreign_keys.db"
    engine = create_engine(f"sqlite:///{database}", **engine_kwargs)

    @event.listens_for(engine, "connect")
    def configure_connection(connection, _record):
        cursor = connection.cursor()
        try:
            if encrypted:
                cursor.execute(
                    "PRAGMA key = 'migration-test-password'"
                )  # gitleaks:allow
                cursor.execute("PRAGMA kdf_iter = 1000")
            cursor.execute("PRAGMA foreign_keys=ON")
        finally:
            cursor.close()

    metadata = MetaData()
    collections = Table(
        "collections",
        metadata,
        Column("id", String, primary_key=True),
        Column("name", String, nullable=False),
        Column("rebuild_pending_since", UtcDateTime(), nullable=True),
    )
    document_collections = Table(
        "document_collections",
        metadata,
        Column("id", String, primary_key=True),
        Column(
            "collection_id",
            String,
            ForeignKey("collections.id", ondelete="CASCADE"),
            nullable=False,
        ),
    )
    try:
        with engine.begin() as connection:
            assert (
                connection.exec_driver_sql("PRAGMA foreign_keys").scalar() == 1
            )
            metadata.create_all(connection)
            connection.execute(
                collections.insert(),
                {
                    "id": "existing",
                    "name": "Existing collection",
                    "rebuild_pending_since": datetime(2026, 10, 18, tzinfo=UTC),
                },
            )
            connection.execute(
                document_collections.insert(),
                {"id": "membership", "collection_id": "existing"},
            )

            with Operations.context(MigrationContext.configure(connection)):
                MIGRATION.downgrade()

            assert "rebuild_pending_since" not in {
                column["name"]
                for column in inspect(connection).get_columns("collections")
            }
            assert connection.execute(
                select(collections.c.id, collections.c.name)
            ).one() == ("existing", "Existing collection")
            assert connection.execute(
                select(
                    document_collections.c.id,
                    document_collections.c.collection_id,
                )
            ).one() == ("membership", "existing")
            assert (
                connection.exec_driver_sql("PRAGMA foreign_keys").scalar() == 1
            )
            assert not connection.exec_driver_sql(
                "PRAGMA foreign_key_check"
            ).all()

        if encrypted:
            assert not database.read_bytes().startswith(b"SQLite format 3")
    finally:
        engine.dispose()