Docker 起 mysql:8 / postgres:16 实跑迁移+核心 CRUD,发现并修复三处阻断问题: - pgx 不支持 ? 占位符:新增 pgx_compat driver 包裹 pgx stdlib,把 ? 重写为 $N (internal/db/pgx_compat.go),driverPgx 指向它;全仓库 ? 写法零改动。 - pgx 对 BOOLEAN 严格按 bool 读写,与 int/boolToInt 写法不兼容:Postgres migration 的 BOOLEAN 列改 INTEGER 0/1,与 SQLite/MySQL 对齐 (00001/00004/00008)。 - pgx 不支持 Result.LastInsertId:新增 insertReturningID helper,Postgres 走 INSERT ... RETURNING id,SQLite/MySQL 走 LastInsertId (Cards/Keywords/ Notifications/Automation 的 INSERT 路径)。 附带 MySQL 部署加固:parseDBURL 自动补 multiStatements=true(缺失会静默只执行 首条迁移语句);base_url TEXT→VARCHAR(512)(strict mode 拒绝 TEXT+DEFAULT)。 新增 cmd/dbverify 三库迁移+核心 CRUD 烟囱测试工具。 验证:SQLite/MySQL/Postgres 三库 9 步 CRUD 全通过;go test ./... 与 go vet 通过。 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
125 lines
3.3 KiB
Go
125 lines
3.3 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
)
|
|
|
|
// Keyword 对应 keywords 表。
|
|
type Keyword struct {
|
|
Keyword string
|
|
Reply string
|
|
ItemID string
|
|
Type string // text/image
|
|
ImageURL string
|
|
}
|
|
|
|
// DefaultReply 对应 default_replies 表。
|
|
type DefaultReply struct {
|
|
Enabled bool
|
|
ReplyContent string
|
|
ReplyImageURL string
|
|
ReplyOnce bool
|
|
}
|
|
|
|
// ItemReply 对应 item_replay 表(指定商品回复)。
|
|
type ItemReply struct {
|
|
ItemID string
|
|
CookieID string
|
|
ReplyContent string
|
|
}
|
|
|
|
// Keywords 关键字操作。
|
|
type Keywords struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
}
|
|
|
|
// AllWithType 取某账号所有关键字(含类型/图片)。
|
|
func (k *Keywords) AllWithType(ctx context.Context, cookieID string) ([]Keyword, error) {
|
|
rows, err := k.DB.QueryContext(ctx,
|
|
`SELECT keyword, reply, COALESCE(item_id,''), COALESCE(type,'text'), COALESCE(image_url,'')
|
|
FROM keywords WHERE cookie_id=?`, cookieID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []Keyword
|
|
for rows.Next() {
|
|
var kw Keyword
|
|
if err := rows.Scan(&kw.Keyword, &kw.Reply, &kw.ItemID, &kw.Type, &kw.ImageURL); err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, kw)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
// DefaultReplies 默认回复操作。
|
|
type DefaultReplies struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
}
|
|
|
|
// Get 取某账号默认回复设置。不存在返回 ErrNotFound。
|
|
func (d *DefaultReplies) Get(ctx context.Context, cookieID string) (*DefaultReply, error) {
|
|
var dr DefaultReply
|
|
var enabled, replyOnce int
|
|
var content, imageURL sql.NullString
|
|
err := d.DB.QueryRowContext(ctx,
|
|
`SELECT enabled, reply_content, reply_image_url, reply_once FROM default_replies WHERE cookie_id=?`,
|
|
cookieID).Scan(&enabled, &content, &imageURL, &replyOnce)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
dr.Enabled = enabled != 0
|
|
dr.ReplyContent = content.String
|
|
dr.ReplyImageURL = imageURL.String
|
|
dr.ReplyOnce = replyOnce != 0
|
|
return &dr, nil
|
|
}
|
|
|
|
// HasRecord 是否已对该 chat_id 回复过(reply_once 用)。
|
|
func (d *DefaultReplies) HasRecord(ctx context.Context, cookieID, chatID string) bool {
|
|
var n int
|
|
err := d.DB.QueryRowContext(ctx,
|
|
`SELECT 1 FROM default_reply_records WHERE cookie_id=? AND chat_id=? LIMIT 1`,
|
|
cookieID, chatID).Scan(&n)
|
|
return err == nil
|
|
}
|
|
|
|
// AddRecord 记录已回复(reply_once 防重复)。
|
|
func (d *DefaultReplies) AddRecord(ctx context.Context, cookieID, chatID string) error {
|
|
_, err := d.DB.ExecContext(ctx,
|
|
dialectInsertIgnorePrefix(d.Dialect)+` INTO default_reply_records (cookie_id, chat_id) VALUES (?, ?)`+dialectInsertIgnore(d.Dialect, []string{"cookie_id", "chat_id"}),
|
|
cookieID, chatID)
|
|
return err
|
|
}
|
|
|
|
// ItemReplies 指定商品回复操作。
|
|
type ItemReplies struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
}
|
|
|
|
// Get 取某账号某商品的指定回复。
|
|
func (i *ItemReplies) Get(ctx context.Context, cookieID, itemID string) (*ItemReply, error) {
|
|
var ir ItemReply
|
|
var content sql.NullString
|
|
err := i.DB.QueryRowContext(ctx,
|
|
`SELECT item_id, cookie_id, reply_content FROM item_replay WHERE cookie_id=? AND item_id=?`,
|
|
cookieID, itemID).Scan(&ir.ItemID, &ir.CookieID, &content)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
ir.ReplyContent = content.String
|
|
return &ir, nil
|
|
}
|