- 业务 ORM 数据支持 MySQL 后端(asyncmy),新增 PortableJSON/
BeijingDateTime 可移植列类型、迁移脚本与切库脚本
- 修复 GET /api/scheduled-tasks/runs/{id} 的 _load_run 无限递归
(误调用自身,应委托 service.get_run)导致的 RecursionError 500
- 定时任务名按用户唯一:create/update/admin_update 主动校验,
冲突抛 DuplicateTaskNameError,路由转 409 而非裸 IntegrityError 500
- 补充回归测试 test_scheduled_task_run_lookup / _name_uniqueness
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
61 lines
2.2 KiB
Python
61 lines
2.2 KiB
Python
"""ORM model for the users table.
|
|
|
|
Lives in the harness persistence package so it is picked up by
|
|
``Base.metadata.create_all()`` alongside ``threads_meta``, ``runs``,
|
|
``run_events``, and ``feedback``. Using the shared engine means:
|
|
|
|
- One SQLite/Postgres database, one connection pool
|
|
- One schema initialisation codepath
|
|
- Consistent async sessions across auth and persistence reads
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import UTC, datetime
|
|
|
|
from sqlalchemy import Boolean, Index, String, text
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from deerflow.persistence.base import Base
|
|
from deerflow.persistence.types import BeijingDateTime
|
|
|
|
|
|
class UserRow(Base):
|
|
__tablename__ = "users"
|
|
|
|
# UUIDs are stored as 36-char strings for cross-backend portability.
|
|
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
|
|
|
email: Mapped[str] = mapped_column(String(191), unique=True, nullable=False, index=True)
|
|
password_hash: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
|
|
# "admin" | "user" — kept as plain string to avoid ALTER TABLE pain
|
|
# when new roles are introduced.
|
|
system_role: Mapped[str] = mapped_column(String(16), nullable=False, default="user")
|
|
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
BeijingDateTime(),
|
|
nullable=False,
|
|
default=lambda: datetime.now(UTC),
|
|
)
|
|
|
|
# OAuth linkage (optional). A partial unique index enforces one
|
|
# account per (provider, oauth_id) pair, leaving NULL/NULL rows
|
|
# unconstrained so plain password accounts can coexist.
|
|
oauth_provider: Mapped[str | None] = mapped_column(String(32), nullable=True)
|
|
oauth_id: Mapped[str | None] = mapped_column(String(128), nullable=True)
|
|
|
|
# Auth lifecycle flags
|
|
needs_setup: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
|
|
token_version: Mapped[int] = mapped_column(nullable=False, default=0)
|
|
|
|
__table_args__ = (
|
|
Index(
|
|
"idx_users_oauth_identity",
|
|
"oauth_provider",
|
|
"oauth_id",
|
|
unique=True,
|
|
sqlite_where=text("oauth_provider IS NOT NULL AND oauth_id IS NOT NULL"),
|
|
),
|
|
)
|