refactor(db): 阶段B+C - MySQL/Postgres migration + 方言适配器
migration - migrations/mysql/ 8 个文件:BIGINT AUTO_INCREMENT、VARCHAR 主键、 TINYINT(1) 布尔、INSERT IGNORE、ON DUPLICATE KEY UPDATE - migrations/postgres/ 8 个文件:BIGSERIAL、BOOLEAN、ON CONFLICT DO UPDATE - 跳过 SQLite 历史遗留列(auto_create_delivery_rule 等)在 mysql/pg 00006 不创建,00008 不删除 方言适配器 (internal/db/dialect.go) - DialectUpsert: SQLite/PG 用 ON CONFLICT DO UPDATE + EXCLUDED.col; MySQL 用 ON DUPLICATE KEY UPDATE + VALUES(col) - DialectInsertIgnore + DialectInsertIgnorePrefix: MySQL INSERT IGNORE; SQLite/PG ON CONFLICT DO NOTHING - DialectQuote: MySQL/SQLite 反引号,Postgres 双引号(处理 key 等保留字列名) 业务 SQL 改造(16 处 upsert) - db: cookies/extras(系统设置/商品信息/通知绑定)/orders/reply/automation 全部改用方言适配器;item_replay Set 改 DELETE+INSERT(无自然唯一键) - server: notification_handlers/settings_handlers/default_reply_handlers 的 ON CONFLICT/INSERT OR REPLACE 改用 db.DialectUpsert - 各 repository struct 加 Dialect 字段,NewStore 注入 验证:SQLite 全测试通过(回归),MySQL/PG migration 已就位待真实实例验证 Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
0fa5d6617c
commit
36a7f74154
20 files changed
+511
-423
No files matched your search
@@ -4,6 +4,8 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"xianyu-go/internal/db"
|
||||
)
|
||||
|
||||
// btoi bool→int(SQLite 无原生 bool)。
|
||||
@@ -61,8 +63,14 @@ func (s *Server) setDefaultReply(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
_, err := s.Store.DB.ExecContext(r.Context(),
|
||||
`INSERT OR REPLACE INTO default_replies (cookie_id, enabled, reply_content, reply_image_url, reply_once, updated_at)
|
||||
VALUES (?,?,?,?,?,CURRENT_TIMESTAMP)`,
|
||||
`INSERT INTO default_replies (cookie_id, enabled, reply_content, reply_image_url, reply_once, updated_at)
|
||||
VALUES (?,?,?,?,?,CURRENT_TIMESTAMP)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id"}, map[string]string{
|
||||
"enabled": "EXCLUDED.enabled",
|
||||
"reply_content": "EXCLUDED.reply_content",
|
||||
"reply_image_url": "EXCLUDED.reply_image_url",
|
||||
"reply_once": "EXCLUDED.reply_once",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
cid, btoi(req.Enabled), req.ReplyContent, nullIfEmpty(req.ReplyImageURL), btoi(req.ReplyOnce))
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "保存失败")
|
||||
|
||||
@@ -175,10 +175,10 @@ func (s *Server) setAccountBindings(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
_, err := s.Store.DB.ExecContext(r.Context(),
|
||||
`INSERT INTO message_notifications (cookie_id, channel_id, enabled)
|
||||
VALUES (?, ?, ?)
|
||||
ON CONFLICT(cookie_id, channel_id) DO UPDATE SET
|
||||
enabled=excluded.enabled,
|
||||
updated_at=CURRENT_TIMESTAMP`,
|
||||
VALUES (?, ?, ?)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id", "channel_id"}, map[string]string{
|
||||
"enabled": "EXCLUDED.enabled",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
cid, req.ChannelID, btoi(enabled))
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "保存失败")
|
||||
|
||||
@@ -140,14 +140,14 @@ func (s *Server) setAIReply(w http.ResponseWriter, r *http.Request) {
|
||||
`INSERT INTO ai_reply_settings
|
||||
(cookie_id, ai_enabled, max_discount_percent, max_discount_amount,
|
||||
max_bargain_rounds, custom_prompts, updated_at)
|
||||
VALUES (?,?,?,?,?,?,CURRENT_TIMESTAMP)
|
||||
ON CONFLICT(cookie_id) DO UPDATE SET
|
||||
ai_enabled=excluded.ai_enabled,
|
||||
max_discount_percent=excluded.max_discount_percent,
|
||||
max_discount_amount=excluded.max_discount_amount,
|
||||
max_bargain_rounds=excluded.max_bargain_rounds,
|
||||
custom_prompts=excluded.custom_prompts,
|
||||
updated_at=CURRENT_TIMESTAMP`,
|
||||
VALUES (?,?,?,?,?,?,CURRENT_TIMESTAMP)`+db.DialectUpsert(s.Store.Dialect, []string{"cookie_id"}, map[string]string{
|
||||
"ai_enabled": "EXCLUDED.ai_enabled",
|
||||
"max_discount_percent": "EXCLUDED.max_discount_percent",
|
||||
"max_discount_amount": "EXCLUDED.max_discount_amount",
|
||||
"max_bargain_rounds": "EXCLUDED.max_bargain_rounds",
|
||||
"custom_prompts": "EXCLUDED.custom_prompts",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
cid, btoi(req.AIEnabled), req.MaxDiscountPercent, req.MaxDiscountAmount,
|
||||
req.MaxBargainRounds, nullIfEmpty(req.CustomPrompts))
|
||||
if err != nil {
|
||||
@@ -326,8 +326,13 @@ func (s *Server) setUserSetting(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusBadRequest, "请求格式错误")
|
||||
return
|
||||
}
|
||||
keyCol := db.DialectQuote(s.Store.Dialect, "key")
|
||||
_, err := s.Store.DB.ExecContext(r.Context(),
|
||||
`INSERT OR REPLACE INTO user_settings (user_id, key, value, updated_at) VALUES (?,?,?,CURRENT_TIMESTAMP)`,
|
||||
`INSERT INTO user_settings (user_id, `+keyCol+`, value, updated_at) VALUES (?,?,?,CURRENT_TIMESTAMP)`+
|
||||
db.DialectUpsert(s.Store.Dialect, []string{"user_id", keyCol}, map[string]string{
|
||||
"value": "EXCLUDED.value",
|
||||
"updated_at": "CURRENT_TIMESTAMP",
|
||||
}),
|
||||
sess.UserID, key, req.Value)
|
||||
if err != nil {
|
||||
writeErr(w, http.StatusInternalServerError, "保存失败")
|
||||
|
||||
Reference in new issue
Block a user