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:
ChristandClaude Opus 4.8 committed 2026-07-06 12:58:23 +08:00
1 parent 0fa5d6617c
commit 36a7f74154
20 files changed
+511 -423

No files matched your search

+7 -4
View File
@@ -36,7 +36,10 @@ type Order struct {
}
// Orders 订单操作。
type Orders struct{ DB *sql.DB }
type Orders struct {
DB *sql.DB
Dialect Dialect
}
// Upsert 插入或更新订单。仅更新提供的非零字段(INSERT OR IGNORE 占位 + 动态 UPDATE)。
// 注:orders.version 列当前仅作占位读取,不参与并发控制——SQLite 单写者序列化写入,
@@ -45,10 +48,10 @@ func (o *Orders) Upsert(ctx context.Context, orderID string, opts OrderUpsertOpt
if orderID == "" {
return errors.New("order_id 不能为空")
}
// 先尝试 INSERT OR IGNORE 占位。
// 先尝试插入占位(冲突忽略)。order_id 是主键。
_, err := o.DB.ExecContext(ctx,
`INSERT OR IGNORE INTO orders (order_id, item_id, buyer_id, cookie_id, order_status, version)
VALUES (?, ?, ?, ?, 'unknown', 1)`,
dialectInsertIgnorePrefix(o.Dialect)+` INTO orders (order_id, item_id, buyer_id, cookie_id, order_status, version)
VALUES (?, ?, ?, ?, 'unknown', 1)`+dialectInsertIgnore(o.Dialect, []string{"order_id"}),
orderID, opts.ItemID, opts.BuyerID, opts.CookieID)
if err != nil {
return err