refactor(db): 阶段C - Postgres migration 补全

- migrations/postgres/ 8 个文件改为 Postgres 方言:
  BIGSERIAL、原生 BOOLEAN、TEXT 主键、ON CONFLICT DO NOTHING/UPDATE、
  "key" 双引号处理保留字
- 跳过 SQLite 历史遗留列(与 MySQL 版一致)
- 00005 ws_messages 空操作(00001 已建)

至此三套数据库 migration 全部就位,业务 SQL 通过方言适配器统一处理。
SQLite 全测试通过,MySQL/PG 待真实实例验证。

Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
ChristandClaude Opus 4.8 committed 2026-07-06 13:02:30 +08:00
1 parent 36a7f74154
commit fb83aacb8b
8 files changed
+106 -199

No files matched your search

@@ -1,16 +1,11 @@
-- +goose Up
-- +goose StatementBegin
-- 自动化中心 schema。
--
-- 设计目标:
-- 1. 不再把“自动发货”写死成唯一业务,付款发货、评价赠品、超时求评价都统一为自动化规则。
-- 2. 触发来源可以是 WS 系统事件、计划任务或后台手动触发;规则和动作统一落在 automation_rules/actions。
-- 3. automation_runs 使用 trigger_key 做持久化防重,避免 WS 重放、服务重启或计划任务重复扫描导致重复发送。
-- 自动化中心 schema(PostgreSQL 版,与 SQLite 00007 对齐)。
CREATE TABLE IF NOT EXISTS automation_rules (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
cookie_id TEXT NOT NULL DEFAULT '',
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
cookie_id TEXT NOT NULL DEFAULT '' REFERENCES cookies(id) ON DELETE CASCADE,
item_id TEXT NOT NULL DEFAULT '',
name TEXT NOT NULL,
trigger_type TEXT NOT NULL,
@@ -18,18 +13,16 @@ CREATE TABLE IF NOT EXISTS automation_rules (
priority INTEGER NOT NULL DEFAULT 100,
config_json TEXT NOT NULL DEFAULT '{}',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_automation_rules_user ON automation_rules(user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_automation_rules_match ON automation_rules(cookie_id, item_id, trigger_type, enabled, priority);
CREATE TABLE IF NOT EXISTS automation_rule_actions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rule_id INTEGER NOT NULL,
id BIGSERIAL PRIMARY KEY,
rule_id BIGINT NOT NULL REFERENCES automation_rules(id) ON DELETE CASCADE,
action_type TEXT NOT NULL,
card_id INTEGER,
card_id BIGINT REFERENCES cards(id) ON DELETE RESTRICT,
delivery_count INTEGER NOT NULL DEFAULT 1,
message_template TEXT NOT NULL DEFAULT '',
delay_seconds INTEGER NOT NULL DEFAULT 0,
@@ -37,15 +30,13 @@ CREATE TABLE IF NOT EXISTS automation_rule_actions (
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,
FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE RESTRICT
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_automation_rule_actions_rule ON automation_rule_actions(rule_id, sort_order, id);
CREATE TABLE IF NOT EXISTS automation_runs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
rule_id INTEGER NOT NULL,
id BIGSERIAL PRIMARY KEY,
rule_id BIGINT NOT NULL REFERENCES automation_rules(id) ON DELETE CASCADE,
cookie_id TEXT NOT NULL,
item_id TEXT NOT NULL DEFAULT '',
order_id TEXT NOT NULL DEFAULT '',
@@ -59,7 +50,6 @@ CREATE TABLE IF NOT EXISTS automation_runs (
raw_event_json TEXT NOT NULL DEFAULT '{}',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (rule_id) REFERENCES automation_rules(id) ON DELETE CASCADE,
UNIQUE(rule_id, trigger_key)
);
CREATE INDEX IF NOT EXISTS idx_automation_runs_cookie_order ON automation_runs(cookie_id, order_id, trigger_type);