Files
provenance/backend/app/models/place.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

43 lines
1.6 KiB
Python

"""Place — a gazetteer entity — and PlaceName, its historical name variants.
PlaceName carries date ranges so a record entered as "Königsberg, 1900" sorts
and displays correctly against "Kaliningrad" (ARCHITECTURE §5, §10).
Phase 0 scopes Place to a Tree (``tree_id``) to keep tenant isolation absolute.
ARCHITECTURE calls the gazetteer "tenant-shared"; a deployment-wide shared
gazetteer is a deliberate later refinement (see ARCHITECTURE §5 note).
"""
import uuid
from datetime import date
from sqlalchemy import Date, Float, ForeignKey, String
from sqlalchemy.orm import Mapped, mapped_column
from app.models.base import Base
from app.models.mixins import SoftDelete, TenantScoped, Timestamps, UUIDPrimaryKey
class Place(Base, UUIDPrimaryKey, TenantScoped, Timestamps, SoftDelete):
__tablename__ = "places"
name: Mapped[str] = mapped_column(String(512))
# Self-referential hierarchy: place within place.
parent_id: Mapped[uuid.UUID | None] = mapped_column(
ForeignKey("places.id", ondelete="SET NULL"), index=True
)
place_type: Mapped[str | None] = mapped_column(String(64))
latitude: Mapped[float | None] = mapped_column(Float)
longitude: Mapped[float | None] = mapped_column(Float)
class PlaceName(Base, UUIDPrimaryKey, TenantScoped, Timestamps, SoftDelete):
__tablename__ = "place_names"
place_id: Mapped[uuid.UUID] = mapped_column(
ForeignKey("places.id", ondelete="CASCADE"), index=True
)
name: Mapped[str] = mapped_column(String(512))
valid_from: Mapped[date | None] = mapped_column(Date)
valid_to: Mapped[date | None] = mapped_column(Date)