From 3786d82a2788a6323b27d9db309fd62dd800b2c2 Mon Sep 17 00:00:00 2001 From: v6ole Date: Sun, 12 Jul 2026 12:58:53 +0800 Subject: [PATCH] feat: add Leave model with auto-migration Co-Authored-By: Claude --- backend/app/main.py | 20 ++++++++++++++++++++ backend/app/models/__init__.py | 2 ++ backend/app/models/leave.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+) create mode 100644 backend/app/models/leave.py diff --git a/backend/app/main.py b/backend/app/main.py index 30f34b1..db2f63d 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -53,6 +53,26 @@ async def lifespan(app: FastAPI): await conn.run_sync(lambda c: c.exec_driver_sql( "CREATE TABLE IF NOT EXISTS system_config (key VARCHAR(64) PRIMARY KEY, value TEXT DEFAULT '')" )) + # leaves table for v0.7 + await conn.run_sync(lambda c: c.exec_driver_sql( + "CREATE TABLE IF NOT EXISTS leaves (" + " id UUID PRIMARY KEY DEFAULT gen_random_uuid()," + " manager_id UUID NOT NULL REFERENCES users(id)," + " leave_type VARCHAR(20) NOT NULL DEFAULT '事假'," + " start_date DATE NOT NULL," + " end_date DATE NOT NULL," + " reason VARCHAR(500) DEFAULT ''," + " submitted_by UUID NOT NULL REFERENCES users(id)," + " created_at TIMESTAMPTZ DEFAULT now()," + " updated_at TIMESTAMPTZ DEFAULT now()" + ")" + )) + await conn.run_sync(lambda c: c.exec_driver_sql( + "CREATE INDEX IF NOT EXISTS idx_leaves_manager_id ON leaves(manager_id)" + )) + await conn.run_sync(lambda c: c.exec_driver_sql( + "CREATE INDEX IF NOT EXISTS idx_leaves_dates ON leaves(start_date, end_date)" + )) # Read notification_time from DB (or use default 17:30) notification_hour, notification_minute = 17, 30 diff --git a/backend/app/models/__init__.py b/backend/app/models/__init__.py index 215f04f..f4ad82c 100644 --- a/backend/app/models/__init__.py +++ b/backend/app/models/__init__.py @@ -9,6 +9,7 @@ from app.models.key_visit import KeyVisit from app.models.daily_note import DailyNote from app.models.ai_summary import AISummary from app.models.system_config import SystemConfig +from app.models.leave import Leave __all__ = [ "User", @@ -22,4 +23,5 @@ __all__ = [ "DailyNote", "AISummary", "SystemConfig", + "Leave", ] diff --git a/backend/app/models/leave.py b/backend/app/models/leave.py new file mode 100644 index 0000000..6abeb49 --- /dev/null +++ b/backend/app/models/leave.py @@ -0,0 +1,30 @@ +import uuid +from datetime import date, datetime +from sqlalchemy import String, Date, DateTime, ForeignKey, func +from sqlalchemy.dialects.postgresql import UUID +from sqlalchemy.orm import Mapped, mapped_column +from app.database import Base + + +class Leave(Base): + __tablename__ = "leaves" + + id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), primary_key=True, default=uuid.uuid4 + ) + manager_id: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), ForeignKey("users.id"), index=True + ) + leave_type: Mapped[str] = mapped_column(String(20), default="事假") + start_date: Mapped[date] = mapped_column(Date) + end_date: Mapped[date] = mapped_column(Date) + reason: Mapped[str] = mapped_column(String(500), default="") + submitted_by: Mapped[uuid.UUID] = mapped_column( + UUID(as_uuid=True), ForeignKey("users.id") + ) + created_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), server_default=func.now() + ) + updated_at: Mapped[datetime] = mapped_column( + DateTime(timezone=True), server_default=func.now(), onupdate=func.now() + )