3 Commits
Author SHA1 Message Date
ChristandClaude Opus 4.8 62705c524d fix(db): 真实容器验证 Postgres/MySQL 方言修复
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>
2026-07-06 16:51:38 +08:00
ChristandClaude Opus 4.8 36a7f74154 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 <noreply@anthropic.com>
2026-07-06 12:58:23 +08:00
ChristandClaude Opus 4.8 0fa5d6617c refactor(db): 阶段A - driver 抽象 + SQLite 保持工作
- db.Open 改为接受 URL(sqlite:// mysql:// postgres://),返回 (db, dialect, err)
- 向后兼容:不含 scheme 的路径仍按 SQLite 文件处理
- migration 移到 migrations/sqlite/ 子目录,embed 包含三个方言目录
- Migrate 按方言选择子目录 + goose dialect
- Store 加 Dialect 字段,NewStore(db, dialect)
- cmd/server 加 -db-url 和 DATABASE_URL 环境变量(优先级:env > -db-url > -db)
- cmd/init-admin 同步支持 URL
- 新增依赖 pgx/v5、go-sql-driver/mysql
- mysql/postgres migration 目录暂为 SQLite 副本占位,阶段B/C 替换
- 所有测试调用方更新为新签名

验证:现有测试全过(SQLite 回归),SQLite 行为不变

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 12:35:01 +08:00