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

86 lines
4.1 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
-- 自动化中心 schema(MySQL 版,与 SQLite 00007 对齐)。
CREATE TABLE IF NOT EXISTS automation_rules (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
user_id BIGINT NOT NULL,
cookie_id VARCHAR(255) NOT NULL DEFAULT '',
item_id VARCHAR(255) NOT NULL DEFAULT '',
name VARCHAR(255) NOT NULL,
trigger_type VARCHAR(64) NOT NULL,
enabled INTEGER NOT NULL DEFAULT 1,
priority INTEGER NOT NULL DEFAULT 100,
config_json LONGTEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT fk_automation_rules_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
CONSTRAINT fk_automation_rules_cookie FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE INDEX idx_automation_rules_user ON automation_rules(user_id, created_at DESC);
CREATE INDEX idx_automation_rules_match ON automation_rules(cookie_id, item_id, trigger_type, enabled, priority);
CREATE TABLE IF NOT EXISTS automation_rule_actions (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
rule_id BIGINT NOT NULL,
action_type VARCHAR(64) NOT NULL,
card_id BIGINT,
delivery_count INTEGER NOT NULL DEFAULT 1,
message_template TEXT NOT NULL,
delay_seconds INTEGER NOT NULL DEFAULT 0,
config_json LONGTEXT NOT NULL,
enabled INTEGER NOT NULL DEFAULT 1,
sort_order INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT fk_automation_rule_actions_rule FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
CONSTRAINT fk_automation_rule_actions_card FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE INDEX idx_automation_rule_actions_rule ON automation_rule_actions(rule_id, sort_order, id);
CREATE TABLE IF NOT EXISTS automation_runs (
id BIGINT AUTO_INCREMENT PRIMARY KEY,
rule_id BIGINT NOT NULL,
cookie_id VARCHAR(255) NOT NULL,
item_id VARCHAR(255) NOT NULL DEFAULT '',
order_id VARCHAR(255) NOT NULL DEFAULT '',
buyer_id VARCHAR(255) NOT NULL DEFAULT '',
chat_id VARCHAR(255) NOT NULL DEFAULT '',
trigger_type VARCHAR(64) NOT NULL,
trigger_key VARCHAR(255) NOT NULL,
status VARCHAR(32) NOT NULL,
sent_count INTEGER NOT NULL DEFAULT 0,
error_message TEXT NOT NULL,
raw_event_json LONGTEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
CONSTRAINT fk_automation_runs_rule FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
UNIQUE KEY uk_automation_runs (rule_id, trigger_key)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE INDEX idx_automation_runs_cookie_order ON automation_runs(cookie_id, order_id, trigger_type);
CREATE INDEX idx_automation_runs_status ON automation_runs(status, updated_at);
ALTER TABLE orders ADD COLUMN paid_at VARCHAR(64) DEFAULT '';
ALTER TABLE orders ADD COLUMN shipped_at VARCHAR(64) DEFAULT '';
ALTER TABLE orders ADD COLUMN completed_at VARCHAR(64) DEFAULT '';
ALTER TABLE orders ADD COLUMN buyer_reviewed_at VARCHAR(64) DEFAULT '';
ALTER TABLE orders ADD COLUMN last_review_request_at VARCHAR(64) DEFAULT '';
ALTER TABLE orders ADD COLUMN review_request_count INTEGER NOT NULL DEFAULT 0;
ALTER TABLE item_publish_batch_rows ADD COLUMN automation_json LONGTEXT NOT NULL;
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS automation_runs;
DROP TABLE IF EXISTS automation_rule_actions;
DROP TABLE IF EXISTS automation_rules;
ALTER TABLE item_publish_batch_rows DROP COLUMN automation_json;
ALTER TABLE orders DROP COLUMN review_request_count;
ALTER TABLE orders DROP COLUMN last_review_request_at;
ALTER TABLE orders DROP COLUMN buyer_reviewed_at;
ALTER TABLE orders DROP COLUMN completed_at;
ALTER TABLE orders DROP COLUMN shipped_at;
ALTER TABLE orders DROP COLUMN paid_at;
-- +goose StatementEnd