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>
48 lines
1.6 KiB
Go
48 lines
1.6 KiB
Go
package db
|
|
|
|
import "database/sql"
|
|
|
|
// Store 聚合各 repository,供上层(HTTP server、account supervisor 等)统一持有。
|
|
type Store struct {
|
|
DB *sql.DB
|
|
Dialect Dialect
|
|
Users *Users
|
|
Sessions *Sessions
|
|
Cookies *Cookies
|
|
Items *Items
|
|
Cards *Cards
|
|
Automation *AutomationRules
|
|
Orders *Orders
|
|
Keywords *Keywords
|
|
DefaultReps *DefaultReplies
|
|
ItemReps *ItemReplies
|
|
AIReply *AIReply
|
|
Notifications *Notifications
|
|
Settings *SystemSettings
|
|
WSMessages *WSMessageStore
|
|
PublishBatches *ItemPublishBatches
|
|
}
|
|
|
|
// NewStore 基于 *sql.DB 构造聚合 store。dialect 用于业务 SQL 方言分支。
|
|
func NewStore(db *sql.DB, dialect Dialect) *Store {
|
|
return &Store{
|
|
DB: db,
|
|
Dialect: dialect,
|
|
Users: &Users{DB: db},
|
|
Sessions: &Sessions{DB: db},
|
|
Cookies: &Cookies{DB: db, Dialect: dialect},
|
|
Items: &Items{DB: db, Dialect: dialect},
|
|
Cards: &Cards{DB: db, Dialect: dialect},
|
|
Automation: &AutomationRules{DB: db, Dialect: dialect},
|
|
Orders: &Orders{DB: db, Dialect: dialect},
|
|
Keywords: &Keywords{DB: db, Dialect: dialect},
|
|
DefaultReps: &DefaultReplies{DB: db, Dialect: dialect},
|
|
ItemReps: &ItemReplies{DB: db, Dialect: dialect},
|
|
AIReply: &AIReply{DB: db},
|
|
Notifications: &Notifications{DB: db, Dialect: dialect},
|
|
Settings: &SystemSettings{DB: db, Dialect: dialect},
|
|
WSMessages: &WSMessageStore{DB: db},
|
|
PublishBatches: &ItemPublishBatches{DB: db},
|
|
}
|
|
}
|