WorkLog initial import
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
"""数据库表结构(SQLAlchemy Core)。
|
||||
|
||||
四张表:users(账号)、sessions(登录会话)、activities(活动)、
|
||||
participations(参加记录,也就是"某人参加过某活动"这一事实)。
|
||||
时间统一存毫秒时间戳整数,前端用 new Date(ms) 渲染。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from sqlalchemy import (
|
||||
Column,
|
||||
ForeignKey,
|
||||
Index,
|
||||
Integer,
|
||||
MetaData,
|
||||
String,
|
||||
Table,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
func,
|
||||
)
|
||||
|
||||
metadata = MetaData()
|
||||
|
||||
auth_limits = Table(
|
||||
"auth_limits", metadata,
|
||||
Column("key", String(64), primary_key=True),
|
||||
Column("window_start", Integer, nullable=False),
|
||||
Column("count", Integer, nullable=False),
|
||||
)
|
||||
|
||||
users = Table(
|
||||
"users",
|
||||
metadata,
|
||||
Column("id", String(36), primary_key=True),
|
||||
Column("username", String(64), nullable=False, unique=True),
|
||||
Column("display_name", String(64), nullable=False),
|
||||
Column("department", String(64), nullable=False, default=""),
|
||||
Column("email", String(255), nullable=False, default=""),
|
||||
# 登录改为邮箱+验证码后不再用密码,但列保留(老数据兼容)
|
||||
Column("password_hash", String(255), nullable=False, default=""),
|
||||
# 邮箱用于验证码登录;新用户注册必填手机号,一人一号(历史空值保留兼容)
|
||||
Column("phone", String(11), nullable=False, default=""),
|
||||
# 遗留列:管理员体系已移除,但老库的 role 列带 NOT NULL 约束,插入必须带上
|
||||
Column("role", String(16), nullable=False, default="member"),
|
||||
Column("bio", String(200), nullable=False, default=""),
|
||||
Column("created_at", Integer, nullable=False),
|
||||
Column("last_login_at", Integer, nullable=False, default=0),
|
||||
)
|
||||
Index("ix_users_email", users.c.email)
|
||||
Index("uq_users_email", func.lower(users.c.email), unique=True, sqlite_where=users.c.email != "")
|
||||
Index("uq_users_phone", users.c.phone, unique=True, sqlite_where=users.c.phone != "")
|
||||
|
||||
email_verifications = Table(
|
||||
"email_verifications",
|
||||
metadata,
|
||||
Column("id", String(36), primary_key=True),
|
||||
Column("email", String(255), nullable=False),
|
||||
Column("code", String(255), nullable=False),
|
||||
Column("attempts", Integer, nullable=False, default=0),
|
||||
# 预留:register / reset 等用途
|
||||
Column("purpose", String(32), nullable=False, default="register"),
|
||||
Column("expires_at", Integer, nullable=False),
|
||||
Column("used", Integer, nullable=False, default=0),
|
||||
Column("created_at", Integer, nullable=False),
|
||||
)
|
||||
Index("ix_email_verifications_email", email_verifications.c.email)
|
||||
|
||||
sessions = Table(
|
||||
"sessions",
|
||||
metadata,
|
||||
Column("token", String(64), primary_key=True),
|
||||
Column("user_id", String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
|
||||
Column("created_at", Integer, nullable=False),
|
||||
Column("expires_at", Integer, nullable=False),
|
||||
)
|
||||
Index("ix_sessions_user", sessions.c.user_id)
|
||||
|
||||
activities = Table(
|
||||
"activities",
|
||||
metadata,
|
||||
Column("id", String(36), primary_key=True),
|
||||
Column("title", String(120), nullable=False),
|
||||
Column("start_at", Integer, nullable=False),
|
||||
Column("location", String(120), nullable=False, default=""),
|
||||
Column("description", Text, nullable=False, default=""),
|
||||
Column("visibility", String(16), nullable=False, default="public"),
|
||||
# 名额上限;NULL 表示不限
|
||||
Column("capacity", Integer, nullable=True),
|
||||
# 兼容老库保留该列;无审核流后恒为 approved
|
||||
Column("status", String(16), nullable=False, default="approved"),
|
||||
Column("organizer_id", String(36), ForeignKey("users.id"), nullable=False),
|
||||
Column("created_at", Integer, nullable=False),
|
||||
Column("updated_at", Integer, nullable=False),
|
||||
)
|
||||
Index("ix_activities_start", activities.c.start_at)
|
||||
Index("ix_activities_organizer", activities.c.organizer_id)
|
||||
|
||||
participations = Table(
|
||||
"participations",
|
||||
metadata,
|
||||
Column("id", String(36), primary_key=True),
|
||||
Column("activity_id", String(36), ForeignKey("activities.id", ondelete="CASCADE"), nullable=False),
|
||||
Column("user_id", String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
|
||||
# organizer 为发起人,member 为共同参加人("我也参加过")
|
||||
Column("role", String(16), nullable=False, default="member"),
|
||||
# 参加状态:joined 已确认 / attended 已参加 / absent 缺席
|
||||
Column("status", String(16), nullable=False, default="joined"),
|
||||
Column("note", String(500), nullable=False, default=""),
|
||||
Column("joined_at", Integer, nullable=False),
|
||||
Column("updated_at", Integer, nullable=False),
|
||||
UniqueConstraint("activity_id", "user_id", name="uq_participation_activity_user"),
|
||||
)
|
||||
Index("ix_participations_user", participations.c.user_id)
|
||||
|
||||
join_links = Table(
|
||||
"join_links",
|
||||
metadata,
|
||||
# 邮件"一键我也参加过"的令牌:点一次即消费,转正为一条参加记录
|
||||
Column("id", String(36), primary_key=True),
|
||||
Column("token", String(64), nullable=False, unique=True),
|
||||
Column("activity_id", String(36), ForeignKey("activities.id", ondelete="CASCADE"), nullable=False),
|
||||
Column("user_id", String(36), ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
|
||||
Column("expires_at", Integer, nullable=False),
|
||||
# 0=可用 1=已点击 2=已失效(活动删除等)
|
||||
Column("used", Integer, nullable=False, default=0),
|
||||
Column("created_at", Integer, nullable=False),
|
||||
)
|
||||
Index("ix_join_links_activity", join_links.c.activity_id)
|
||||
|
||||
TABLES = (users, sessions, email_verifications, activities, participations, join_links)
|
||||
Reference in New Issue
Block a user