Repository navigation
Expand file tree
/
Copy pathschema.sql
More file actions
147 lines (136 loc) · 5.71 KB
/
Copy pathschema.sql
File metadata and controls
147 lines (136 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- nuistshare 审核库 schema(幂等)
CREATE TABLE IF NOT EXISTS users (
user_id text PRIMARY KEY,
display text NOT NULL DEFAULT '',
role text NOT NULL DEFAULT 'user', -- user | admin
quota_bytes bigint NOT NULL DEFAULT 0,
used_bytes bigint NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL DEFAULT now()
);
-- Reviewer identities are separate from uploaders in users.
CREATE TABLE IF NOT EXISTS reviewers (
reviewer_id text PRIMARY KEY,
display_name text NOT NULL,
role text NOT NULL DEFAULT 'reviewer' CHECK(role IN ('admin','reviewer')),
enabled boolean NOT NULL DEFAULT true,
preferences jsonb NOT NULL DEFAULT '{}',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO reviewers(reviewer_id,display_name,role) VALUES('bootstrap-admin','管理员','admin')
ON CONFLICT(reviewer_id) DO NOTHING;
ALTER TABLE reviewers ADD COLUMN IF NOT EXISTS username text;
ALTER TABLE reviewers ADD COLUMN IF NOT EXISTS password_hash text;
UPDATE reviewers SET enabled=false WHERE role='reviewer' AND password_hash IS NULL AND enabled;
CREATE UNIQUE INDEX IF NOT EXISTS reviewers_username_unique ON reviewers(lower(username)) WHERE username IS NOT NULL;
CREATE TABLE IF NOT EXISTS reviewer_sessions (
token_hash text PRIMARY KEY,
reviewer_id text NOT NULL REFERENCES reviewers(reviewer_id),
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL
);
CREATE INDEX IF NOT EXISTS reviewer_sessions_owner ON reviewer_sessions(reviewer_id);
CREATE TABLE IF NOT EXISTS reviewer_login_limits (
key text PRIMARY KEY,
attempts integer NOT NULL DEFAULT 1,
window_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS sessions (
session_id text PRIMARY KEY,
user_id text NOT NULL REFERENCES users(user_id),
category text NOT NULL DEFAULT '',
created_at timestamptz NOT NULL DEFAULT now(),
expires_at timestamptz NOT NULL
);
-- 预签名/直传的账:confirm 前后状态可追踪
CREATE TABLE IF NOT EXISTS uploads (
upload_id text PRIMARY KEY,
session_id text NOT NULL REFERENCES sessions(session_id),
user_id text NOT NULL,
obj_key text NOT NULL,
name text NOT NULL,
size_bytes bigint NOT NULL,
mime text NOT NULL DEFAULT '',
etag text NOT NULL DEFAULT '',
state text NOT NULL DEFAULT 'presigned', -- presigned|uploading|confirmed|failed
reserved bigint NOT NULL DEFAULT 0,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS objects (
object_id bigserial PRIMARY KEY,
event_id text UNIQUE, -- 幂等键1:事件id
upload_id text,
session_id text NOT NULL,
user_id text NOT NULL,
bucket text NOT NULL,
key text NOT NULL,
etag text NOT NULL DEFAULT '', -- 幂等键2的一部分
name text NOT NULL,
size_bytes bigint NOT NULL DEFAULT 0,
mime text NOT NULL DEFAULT '',
kind text NOT NULL DEFAULT 'file', -- file | archive
parent_id bigint REFERENCES objects(object_id),
depth int NOT NULL DEFAULT 0,
status text NOT NULL DEFAULT 'pending',
risk text NOT NULL DEFAULT '', -- clean|dirty|low_conf|rule_fail
verdict_note text NOT NULL DEFAULT '',
published_key text NOT NULL DEFAULT '',
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (bucket, key, etag)
);
-- BEGIN UPLOAD OUTBOX
CREATE TABLE IF NOT EXISTS upload_outbox (
event_id text PRIMARY KEY,
upload_id text NOT NULL UNIQUE,
payload jsonb NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
delivered_at timestamptz,
attempts integer NOT NULL DEFAULT 0,
next_attempt_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS upload_outbox_pending ON upload_outbox(next_attempt_at,created_at)
WHERE delivered_at IS NULL;
-- END UPLOAD OUTBOX
CREATE INDEX IF NOT EXISTS idx_objects_status ON objects(status);
CREATE INDEX IF NOT EXISTS idx_objects_parent ON objects(parent_id);
CREATE INDEX IF NOT EXISTS idx_objects_user ON objects(user_id);
-- 审核动作留痕(审计真源)
CREATE TABLE IF NOT EXISTS reviews (
review_id bigserial PRIMARY KEY,
object_id bigint NOT NULL REFERENCES objects(object_id),
stage text NOT NULL, -- rule|av|llm|manual|publish|sweep
verdict text NOT NULL, -- pass|hold|fail|skip
detail jsonb NOT NULL DEFAULT '{}',
actor text NOT NULL DEFAULT 'system',
created_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS audit (
id bigserial PRIMARY KEY,
actor text NOT NULL,
actor_ip text NOT NULL DEFAULT '',
action text NOT NULL,
target text NOT NULL DEFAULT '',
detail jsonb NOT NULL DEFAULT '{}',
created_at timestamptz NOT NULL DEFAULT now()
);
-- 发布幂等表
CREATE TABLE IF NOT EXISTS publishes (
object_id bigint PRIMARY KEY REFERENCES objects(object_id),
published_key text NOT NULL UNIQUE,
source_etag text NOT NULL,
published_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS ai_settings (
id integer PRIMARY KEY CHECK (id=1),
value jsonb NOT NULL,
version bigint NOT NULL DEFAULT 0,
runtime jsonb NOT NULL DEFAULT '{}',
updated_at timestamptz NOT NULL DEFAULT now()
);
INSERT INTO ai_settings(id,value) VALUES(1,
'{"enabled":false,"model":"qwen-plus","timeout_sec":20,"min_confidence":0.9,"excerpt_bytes":3800,"suggest_category":true}')
ON CONFLICT(id) DO NOTHING;
ALTER TABLE ai_settings ADD COLUMN IF NOT EXISTS connection_base text;
ALTER TABLE ai_settings ADD COLUMN IF NOT EXISTS api_key_cipher text NOT NULL DEFAULT '';