Files
provenance/backend/app/models/source.py
justin 297cb797d6 Add core data model (12 tables) and initial Alembic migration
All core entities from ARCHITECTURE §5: tenancy (User, Tree, TreeMembership), people (Person, Name, Relationship), facts (Event, Place, PlaceName), provenance (Source, Citation), and the append-only AuditEntry. Cross-cutting mixins give every row a UUID key, timestamps, soft delete, and (where tree-owned) a tree_id for uniform tenant isolation.

Modeling choices: parentage as qualified edges (biological/adoptive/step/foster/donor/guardian) so non-traditional families are first-class; events keep both a verbatim date string and a normalized start/end range; closed sets are PG enums while GEDCOM-extensible vocabularies (event/name/source type) stay strings; CHECK constraints enforce single-subject events and single-target citations. Place is tree-scoped in Phase 0 (see ARCHITECTURE note). The migration is verified reversible (upgrade/downgrade drops tables and enum types) and matches the models (alembic check clean); applied on the deploy target. Dockerfile now ships migrations.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Justin Paul <justin@jpaul.me>
2026-06-06 10:40:00 -04:00

67 lines
2.6 KiB
Python

"""Source and Citation — the first-class provenance spine.
A Source is a reusable record of an origin; a Citation links one Source to one
specific fact (a Person, Name, Event, or Relationship — and OwnershipEvent once
property lands). A CHECK enforces exactly one target so a citation always points
at a single fact.
"""
import uuid
from sqlalchemy import CheckConstraint, ForeignKey, String, Text
from sqlalchemy import Enum as SAEnum
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
from app.models.enums import CitationConfidence
from app.models.mixins import SoftDelete, TenantScoped, Timestamps, UUIDPrimaryKey
class Source(Base, UUIDPrimaryKey, TenantScoped, Timestamps, SoftDelete):
__tablename__ = "sources"
title: Mapped[str] = mapped_column(String(512))
author: Mapped[str | None] = mapped_column(String(255))
source_type: Mapped[str | None] = mapped_column(String(64)) # book, census, deed, ...
repository: Mapped[str | None] = mapped_column(String(255))
url: Mapped[str | None] = mapped_column(String(1024))
citation_text: Mapped[str | None] = mapped_column(Text)
publication_info: Mapped[str | None] = mapped_column(Text)
quality_note: Mapped[str | None] = mapped_column(String(255))
class Citation(Base, UUIDPrimaryKey, TenantScoped, Timestamps, SoftDelete):
__tablename__ = "citations"
__table_args__ = (
CheckConstraint(
"(person_id IS NOT NULL)::int + (event_id IS NOT NULL)::int "
"+ (name_id IS NOT NULL)::int + (relationship_id IS NOT NULL)::int = 1",
name="exactly_one_target",
),
)
source_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey("sources.id", ondelete="CASCADE"), index=True
)
# Exactly one of these is set (see CHECK above).
person_id: Mapped[uuid.UUID | None] = mapped_column(
ForeignKey("persons.id", ondelete="CASCADE"), index=True
)
event_id: Mapped[uuid.UUID | None] = mapped_column(
ForeignKey("events.id", ondelete="CASCADE"), index=True
)
name_id: Mapped[uuid.UUID | None] = mapped_column(
ForeignKey("names.id", ondelete="CASCADE"), index=True
)
relationship_id: Mapped[uuid.UUID | None] = mapped_column(
ForeignKey("relationships.id", ondelete="CASCADE"), index=True
)
# Locality within the source.
page: Mapped[str | None] = mapped_column(String(255))
detail: Mapped[str | None] = mapped_column(Text) # entry, line, free notes
confidence: Mapped[CitationConfidence | None] = mapped_column(
SAEnum(CitationConfidence, name="citation_confidence")
)