Files
HarborForge.Backend/app/models/proposal.py
zhi 089d75f953 BE-PR-003: Add Essential SQLAlchemy model
- New app/models/essential.py with Essential model and EssentialType enum
  (feature, improvement, refactor)
- Fields: id, essential_code (unique), proposal_id (FK to proposes),
  type, title, description, created_by_id (FK to users), created_at, updated_at
- Added essentials relationship to Proposal model (cascade delete-orphan)
- Added essentials table auto-migration in main.py _migrate_schema()
- Registered essential module import in startup()
2026-03-29 16:33:00 +00:00

95 lines
3.5 KiB
Python

from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Enum
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.config import Base
import enum
class ProposalStatus(str, enum.Enum):
OPEN = "open"
ACCEPTED = "accepted"
REJECTED = "rejected"
class Proposal(Base):
"""Proposal model — a suggested scope of work under a Project.
After BE-PR-001 rename: Python class is ``Proposal``, DB table stays ``proposes``
for backward compatibility.
Relationships
-------------
- ``project_id`` — FK to ``projects.id``; every Proposal belongs to exactly
one Project.
- ``created_by_id`` — FK to ``users.id``; the user who authored the Proposal.
Nullable for legacy rows created before tracking was added.
- ``feat_task_id`` — **DEPRECATED**. Previously stored the single generated
``story/feature`` task id on accept. Will be replaced by
the Essential → story-task mapping (see BE-PR-008).
Kept in the DB column for read-only backward compat; new
code MUST NOT write to this field.
"""
__tablename__ = "proposes" # keep DB table name for compat
id = Column(Integer, primary_key=True, index=True)
# DB column stays ``propose_code`` for migration safety; use the
# ``proposal_code`` hybrid property in new Python code.
propose_code = Column(
String(64), nullable=True, unique=True, index=True,
comment="Unique human-readable code, e.g. PROJ:P00001",
)
title = Column(String(255), nullable=False, comment="Short title of the proposal")
description = Column(Text, nullable=True, comment="Detailed description / rationale")
status = Column(
Enum(ProposalStatus, values_callable=lambda x: [e.value for e in x]),
default=ProposalStatus.OPEN,
comment="Lifecycle status: open → accepted | rejected",
)
project_id = Column(
Integer, ForeignKey("projects.id"), nullable=False,
comment="Owning project",
)
created_by_id = Column(
Integer, ForeignKey("users.id"), nullable=True,
comment="Author of the proposal (nullable for legacy rows)",
)
# DEPRECATED — see class docstring. Read-only; will be removed once
# Essential-based accept (BE-PR-007 / BE-PR-008) is fully rolled out.
feat_task_id = Column(
String(64), nullable=True,
comment="DEPRECATED: id of the single story/feature task generated on old-style accept",
)
created_at = Column(DateTime(timezone=True), server_default=func.now())
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
# ---- relationships -----------------------------------------------------
essentials = relationship(
"Essential",
foreign_keys="Essential.proposal_id",
cascade="all, delete-orphan",
lazy="select",
)
# ---- convenience alias ------------------------------------------------
@hybrid_property
def proposal_code(self) -> str | None:
"""Preferred accessor — maps to the DB column ``propose_code``."""
return self.propose_code
@proposal_code.setter # type: ignore[no-redef]
def proposal_code(self, value: str | None) -> None:
self.propose_code = value
# Backward-compatible aliases
ProposeStatus = ProposalStatus
Propose = Proposal