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.

17.4 KB
Raw Blame
"""
Tests for migrations 0023 (drop uix_chunk_collection) and 0024 (document_chunks
AUTOINCREMENT).

Covers:
- 0023 downgrade REFUSES with a RuntimeError when document_chunks has
  duplicate (chunk_hash, collection_name) rows, without deleting anything
  or moving the alembic version off 0023.
- 0023 downgrade SUCCEEDS (re-adding the unique constraint) when there are
  no duplicates.
- 0024 makes document_chunks.id an AUTOINCREMENT column so ids are never
  reused after the max-id row is deleted — the invariant the faiss
  id-keyed vector store depends on to avoid cross-document text leaks.
- Migrations 0023 and 0024 form the expected 0022 -> 0023 -> 0024 chain.
"""

import uuid

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

from local_deep_research.database.alembic_runner import get_alembic_config


def _upgrade_to(engine, revision):
    config = get_alembic_config(engine)
    with engine.begin() as conn:
        config.attributes["connection"] = conn
        command.upgrade(config, revision)


def _downgrade_to(engine, revision):
    config = get_alembic_config(engine)
    with engine.begin() as conn:
        config.attributes["connection"] = conn
        command.downgrade(config, revision)


def _current_revision(engine):
    with engine.connect() as conn:
        row = conn.execute(
            text("SELECT version_num FROM alembic_version")
        ).first()
    return row[0] if row else None


def _get_unique_constraint_names(engine, table):
    insp = inspect(engine)
    if not insp.has_table(table):
        return set()
    names = set()
    for u in insp.get_unique_constraints(table):
        if u.get("name"):
            names.add(u["name"])
    return names


def _table_sql(engine, table):
    with engine.connect() as conn:
        row = conn.execute(
            text(
                "SELECT sql FROM sqlite_master WHERE type='table' AND name=:n"
            ),
            {"n": table},
        ).first()
    return (row[0] or "") if row else ""


def _insert_chunk(
    conn,
    *,
    chunk_hash="hash-a",
    collection_name="collection_x",
    chunk_text="some chunk text",
    embedding_id=None,
):
    """Insert a minimal, valid document_chunks row.

    NOT NULL columns per the model: chunk_hash, source_type,
    collection_name, chunk_text, chunk_index, start_char, end_char,
    word_count, embedding_id (unique), embedding_model,
    embedding_model_type, created_at.
    """
    conn.execute(
        text(
            "INSERT INTO document_chunks "
            "(chunk_hash, source_type, source_id, collection_name, "
            "chunk_text, chunk_index, start_char, end_char, word_count, "
            "embedding_id, embedding_model, embedding_model_type, "
            "created_at) VALUES "
            "(:chunk_hash, 'document', 'doc-1', :collection_name, "
            ":chunk_text, 0, 0, 10, 2, :embedding_id, 'test-model', "
            "'sentence_transformers', CURRENT_TIMESTAMP)"
        ),
        {
            "chunk_hash": chunk_hash,
            "collection_name": collection_name,
            "chunk_text": chunk_text,
            "embedding_id": embedding_id or str(uuid.uuid4()),
        },
    )


@pytest.fixture
def fresh_engine(tmp_path):
    db_path = tmp_path / "rag_migration_test.db"
    engine = create_engine(f"sqlite:///{db_path}")
    yield engine
    engine.dispose()


@pytest.fixture
def engine_at_0023(fresh_engine):
    _upgrade_to(fresh_engine, "0023")
    return fresh_engine


@pytest.fixture
def engine_at_0024(fresh_engine):
    _upgrade_to(fresh_engine, "0024")
    return fresh_engine


# ---------------------------------------------------------------------------
# 0023 — dropping uix_chunk_collection, and refusing to bring it back over
# duplicate data.
# ---------------------------------------------------------------------------


class TestMigration0023DropUniqueConstraint:
    def test_upgrade_drops_the_unique_constraint(self, engine_at_0023):
        names = _get_unique_constraint_names(engine_at_0023, "document_chunks")
        assert "uix_chunk_collection" not in names

    def test_upgrade_allows_duplicate_chunk_hash_collection_rows(
        self, engine_at_0023
    ):
        """Positive control: proves upgrade genuinely lifted the constraint,
        not merely that inspection doesn't see its name."""
        engine = engine_at_0023
        with engine.begin() as conn:
            _insert_chunk(
                conn, chunk_hash="dup-hash", collection_name="dup-coll"
            )
            _insert_chunk(
                conn, chunk_hash="dup-hash", collection_name="dup-coll"
            )
        with engine.connect() as conn:
            count = conn.execute(
                text(
                    "SELECT COUNT(*) FROM document_chunks "
                    "WHERE chunk_hash='dup-hash' AND collection_name='dup-coll'"
                )
            ).scalar()
        assert count == 2

    def test_downgrade_refuses_with_runtime_error_on_duplicates(
        self, engine_at_0023
    ):
        """The core safety guarantee: downgrading past 0023 while
        document_chunks holds duplicate (chunk_hash, collection_name) rows
        must raise, not silently delete a row to satisfy the constraint —
        a dropped row's chunk_text may be the only copy of that user data.
        """
        engine = engine_at_0023
        with engine.begin() as conn:
            _insert_chunk(
                conn, chunk_hash="same-hash", collection_name="same-coll"
            )
            _insert_chunk(
                conn, chunk_hash="same-hash", collection_name="same-coll"
            )

        with engine.connect() as conn:
            before_count = conn.execute(
                text("SELECT COUNT(*) FROM document_chunks")
            ).scalar()
        assert before_count == 2, "seed failed"

        with pytest.raises(RuntimeError, match="Cannot downgrade past 0023"):
            _downgrade_to(engine, "0022")

        # No rows were silently dropped to force the constraint back on.
        with engine.connect() as conn:
            after_count = conn.execute(
                text("SELECT COUNT(*) FROM document_chunks")
            ).scalar()
        assert after_count == before_count, (
            "downgrade must never delete rows to satisfy the re-added "
            "unique constraint"
        )

        # The alembic version must remain at 0023 — the failed downgrade
        # must not silently advance the recorded revision.
        assert _current_revision(engine) == "0023"

        # The table must still be usable (no stranded _alembic_tmp table
        # left over from an aborted batch rebuild).
        insp = inspect(engine)
        assert insp.has_table("document_chunks")
        assert not insp.has_table("_alembic_tmp_document_chunks")

    def test_downgrade_succeeds_without_duplicates(self, engine_at_0023):
        """Negative control: with no duplicate (chunk_hash, collection_name)
        rows, downgrade must succeed and the unique constraint must be back.
        """
        engine = engine_at_0023
        with engine.begin() as conn:
            _insert_chunk(conn, chunk_hash="hash-1", collection_name="coll-1")
            _insert_chunk(conn, chunk_hash="hash-2", collection_name="coll-1")
            _insert_chunk(conn, chunk_hash="hash-1", collection_name="coll-2")

        _downgrade_to(engine, "0022")

        assert _current_revision(engine) == "0022"
        names = _get_unique_constraint_names(engine, "document_chunks")
        assert "uix_chunk_collection" in names

        # And the constraint is genuinely enforced again: a fresh duplicate
        # insert now fails.
        with engine.connect() as conn:
            count = conn.execute(
                text("SELECT COUNT(*) FROM document_chunks")
            ).scalar()
        assert count == 3, "downgrade must not delete legitimate rows either"


# ---------------------------------------------------------------------------
# 0024 — AUTOINCREMENT on document_chunks.id: ids must never be reused.
# ---------------------------------------------------------------------------


class TestMigration0024Autoincrement:
    def test_upgrade_marks_table_autoincrement(self, engine_at_0024):
        sql = _table_sql(engine_at_0024, "document_chunks")
        assert "AUTOINCREMENT" in sql.upper()

    def test_deleted_max_id_is_never_reused(self, engine_at_0024):
        """The security invariant the faiss id-keyed vector store relies
        on: once an id has been used, it is never handed out again — even
        after the row holding the current maximum id is deleted. Without
        AUTOINCREMENT, SQLite's plain ROWID allocation recycles the freed
        max id on the next insert, which would let a later, unrelated
        chunk's vector rehydrate under a stale/removed id and leak the
        wrong document's text.
        """
        engine = engine_at_0024

        with engine.begin() as conn:
            _insert_chunk(conn, chunk_hash="h1", collection_name="c1")
            _insert_chunk(conn, chunk_hash="h2", collection_name="c1")
            _insert_chunk(conn, chunk_hash="h3", collection_name="c1")

        with engine.connect() as conn:
            ids = [
                row[0]
                for row in conn.execute(
                    text("SELECT id FROM document_chunks ORDER BY id")
                ).fetchall()
            ]
        assert len(ids) == 3
        max_id_before = max(ids)

        # Delete the row holding the current maximum id — the scenario a
        # plain ROWID table would recycle.
        with engine.begin() as conn:
            conn.execute(
                text("DELETE FROM document_chunks WHERE id = :id"),
                {"id": max_id_before},
            )

        with engine.begin() as conn:
            _insert_chunk(conn, chunk_hash="h4", collection_name="c1")

        with engine.connect() as conn:
            new_id = conn.execute(
                text("SELECT id FROM document_chunks WHERE chunk_hash='h4'")
            ).scalar()

        assert new_id > max_id_before, (
            f"id {new_id} was reused/recycled after deleting the previous "
            f"max id {max_id_before} — AUTOINCREMENT is not effective"
        )
        # Every id ever used (including the deleted one) must stay strictly
        # below every id used afterward.
        assert new_id not in ids

    def test_sanity_plain_rowid_would_have_reused_the_id(self, tmp_path):
        """Control proving the assertion above actually bites: a plain
        (non-AUTOINCREMENT) INTEGER PRIMARY KEY table DOES recycle the
        freed max id in this exact insert/delete/insert sequence, so the
        migration is doing real work and the test above isn't vacuous.
        """
        db_path = tmp_path / "plain_rowid_control.db"
        engine = create_engine(f"sqlite:///{db_path}")
        try:
            with engine.begin() as conn:
                conn.execute(
                    text(
                        "CREATE TABLE plain_ids "
                        "(id INTEGER PRIMARY KEY, val TEXT)"
                    )
                )
                conn.execute(text("INSERT INTO plain_ids (val) VALUES ('a')"))
                conn.execute(text("INSERT INTO plain_ids (val) VALUES ('b')"))
                conn.execute(text("INSERT INTO plain_ids (val) VALUES ('c')"))

            with engine.connect() as conn:
                max_id_before = conn.execute(
                    text("SELECT MAX(id) FROM plain_ids")
                ).scalar()

            with engine.begin() as conn:
                conn.execute(
                    text("DELETE FROM plain_ids WHERE id = :id"),
                    {"id": max_id_before},
                )
                conn.execute(text("INSERT INTO plain_ids (val) VALUES ('d')"))

            with engine.connect() as conn:
                new_id = conn.execute(
                    text("SELECT id FROM plain_ids WHERE val='d'")
                ).scalar()

            assert new_id == max_id_before, (
                "control assumption broken: plain ROWID table did not "
                "recycle the freed max id, so the AUTOINCREMENT test above "
                "would pass even if migration 0024 were a no-op"
            )
        finally:
            engine.dispose()

    def test_downgrade_removes_autoincrement(self, engine_at_0024):
        _downgrade_to(engine_at_0024, "0023")
        sql = _table_sql(engine_at_0024, "document_chunks")
        assert "AUTOINCREMENT" not in sql.upper()

    def test_upgrade_preserves_existing_rows(self, engine_at_0023):
        """Rows and their ids inserted before the 0024 rebuild must survive
        the batch table-recreation unchanged."""
        engine = engine_at_0023
        with engine.begin() as conn:
            _insert_chunk(conn, chunk_hash="pre-h1", collection_name="pre-c1")
            _insert_chunk(conn, chunk_hash="pre-h2", collection_name="pre-c1")

        with engine.connect() as conn:
            before = sorted(
                conn.execute(
                    text("SELECT id, chunk_hash FROM document_chunks")
                ).fetchall()
            )

        _upgrade_to(engine, "0024")

        with engine.connect() as conn:
            after = sorted(
                conn.execute(
                    text("SELECT id, chunk_hash FROM document_chunks")
                ).fetchall()
            )
        assert before == after

    def test_upgrade_downgrade_upgrade_round_trip_preserves_rows_and_reapplies_protection(
        self, engine_at_0023
    ):
        """A full 0024 -> 0023 -> 0024 rebuild cycle must be lossless, and
        the second upgrade must genuinely re-establish the id-reuse
        protection — not merely leave the schema looking right while the
        underlying SQLite AUTOINCREMENT bookkeeping was lost in the
        downgrade/upgrade round trip.
        """
        engine = engine_at_0023
        with engine.begin() as conn:
            _insert_chunk(conn, chunk_hash="rt-h1", collection_name="rt-c1")
            _insert_chunk(conn, chunk_hash="rt-h2", collection_name="rt-c1")
            _insert_chunk(conn, chunk_hash="rt-h3", collection_name="rt-c1")

        with engine.connect() as conn:
            before = sorted(
                conn.execute(
                    text("SELECT id, chunk_hash FROM document_chunks")
                ).fetchall()
            )
        assert len(before) == 3, "seed failed"

        _upgrade_to(engine, "0024")
        _downgrade_to(engine, "0023")
        _upgrade_to(engine, "0024")

        assert _current_revision(engine) == "0024"

        with engine.connect() as conn:
            after = sorted(
                conn.execute(
                    text("SELECT id, chunk_hash FROM document_chunks")
                ).fetchall()
            )
        assert before == after, (
            "the full set of (id, chunk_hash) rows must be identical after "
            "an upgrade -> downgrade -> upgrade rebuild cycle"
        )

        sql = _table_sql(engine, "document_chunks")
        assert "AUTOINCREMENT" in sql.upper(), (
            "AUTOINCREMENT must be present again after the second upgrade, "
            "not just the first"
        )

        # The id-reuse protection must be genuinely re-established by the
        # second upgrade, not just reported present in the schema text:
        # deleting the max-id row then inserting a new chunk must yield a
        # strictly greater id, never a recycled one.
        with engine.connect() as conn:
            max_id_before = conn.execute(
                text("SELECT MAX(id) FROM document_chunks")
            ).scalar()

        with engine.begin() as conn:
            conn.execute(
                text("DELETE FROM document_chunks WHERE id = :id"),
                {"id": max_id_before},
            )

        with engine.begin() as conn:
            _insert_chunk(conn, chunk_hash="rt-h4", collection_name="rt-c1")

        with engine.connect() as conn:
            new_id = conn.execute(
                text("SELECT id FROM document_chunks WHERE chunk_hash='rt-h4'")
            ).scalar()

        assert new_id > max_id_before, (
            f"id {new_id} was reused/recycled after the round trip, despite "
            f"deleting the previous max id {max_id_before} — the second "
            "upgrade did not genuinely re-establish AUTOINCREMENT protection"
        )


# ---------------------------------------------------------------------------
# Migration chain
# ---------------------------------------------------------------------------


class TestMigrationChain:
    def test_single_head_no_branches(self):
        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)
        heads = script.get_heads()
        assert len(heads) == 1, f"expected a single head, found {heads}"

    def test_0023_chains_to_0022_and_0024_chains_to_0023(self):
        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("0023").down_revision == "0022"
        assert script.get_revision("0024").down_revision == "0023"