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.

feat(journal_quality): CHECK constraint on Source.score_source (#3578)

LearningCircuitda582f5f42e8
diff --git a/src/local_deep_research/journal_quality/db.py b/src/local_deep_research/journal_quality/db.py
index 0b6e65418c..dfd04f00f3 100644
--- a/src/local_deep_research/journal_quality/db.py
+++ b/src/local_deep_research/journal_quality/db.py
@@ -66,7 +66,7 @@ _BATCH_SIZE = 5000
# Stamped as SQLite `PRAGMA user_version` during build_db; checked on
# _ensure_engine. Separate from JOURNAL_DATA_VERSION (downloader.py)
# which tracks upstream source-data freshness.
-JOURNAL_QUALITY_SCHEMA_VERSION = 2
+JOURNAL_QUALITY_SCHEMA_VERSION = 3
# Quality tier → score range, used by the dashboard tier filter.
_TIER_RANGES = {
diff --git a/src/local_deep_research/journal_quality/models.py b/src/local_deep_research/journal_quality/models.py
index 6107e07dab..bdb9fda658 100644
--- a/src/local_deep_research/journal_quality/models.py
+++ b/src/local_deep_research/journal_quality/models.py
@@ -24,7 +24,7 @@ ride along automatically — no Alembic, no migration plumbing.
from __future__ import annotations
-from sqlalchemy import Boolean, Float, Integer, String
+from sqlalchemy import Boolean, CheckConstraint, Float, Integer, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
@@ -44,6 +44,21 @@ class Source(JournalQualityBase):
"""An academic venue (journal or conference)."""
__tablename__ = "sources"
+ __table_args__ = (
+ # Defense-in-depth for ``score_source``. The API layer already
+ # rejects out-of-allowlist values at ``/api/journals`` (see
+ # metrics_routes.py _ALLOWED_SCORE_SOURCES), but that only
+ # covers the read path. A DB-level CHECK catches any future
+ # writer — a refactor of _populate_sources, a one-off import
+ # script, a hand-edited manifest — that accidentally inserts
+ # an invalid value. Build fails fast instead of corrupting the
+ # dashboard silently. The allowlist matches what
+ # _populate_sources actually emits in db.py (openalex + doaj).
+ CheckConstraint(
+ "score_source IN ('openalex', 'doaj')",
+ name="ck_sources_score_source",
+ ),
+ )
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String, nullable=False)
diff --git a/tests/journal_quality/test_db.py b/tests/journal_quality/test_db.py
index 979d2364c3..384cdd73fe 100644
--- a/tests/journal_quality/test_db.py
+++ b/tests/journal_quality/test_db.py
@@ -297,6 +297,55 @@ class TestBuildReferenceDb:
"full-table scan of ~217K rows."
)
+ def test_score_source_check_constraint_rejects_invalid(self):
+ """CHECK constraint on ``sources.score_source`` must reject
+ values outside the allowlist — defense-in-depth against any
+ future writer (refactor, migration, import script) that
+ accidentally bypasses the API layer's validation and tries
+ to insert garbage.
+ """
+ from sqlalchemy import create_engine, text
+ from sqlalchemy.exc import IntegrityError
+ from local_deep_research.journal_quality.models import (
+ JournalQualityBase,
+ )
+
+ engine = create_engine("sqlite:///:memory:")
+ # SQLite only enforces CHECK constraints when foreign-key/CHECK
+ # handling is explicitly on for the connection. create_engine's
+ # default SQLite connection does enforce CHECK — verify via a
+ # round-trip.
+ JournalQualityBase.metadata.create_all(engine)
+
+ # Minimal columns needed for a valid row — the NOT NULL flags
+ # come from the model, not this test. is_in_doaj / has_doaj_seal
+ # / is_predatory all default to False but the insert still has
+ # to satisfy NOT NULL.
+ cols = (
+ "name, name_lower, is_in_doaj, has_doaj_seal, "
+ "is_predatory, score_source"
+ )
+
+ with engine.begin() as conn:
+ # Happy path: known-valid value goes through.
+ conn.execute(
+ text(
+ f"INSERT INTO sources ({cols}) "
+ "VALUES ('Test Journal', 'test journal', "
+ "0, 0, 0, 'openalex')"
+ )
+ )
+
+ with engine.begin() as conn:
+ with pytest.raises(IntegrityError, match=r"(?i)CHECK|score_source"):
+ conn.execute(
+ text(
+ f"INSERT INTO sources ({cols}) "
+ "VALUES ('Bad Journal', 'bad journal', "
+ "0, 0, 0, 'garbage')"
+ )
+ )
+
def test_built_db_stamps_schema_version(self, tmp_path):
"""``build_db`` must stamp ``PRAGMA user_version`` to the
current ``JOURNAL_QUALITY_SCHEMA_VERSION`` constant. The