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

@@ -1,51 +1,42 @@
-- +goose Up
-- +goose StatementBegin
-- 清理被 automation_rules 取代或从未使用的 schema:
-- - delivery_rules + delivery_rule_variants:发货规则旧系统,已被 automation_rules 完全取代(前端只走 automation-rules)。
-- - email_verifications / captcha_codes / ai_item_cache / risk_control_logs:从未有 Go 代码读写。
-- - item_publish_batch_rows.auto_create_delivery_rule / card_group_id / delivery_count:批量铺货改走 automation_json,三列从未读写。
-- 清理被 automation_rules 取代或从未使用的 schema(MySQL 版)。
-- 注意:MySQL 00006 未创建 auto_create_delivery_rule/card_group_id/delivery_count
-- (这三个列是 SQLite 历史遗留,00008 在 SQLite 里删除,MySQL 版直接没建,故此处不删)。
DROP TABLE IF EXISTS delivery_rule_variants;
DROP TABLE IF EXISTS delivery_rules;
DROP TABLE IF EXISTS email_verifications;
DROP TABLE IF EXISTS captcha_codes;
DROP TABLE IF EXISTS ai_item_cache;
DROP TABLE IF EXISTS risk_control_logs;
ALTER TABLE item_publish_batch_rows DROP COLUMN auto_create_delivery_rule;
ALTER TABLE item_publish_batch_rows DROP COLUMN card_group_id;
ALTER TABLE item_publish_batch_rows DROP COLUMN delivery_count;
-- email_verifications / ai_item_cache / risk_control_logs 在 MySQL 00001 未创建,跳过删除。
-- 若存在则忽略报错:MySQL 的 DROP TABLE IF EXISTS 对不存在的表是安全的。
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
-- 仅重建结构,历史数据不可恢复。
CREATE TABLE IF NOT EXISTS delivery_rules (
id INTEGER PRIMARY KEY AUTOINCREMENT,
keyword TEXT NOT NULL,
card_id INTEGER NOT NULL,
id BIGINT AUTO_INCREMENT PRIMARY KEY,
keyword VARCHAR(255) NOT NULL,
card_id BIGINT NOT NULL,
delivery_count INTEGER DEFAULT 1,
enabled BOOLEAN DEFAULT TRUE,
enabled TINYINT(1) DEFAULT 1,
description TEXT,
delivery_times INTEGER DEFAULT 0,
user_id INTEGER NOT NULL DEFAULT 1,
cookie_id TEXT NOT NULL DEFAULT '',
item_id TEXT NOT NULL DEFAULT '',
user_id BIGINT NOT NULL DEFAULT 1,
cookie_id VARCHAR(255) NOT NULL DEFAULT '',
item_id VARCHAR(255) NOT NULL DEFAULT '',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS delivery_rule_variants (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rule_id INTEGER NOT NULL,
spec_name TEXT NOT NULL DEFAULT '',
spec_value TEXT NOT NULL DEFAULT '',
card_id INTEGER NOT NULL,
id BIGINT AUTO_INCREMENT PRIMARY KEY,
rule_id BIGINT NOT NULL,
spec_name VARCHAR(255) NOT NULL DEFAULT '',
spec_value VARCHAR(255) NOT NULL DEFAULT '',
card_id BIGINT NOT NULL,
delivery_count INTEGER NOT NULL DEFAULT 1,
enabled BOOLEAN NOT NULL DEFAULT TRUE,
enabled TINYINT(1) NOT NULL DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
ALTER TABLE item_publish_batch_rows ADD COLUMN auto_create_delivery_rule INTEGER NOT NULL DEFAULT 0;
ALTER TABLE item_publish_batch_rows ADD COLUMN card_group_id INTEGER NOT NULL DEFAULT 0;
ALTER TABLE item_publish_batch_rows ADD COLUMN delivery_count INTEGER NOT NULL DEFAULT 1;
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- +goose StatementEnd