Skip to content

Commit 8a0335f

Browse files
authored
Merge pull request #154 from ql-link/chore/migration-0014-llm-seed
chore(db): 添加数据库同步脚本以及llm厂商配置种子数据
2 parents b59ffb2 + 01b7dc5 commit 8a0335f

5 files changed

Lines changed: 2054 additions & 0 deletions

File tree

docs/api/schemas/mysql.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ ORM:[`UsageLogDB`](../../src/models/db_models.py)
195195
| `created_at` / `updated_at` | DATETIME | 创建 / 更新时间 |
196196

197197
索引:
198+
- `uk_conversation_user_dataset_title(user_id, dataset_id, title)`
198199
- `idx_chat_conversation_user_pinned_updated(user_id, is_pinned, updated_at)`
199200
- `idx_chat_conversation_dataset_updated(dataset_id, updated_at)`
200201

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""add unique title constraint for chat conversations
2+
3+
Java requires conversation titles to be unique within the same user and dataset.
4+
Before adding the unique key, remove existing duplicates by keeping the newest
5+
conversation row (largest id) for each ``(user_id, dataset_id, title)`` group.
6+
7+
Revision ID: 0014
8+
Revises: 0013
9+
Create Date: 2026-06-07
10+
"""
11+
12+
from __future__ import annotations
13+
14+
from typing import Sequence, Union
15+
16+
import sqlalchemy as sa
17+
from alembic import op
18+
19+
20+
revision: str = "0014"
21+
down_revision: Union[str, None] = "0013"
22+
branch_labels: Union[str, Sequence[str], None] = None
23+
depends_on: Union[str, Sequence[str], None] = None
24+
25+
26+
def upgrade() -> None:
27+
op.execute(
28+
sa.text(
29+
"""
30+
DELETE c1 FROM chat_conversation c1
31+
INNER JOIN chat_conversation c2
32+
ON c1.user_id = c2.user_id
33+
AND c1.dataset_id = c2.dataset_id
34+
AND c1.title = c2.title
35+
AND c1.id < c2.id
36+
"""
37+
)
38+
)
39+
op.create_unique_constraint(
40+
"uk_conversation_user_dataset_title",
41+
"chat_conversation",
42+
["user_id", "dataset_id", "title"],
43+
)
44+
45+
46+
def downgrade() -> None:
47+
op.drop_constraint(
48+
"uk_conversation_user_dataset_title",
49+
"chat_conversation",
50+
type_="unique",
51+
)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
-- Add Aliyun DashScope system presets for remote dev.
2+
-- Do not store plaintext API keys in llm_system_preset.api_key.
3+
-- api_key below is encrypted with the current tolink.llm.api-key.secret
4+
-- using ApiKeyEncryptService AES-256-GCM format: base64(iv + cipherText + tag).
5+
6+
USE tolink_rag_db;
7+
8+
START TRANSACTION;
9+
10+
-- Provider: Tongyi Qianwen uses the project's existing provider_type "aliyun".
11+
INSERT INTO llm_system_provider (
12+
provider_type,
13+
provider_name,
14+
api_base_url,
15+
is_active,
16+
priority
17+
)
18+
VALUES (
19+
'aliyun',
20+
'Tongyi Qianwen (DashScope)',
21+
'https://dashscope.aliyuncs.com/compatible-mode/v1',
22+
TRUE,
23+
99
24+
)
25+
ON DUPLICATE KEY UPDATE
26+
provider_name = VALUES(provider_name),
27+
api_base_url = VALUES(api_base_url),
28+
is_active = TRUE,
29+
priority = GREATEST(priority, VALUES(priority));
30+
31+
-- Ensure the preset models exist in the provider model catalog.
32+
INSERT INTO llm_provider_model (
33+
provider_id,
34+
model_name,
35+
capability,
36+
is_active
37+
)
38+
SELECT p.id, m.model_name, m.capability, TRUE
39+
FROM llm_system_provider p
40+
JOIN (
41+
SELECT 'qwen3.5-flash' AS model_name, 'CHAT' AS capability
42+
UNION ALL SELECT 'text-embedding-v4', 'EMBEDDING'
43+
UNION ALL SELECT 'qwen3-rerank', 'RERANK'
44+
UNION ALL SELECT 'qwen-vl-max', 'VISION'
45+
) m
46+
WHERE p.provider_type = 'aliyun'
47+
ON DUPLICATE KEY UPDATE
48+
is_active = TRUE;
49+
50+
-- Active system presets are copied to new users as is_system_preset=true.
51+
-- Existing matching presets are updated to this key and enabled.
52+
INSERT INTO llm_system_preset (
53+
provider_id,
54+
model_name,
55+
capability,
56+
api_key,
57+
is_active
58+
)
59+
SELECT p.id, m.model_name, m.capability,
60+
'vIgEBTQxRc4g8WU4ghnMpwCOFiFAdN/AxaVwfGLa499SjFlsXJ/0HZ+a1A2qJdnokqPKM/yNlJ1SnF/3MU2+' AS api_key,
61+
TRUE
62+
FROM llm_system_provider p
63+
JOIN (
64+
SELECT 'qwen3.5-flash' AS model_name, 'CHAT' AS capability
65+
UNION ALL SELECT 'text-embedding-v4', 'EMBEDDING'
66+
UNION ALL SELECT 'qwen3-rerank', 'RERANK'
67+
UNION ALL SELECT 'qwen-vl-max', 'VISION'
68+
) m
69+
WHERE p.provider_type = 'aliyun'
70+
ON DUPLICATE KEY UPDATE
71+
api_key = VALUES(api_key),
72+
is_active = TRUE,
73+
updated_at = CURRENT_TIMESTAMP;
74+
75+
-- Verification query.
76+
SELECT
77+
p.provider_type,
78+
p.provider_name,
79+
p.api_base_url,
80+
s.model_name,
81+
s.capability,
82+
s.is_active
83+
FROM llm_system_preset s
84+
JOIN llm_system_provider p ON p.id = s.provider_id
85+
WHERE p.provider_type = 'aliyun'
86+
AND (s.model_name, s.capability) IN (
87+
('qwen3.5-flash', 'CHAT'),
88+
('text-embedding-v4', 'EMBEDDING'),
89+
('qwen3-rerank', 'RERANK'),
90+
('qwen-vl-max', 'VISION')
91+
)
92+
ORDER BY FIELD(s.capability, 'CHAT', 'EMBEDDING', 'RERANK', 'VISION');
93+
94+
COMMIT;
95+
96+
-- Rollback if needed:
97+
-- DELETE s
98+
-- FROM llm_system_preset s
99+
-- JOIN llm_system_provider p ON p.id = s.provider_id
100+
-- WHERE p.provider_type = 'aliyun'
101+
-- AND (s.model_name, s.capability) IN (
102+
-- ('qwen3.5-flash', 'CHAT'),
103+
-- ('text-embedding-v4', 'EMBEDDING'),
104+
-- ('qwen3-rerank', 'RERANK'),
105+
-- ('qwen-vl-max', 'VISION')
106+
-- );

scripts/db/init.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ CREATE TABLE IF NOT EXISTS chat_conversation (
125125
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
126126
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
127127

128+
UNIQUE KEY uk_conversation_user_dataset_title (user_id, dataset_id, title),
128129
INDEX idx_chat_conversation_user_pinned_updated (user_id, is_pinned, updated_at),
129130
INDEX idx_chat_conversation_dataset_updated (dataset_id, updated_at)
130131
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=10000 COMMENT '对话表';

0 commit comments

Comments
 (0)