feat: implement definition-aware forms runtime

This commit is contained in:
2026-08-01 17:48:35 +02:00
parent 396d6b0c90
commit 9dc49fe27b
25 changed files with 4180 additions and 79 deletions
@@ -0,0 +1,132 @@
from __future__ import annotations
from datetime import datetime
from typing import Any
import uuid
from sqlalchemy import DateTime, ForeignKey, Index, Integer, JSON, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column
from govoplan_core.db.base import Base, TimestampMixin
def new_uuid() -> str:
return str(uuid.uuid4())
class FormInstanceIdentity(Base, TimestampMixin):
__tablename__ = "form_instance_identities"
__table_args__ = (
UniqueConstraint(
"tenant_id",
"instance_id",
name="uq_form_instance_identity",
),
Index(
"ix_form_instance_owner",
"tenant_id",
"created_by",
"definition_id",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
instance_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
definition_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
definition_revision: Mapped[str] = mapped_column(
String(255), nullable=False, index=True
)
created_by: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
class FormInstanceRevision(Base, TimestampMixin):
__tablename__ = "form_instance_revisions"
__table_args__ = (
UniqueConstraint(
"tenant_id",
"instance_id",
"revision",
name="uq_form_instance_revision",
),
Index(
"ix_form_instance_current",
"tenant_id",
"instance_id",
"superseded_at",
),
Index(
"ix_form_instance_catalog",
"tenant_id",
"status",
"recorded_at",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
instance_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
identity_id: Mapped[str] = mapped_column(
ForeignKey("form_instance_identities.id", ondelete="RESTRICT"),
nullable=False,
index=True,
)
revision: Mapped[int] = mapped_column(Integer, nullable=False)
previous_revision_id: Mapped[str | None] = mapped_column(
ForeignKey("form_instance_revisions.id", ondelete="RESTRICT"),
nullable=True,
index=True,
)
status: Mapped[str] = mapped_column(String(30), nullable=False, index=True)
recorded_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, index=True
)
superseded_at: Mapped[datetime | None] = mapped_column(
DateTime(timezone=True), nullable=True, index=True
)
snapshot: Mapped[dict[str, Any]] = mapped_column(JSON, nullable=False)
changed_by: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
class FormInstanceEvent(Base, TimestampMixin):
__tablename__ = "form_instance_events"
__table_args__ = (
UniqueConstraint(
"tenant_id",
"event_id",
name="uq_form_instance_event",
),
UniqueConstraint(
"tenant_id",
"idempotency_key",
name="uq_form_instance_idempotency",
),
Index(
"ix_form_instance_event_history",
"tenant_id",
"instance_id",
"occurred_at",
),
)
id: Mapped[str] = mapped_column(String(36), primary_key=True, default=new_uuid)
tenant_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
instance_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
instance_revision: Mapped[int] = mapped_column(Integer, nullable=False)
event_id: Mapped[str] = mapped_column(String(36), nullable=False, index=True)
event_type: Mapped[str] = mapped_column(String(120), nullable=False, index=True)
status: Mapped[str] = mapped_column(String(30), nullable=False, index=True)
occurred_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True), nullable=False, index=True
)
actor_id: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
idempotency_key: Mapped[str] = mapped_column(String(255), nullable=False)
request_sha256: Mapped[str] = mapped_column(String(64), nullable=False)
payload: Mapped[dict[str, Any]] = mapped_column(JSON, default=dict, nullable=False)
__all__ = [
"FormInstanceEvent",
"FormInstanceIdentity",
"FormInstanceRevision",
]