Files
saas_backend/app/models/system/subscription_history_model.py
T
2026-08-31 20:39:41 -04:00

48 lines
1.7 KiB
Python

import uuid
from sqlalchemy import Column, Date, DateTime, ForeignKey, String, Text, func
from sqlalchemy.dialects.postgresql import UUID
from app.config.database import Base
class TenantSubscriptionHistory(Base):
"""What changed about a workspace's subscription, when, and who did it.
Append-only by convention: nothing updates a row here. Answering "why is this
workspace on that plan" previously required guessing from the current state,
which is no answer at all once more than one person can make the change.
"""
__tablename__ = "tenant_subscription_history"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
tenant_id = Column(
UUID(as_uuid=True), ForeignKey("tenants.id", ondelete="CASCADE"), nullable=False, index=True
)
from_plan_id = Column(
UUID(as_uuid=True), ForeignKey("subscription_plans.id", ondelete="SET NULL"), nullable=True
)
to_plan_id = Column(
UUID(as_uuid=True), ForeignKey("subscription_plans.id", ondelete="SET NULL"), nullable=True
)
change_type = Column(String(30), nullable=False)
from_end_date = Column(Date, nullable=True)
to_end_date = Column(Date, nullable=True)
from_status = Column(String(30), nullable=True)
to_status = Column(String(30), nullable=True)
changed_by_id = Column(
UUID(as_uuid=True), ForeignKey("users.id", ondelete="SET NULL"), nullable=True
)
changed_by_email = Column(String, nullable=True)
notes = Column(Text, nullable=True)
created_at = Column(DateTime(timezone=True), server_default=func.now(), nullable=False)
def __repr__(self) -> str:
return f"<TenantSubscriptionHistory {self.tenant_id} {self.change_type}>"