feat(backend): fix T4 index drift, lazy import, FK policy, expand test coverage

This commit is contained in:
Tyler
2026-06-19 21:42:11 -06:00
parent 0fdb760bc1
commit 2019a651ed
2 changed files with 96 additions and 6 deletions
+19 -6
View File
@@ -11,6 +11,7 @@ same engine.
from __future__ import annotations
import enum
import json
import os
from datetime import date, datetime
from decimal import Decimal
@@ -28,6 +29,7 @@ from sqlalchemy import (
String,
Text,
UniqueConstraint,
text,
)
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship, sessionmaker
from sqlalchemy.types import TypeDecorator
@@ -141,15 +143,11 @@ class JSONText(TypeDecorator):
def process_bind_param(self, value, dialect): # type: ignore[no-untyped-def]
if value is None:
return None
import json
return json.dumps(value)
def process_result_value(self, value, dialect): # type: ignore[no-untyped-def]
if value is None:
return None
import json
return json.loads(value)
@@ -172,8 +170,13 @@ class Batch(Base):
)
__table_args__ = (
Index("ix_batches_parsed_at", "parsed_at"),
# Migration is the source of truth: 0001_initial.sql declares this DESC.
# `text(...)` keeps the ORM in sync so fresh DBs built via `create_all`
# alone (no migration runner) get the same index direction as prod.
Index("ix_batches_parsed_at", text("parsed_at DESC")),
)
# TODO(T6): when ActivityEvent model is added, mirror this pattern for
# `ix_activity_events_ts` on activity_events(ts DESC) per 0001_initial.sql:86.
class Claim(Base):
@@ -196,7 +199,17 @@ class Claim(Base):
Enum(ClaimState, native_enum=False), nullable=True
)
matched_remittance_id: Mapped[Optional[str]] = mapped_column(
String(64), ForeignKey("remittances.id"), nullable=True
# ORM policy: SET NULL on remittance delete. The claim may outlive its
# remittance during reversal/reimport flows; without this, deleting a
# remittance with matched claims raises IntegrityError.
# Note: 0001_initial.sql predates this policy (no ON DELETE clause).
# SQLite ignores FK direction by default unless PRAGMA foreign_keys=ON,
# so the inconsistency is benign there; the ORM is the source of truth
# for PostgreSQL/test environments with FK enforcement. Amendment to the
# migration is deferred to post-SQLite rollout.
String(64),
ForeignKey("remittances.id", ondelete="SET NULL"),
nullable=True,
)
raw_json: Mapped[Optional[dict]] = mapped_column(JSONText, nullable=True)