Proposal: 数据模型设计(核心表与关系)
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 35/100
Hướng nghiên cứu
Read the overall plan in #1 and the JSONB structure proposal in #4, then review this issue's PostgreSQL DDL and design constraints. The expected outcome is review approval; after that, #8 and #13 are assigned to implement the migrations and domain logic. The issue is design work and does not name migration files or tests to start from.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
背景
总体方案见 #1。本文约定 P0 所需的核心表结构、关键字段与设计约束。数据库 PostgreSQL,迁移工具 golang-migrate,DDL 由迁移文件维护。所有业务表必须携带 tenant_id、created_at、updated_at。
实体关系
erDiagram
tenants ||--o{ tenant_members : has
users ||--o{ tenant_members : joins
tenants ||--o{ ticket_types : defines
ticket_types ||--o{ tickets : categorizes
users ||--o{ tickets : creates
users ||--o{ tickets : handles
tickets ||--o{ ticket_timeline : records
tickets ||--o{ attachments : has
tickets ||--o{ notifications : triggers
核心表
tenants(租户)
CREATE TABLE tenants (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
slug TEXT NOT NULL UNIQUE, -- 人类可读标识,仅对外展示
logo_url TEXT,
theme JSONB NOT NULL DEFAULT '{}', -- 主色等品牌配置
status TEXT NOT NULL DEFAULT 'active', -- active / disabled
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
users 与 tenant_members(全局用户 + 租户内成员)
用户全局唯一(同一邮箱可加入多个租户),角色挂在成员关系上:
CREATE TABLE users (
id BIGSERIAL PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
password_hash TEXT NOT NULL,
display_name TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE tenant_members (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
user_id BIGINT NOT NULL REFERENCES users(id),
role TEXT NOT NULL, -- tenant_admin / agent / requester
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (tenant_id, user_id)
);
ticket_types(模板包)
一个工单类型 = 表单 schema + 流程 + SLA + 分派 + 视图配置。P0 以 JSONB 承载,各 JSONB 的内部结构见 #4:
CREATE TABLE ticket_types (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
slug TEXT NOT NULL, -- 租户内唯一,仅对外展示
name TEXT NOT NULL,
icon TEXT,
form_schema JSONB NOT NULL DEFAULT '{}',
workflow JSONB NOT NULL DEFAULT '{}',
sla_policy JSONB NOT NULL DEFAULT '{}',
assignment JSONB NOT NULL DEFAULT '{}',
view_config JSONB NOT NULL DEFAULT '{}',
is_builtin BOOLEAN NOT NULL DEFAULT false,
status TEXT NOT NULL DEFAULT 'enabled',
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (tenant_id, slug)
);
tickets(工单)
CREATE TABLE tickets (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL REFERENCES tenants(id),
ticket_type_id BIGINT NOT NULL REFERENCES ticket_types(id),
number TEXT NOT NULL, -- 租户内递增序号的十进制字符串,仅展示
title TEXT NOT NULL,
content TEXT NOT NULL DEFAULT '',
priority TEXT NOT NULL DEFAULT 'normal', -- low / normal / high / urgent
state TEXT NOT NULL, -- workflow 中定义的状态 key
custom_fields JSONB NOT NULL DEFAULT '{}', -- 按 form_schema 校验后落库
creator_id BIGINT NOT NULL REFERENCES users(id),
assignee_id BIGINT REFERENCES users(id),
due_at TIMESTAMPTZ,
sla_deadlines JSONB NOT NULL DEFAULT '{}',
closed_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (tenant_id, number)
);
CREATE INDEX idx_tickets_tenant_state ON tickets (tenant_id, state);
CREATE INDEX idx_tickets_tenant_assignee ON tickets (tenant_id, assignee_id);
CREATE INDEX idx_tickets_tenant_type ON tickets (tenant_id, ticket_type_id);
ticket_timeline(时间线,单一事实来源)
对话、内部备注、状态变更、分派、系统事件全部落这张表,审计与进度展示同源:
CREATE TABLE ticket_timeline (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL,
ticket_id BIGINT NOT NULL REFERENCES tickets(id),
kind TEXT NOT NULL, -- comment / internal_note / state_change / assignment / system
author_id BIGINT NOT NULL REFERENCES users(id),
body TEXT NOT NULL DEFAULT '',
meta JSONB NOT NULL DEFAULT '{}', -- 如 {"from":"pending","to":"processing","action":"accept"}
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_timeline_ticket ON ticket_timeline (ticket_id, created_at);
attachments 与 notifications(P0)
CREATE TABLE attachments (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL,
ticket_id BIGINT NOT NULL REFERENCES tickets(id),
timeline_id BIGINT REFERENCES ticket_timeline(id),
filename TEXT NOT NULL,
size_bytes BIGINT NOT NULL,
content_type TEXT NOT NULL,
storage_path TEXT NOT NULL,
sha256 TEXT NOT NULL,
uploader_id BIGINT NOT NULL REFERENCES users(id),
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE TABLE notifications (
id BIGSERIAL PRIMARY KEY,
tenant_id BIGINT NOT NULL,
user_id BIGINT NOT NULL REFERENCES users(id),
ticket_id BIGINT REFERENCES tickets(id),
kind TEXT NOT NULL, -- 稳定事件 code,见 #7
title TEXT NOT NULL,
body TEXT NOT NULL DEFAULT '',
read_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_notifications_user_unread ON notifications (user_id) WHERE read_at IS NULL;
P1 追加:webhook_endpoints、events(outbox)、audit_log(管理域审计)、满意度评价字段。P0 不建半成品表。
设计约束(评审重点)
- 标识符分离:数字
id只做表关联与接口入参;slug、number只做对外展示,禁止参与表关联、禁止作为接口入参(历史调用可做归一兜底,新代码不得依赖); - state 不用数据库枚举:状态值来自该类型
workflow中定义的状态 key,合法性由状态机引擎在写入前校验; - custom_fields 写入前校验:按该类型
form_schema校验通过才允许落库,禁止存任意 JSON; - tenant_id 纪律:所有查询经 repository 层统一 scope(机制见 #6),handler 禁止拼裸 SQL;
- 工单号生成:租户内递增(租户级序号行
SELECT ... FOR UPDATE或等价方案),存十进制字符串如"42",展示前缀(如#)由前端添加; - 软删除:P0 不引入软删除,成员移除即删关系行,工单只关闭不删除。
Expect
- 评审通过后产出
migrations/初始迁移文件,由 #8 落地骨架、#13 完成领域逻辑; - 后续字段演进只允许新增迁移,禁止修改已合并的迁移文件。
- Ngôn ngữ chính
- Không có dữ liệu ngôn ngữ
- Star
- 0
- Fork
- 1
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của LinuxSuRen/tick-flow
-
项目门面:logo、README 与仓库 AboutCó thể đã có người làm @linuxsuren-fs đã nhận 9 ngày trước. Đang mởdocumentation good first issue help wanted P0
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 20/100
LinuxSuRen/tick-flow#20 ·
-
编写四个内置种子模板包Có thể đã có người làm @linuxsuren-fs đã nhận 9 ngày trước. Đang mởgood first issue help wanted P0
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
LinuxSuRen/tick-flow#19 ·
-
实现处理工作台前端:列表、详情与处理动作Có thể đã có người làm @linuxsuren-fs đã nhận 9 ngày trước. Đang mởhelp wanted P0
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 25/100
LinuxSuRen/tick-flow#18 ·
-
实现提交门户前端:表单渲染器与我的工单Có thể đã có người làm @linuxsuren-fs đã nhận 9 ngày trước. Đang mởhelp wanted P0
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
LinuxSuRen/tick-flow#17 ·
-
实现站内通知与邮件通知Có thể đã có người làm @linuxsuren-fs đã nhận 9 ngày trước. Đang mởhelp wanted P0
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
LinuxSuRen/tick-flow#16 ·
Tất cả issue của LinuxSuRen/tick-flow
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
vetro-protocol/vetro-monorepo#1262 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
ProyectoRespira/respira-webapp#334 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100