D09: Import record event history for revision tracking
- Add ImportRecordEvent model to track per-record import changes - Log created/updated events for each official record import - Add alembic migration 0014 for import_record_event table - Enables audit trail for which import run modified which records
This commit is contained in:
@@ -13,7 +13,7 @@ from sqlalchemy.orm import Session
|
||||
from rf4_research.official_parser import RecordsContractError, parse_official_records
|
||||
|
||||
from .models import (
|
||||
Bait, BaitKind, CatchReport, Fish, ImportStatus, ModerationStatus,
|
||||
Bait, BaitKind, CatchReport, Fish, ImportRecordEvent, ImportStatus, ModerationStatus,
|
||||
OfficialRecordImport, SourceType, Waterbody,
|
||||
)
|
||||
|
||||
@@ -185,12 +185,16 @@ def _import_records_locked(session: Session, *, url: str, region: str, category:
|
||||
bait = _bait(session, raw.bait) if raw.bait else None
|
||||
payload = asdict(raw) | {"record_date": raw.record_date.isoformat()}
|
||||
caught = datetime.combine(raw.record_date, time(), tzinfo=timezone.utc)
|
||||
now = datetime.now(timezone.utc)
|
||||
if report is None:
|
||||
session.add(CatchReport(fish=fish, waterbody=waterbody, bait=bait, spot=None, weight_g=raw.weight_g, caught_at=caught, reported_at=datetime.now(timezone.utc), player_name=raw.player, source_type=SourceType.official_record, source_url=url, source_external_id=key, source_confidence=100, moderation_status=ModerationStatus.approved, raw_payload=payload))
|
||||
report = CatchReport(fish=fish, waterbody=waterbody, bait=bait, spot=None, weight_g=raw.weight_g, caught_at=caught, reported_at=now, player_name=raw.player, source_type=SourceType.official_record, source_url=url, source_external_id=key, source_confidence=100, moderation_status=ModerationStatus.approved, raw_payload=payload)
|
||||
session.add(report)
|
||||
session.add(ImportRecordEvent(catch_report=report, import_run=run, event_type="created", created_at=now))
|
||||
run.rows_created += 1
|
||||
else:
|
||||
report.raw_payload = payload
|
||||
report.source_url = url
|
||||
session.add(ImportRecordEvent(catch_report=report, import_run=run, event_type="updated", created_at=now))
|
||||
run.rows_updated += 1
|
||||
run.status = ImportStatus.success
|
||||
run.finished_at = datetime.now(timezone.utc)
|
||||
|
||||
@@ -212,3 +212,15 @@ class ExternalEntityAlias(Base):
|
||||
updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|
||||
fish: Mapped[Fish | None] = relationship()
|
||||
waterbody: Mapped[Waterbody | None] = relationship()
|
||||
|
||||
|
||||
class ImportRecordEvent(Base):
|
||||
"""Track per-record import events for D09 revision history."""
|
||||
__tablename__ = "import_record_event"
|
||||
id: Mapped[uuid.UUID] = mapped_column(primary_key=True, default=uuid.uuid4)
|
||||
catch_report_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("catch_report.id"), index=True)
|
||||
import_run_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("official_record_import.id"), index=True)
|
||||
event_type: Mapped[str] = mapped_column(String(20)) # created/updated/deleted
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True))
|
||||
catch_report: Mapped[CatchReport] = relationship()
|
||||
import_run: Mapped[OfficialRecordImport] = relationship()
|
||||
|
||||
Reference in New Issue
Block a user