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.

5.6 KB
Raw Blame
"""Tests for migration 0020: add the Zotero integration tables.

Verifies the migration chains correctly after 0019 and that the upgrade
creates the two Zotero tables. The head-alignment guard is NOT here: when
feat/notes-v2 was merged it re-chained note_tables (0021) and
note_references (0022) on top of this migration, so 0020 is no longer the
head — the guard lives in TestMigration0022HeadAlignment in
test_migration_0022_note_references.py.
"""

import pytest
from alembic import command
from alembic.config import Config
from alembic.script import ScriptDirectory
from sqlalchemy import create_engine, inspect, text
from sqlalchemy.exc import IntegrityError

from local_deep_research.database.alembic_runner import (
    get_alembic_config,
    get_migrations_dir,
    run_migrations,
)


def test_0020_chains_after_0019():
    config = Config()
    config.set_main_option("script_location", str(get_migrations_dir()))
    script = ScriptDirectory.from_config(config)
    rev = script.get_revision("0020")
    assert rev.down_revision == "0019"


def test_upgrade_creates_zotero_tables(tmp_path):
    engine = create_engine(f"sqlite:///{tmp_path / 'test_0020.db'}")
    run_migrations(engine)  # up to head (includes 0020)
    tables = set(inspect(engine).get_table_names())
    assert "zotero_sync_state" in tables
    assert "zotero_item_map" in tables
    engine.dispose()


def _migrated_engine(tmp_path, name="test_0020.db"):
    engine = create_engine(f"sqlite:///{tmp_path / name}")
    run_migrations(engine)
    return engine


def _run_alembic(engine, direction, target):
    """Run an alembic upgrade/downgrade, wiring the connection into the config
    the way alembic_runner.run_migrations does (this project's env.py requires
    config.attributes['connection'])."""
    config = get_alembic_config(engine)
    with engine.connect() as conn:
        with conn.begin():
            config.attributes["connection"] = conn
            getattr(command, direction)(config, target)


def test_upgrade_creates_unique_constraints(tmp_path):
    # Production per-user DBs are built PURELY from the Alembic chain (not
    # create_all), and the sync service relies on these UNIQUE constraints as
    # its race backstop. Assert the migration actually shipped them.
    engine = _migrated_engine(tmp_path)
    insp = inspect(engine)

    item_uqs = insp.get_unique_constraints("zotero_item_map")
    assert any(
        set(uq["column_names"]) == {"ldr_collection_id", "zotero_item_key"}
        for uq in item_uqs
    ), item_uqs

    state_uqs = insp.get_unique_constraints("zotero_sync_state")
    assert any(
        set(uq["column_names"])
        == {"library_type", "library_id", "collection_key"}
        for uq in state_uqs
    ), state_uqs
    engine.dispose()


def test_upgrade_creates_expected_indexes(tmp_path):
    engine = _migrated_engine(tmp_path)
    insp = inspect(engine)
    item_indexes = {ix["name"] for ix in insp.get_indexes("zotero_item_map")}
    state_indexes = {ix["name"] for ix in insp.get_indexes("zotero_sync_state")}
    assert "ix_zotero_item_map_ldr_collection_id" in item_indexes
    assert "ix_zotero_item_map_zotero_item_key" in item_indexes
    assert "ix_zotero_item_map_document_id" in item_indexes
    assert "ix_zotero_sync_state_ldr_collection_id" in state_indexes
    engine.dispose()


def test_upgrade_foreign_keys(tmp_path):
    engine = _migrated_engine(tmp_path)
    insp = inspect(engine)
    fks = {
        fk["constrained_columns"][0]: fk
        for fk in insp.get_foreign_keys("zotero_item_map")
    }
    assert fks["ldr_collection_id"]["referred_table"] == "collections"
    assert fks["document_id"]["referred_table"] == "documents"
    # ondelete: CASCADE for the owning collection, SET NULL for the document
    # (the sync service treats a null document as "needs re-import").
    assert (
        fks["ldr_collection_id"].get("options", {}).get("ondelete", "").upper()
        == "CASCADE"
    )
    assert (
        fks["document_id"].get("options", {}).get("ondelete", "").upper()
        == "SET NULL"
    )
    engine.dispose()


def test_item_map_unique_constraint_is_enforced(tmp_path):
    # Behavioral proof (against the migration-built schema) that a duplicate
    # (ldr_collection_id, zotero_item_key) is rejected.
    engine = _migrated_engine(tmp_path)
    ts = "2020-01-01 00:00:00"
    insert = text(
        "INSERT INTO zotero_item_map "
        "(ldr_collection_id, zotero_item_key, zotero_version, has_pdf, "
        "created_at, updated_at) "
        "VALUES ('c1', 'K1', :v, 0, :ts, :ts)"
    )
    with engine.begin() as conn:
        conn.execute(insert, {"v": 1, "ts": ts})
    with pytest.raises(IntegrityError):
        with engine.begin() as conn:
            conn.execute(insert, {"v": 2, "ts": ts})
    engine.dispose()


def test_downgrade_then_upgrade_roundtrip(tmp_path):
    engine = _migrated_engine(tmp_path)

    # Downgrade removes both tables...
    _run_alembic(engine, "downgrade", "0019")
    tables = set(inspect(engine).get_table_names())
    assert "zotero_sync_state" not in tables
    assert "zotero_item_map" not in tables

    # ...and re-upgrading recreates them (exercises the has_table guards).
    _run_alembic(engine, "upgrade", "0020")
    tables = set(inspect(engine).get_table_names())
    assert "zotero_sync_state" in tables
    assert "zotero_item_map" in tables
    engine.dispose()


def test_upgrade_is_idempotent(tmp_path):
    # Running the migration runner a second time must not error.
    engine = _migrated_engine(tmp_path)
    run_migrations(engine)
    tables = set(inspect(engine).get_table_names())
    assert "zotero_sync_state" in tables
    assert "zotero_item_map" in tables
    engine.dispose()