Files
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

43 lines
1.8 KiB
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- +goose Up
-- +goose StatementBegin
-- 清理被 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 captcha_codes;
-- 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 BIGINT AUTO_INCREMENT PRIMARY KEY,
keyword VARCHAR(255) NOT NULL,
card_id BIGINT NOT NULL,
delivery_count INTEGER DEFAULT 1,
enabled TINYINT(1) DEFAULT 1,
description TEXT,
delivery_times INTEGER DEFAULT 0,
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 ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS delivery_rule_variants (
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 TINYINT(1) NOT NULL DEFAULT 1,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
-- +goose StatementEnd