From fb83aacb8bc5a56616043c76e66243a5ee3861ea Mon Sep 17 00:00:00 2001 From: Christ Date: Mon, 6 Jul 2026 13:02:30 +0800 Subject: [PATCH] =?UTF-8?q?refactor(db):=20=E9=98=B6=E6=AE=B5C=20-=20Postg?= =?UTF-8?q?res=20migration=20=E8=A1=A5=E5=85=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- .../db/migrations/postgres/00001_init.sql | 179 ++++++------------ .../postgres/00002_cookie_profile.sql | 3 +- .../postgres/00003_global_ai_settings.sql | 7 +- .../postgres/00004_delivery_rule_variants.sql | 18 +- .../migrations/postgres/00005_ws_messages.sql | 21 +- .../postgres/00006_item_publish_batches.sql | 15 +- .../postgres/00007_automation_center.sql | 32 ++-- .../postgres/00008_drop_dead_schema.sql | 30 ++- 8 files changed, 106 insertions(+), 199 deletions(-) diff --git a/internal/db/migrations/postgres/00001_init.sql b/internal/db/migrations/postgres/00001_init.sql index c9a2ec1..a5bb12d 100644 --- a/internal/db/migrations/postgres/00001_init.sql +++ b/internal/db/migrations/postgres/00001_init.sql @@ -1,20 +1,15 @@ -- +goose Up -- +goose StatementBegin --- 闲鱼管家初始 schema。 --- 把历史上 12+ 处运行时 ALTER TABLE 补齐到 CREATE,并修复 schema 不一致: --- - orders.system_shipped:原 CREATE 缺失却被 insert_or_update_order 引用 → 补齐 --- - orders.receiver_city:原为运行时补齐 → 补齐 --- - delivery_rules.user_id:原 CREATE 缺失却被 create_delivery_rule 引用 → 补齐 --- - cards.user_id / delay_seconds / image_url:历史 ALTER → 补齐 --- - keywords.item_id / type / image_url:历史 ALTER → 补齐 --- - item_info.multi_quantity_delivery:历史 ALTER → 补齐 --- - default_replies.reply_once / reply_image_url:历史 ALTER → 补齐 --- - users.is_admin:历史 ALTER → 补齐 --- 注:老库导入对齐在 Phase 6 单独脚本处理;本迁移面向全新库。 +-- 闲鱼管家初始 schema(PostgreSQL 版)。 +-- 与 SQLite 00001 对齐,差异: +-- - INTEGER PRIMARY KEY AUTOINCREMENT → BIGSERIAL PRIMARY KEY +-- - BOOLEAN 原生支持(无需 boolToInt,但 Go 代码传 0/1 也兼容) +-- - INSERT OR IGNORE → ON CONFLICT DO NOTHING(业务代码用方言适配器) +-- - CURRENT_TIMESTAMP 一致;updated_at 不设 ON UPDATE(业务代码手动 SET) -- 认证与会话 CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, + id BIGSERIAL PRIMARY KEY, username TEXT UNIQUE NOT NULL, email TEXT UNIQUE NOT NULL, password_hash TEXT NOT NULL, @@ -26,25 +21,16 @@ CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS sessions ( session_id TEXT PRIMARY KEY, - user_id INTEGER NOT NULL, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, username TEXT NOT NULL, - is_admin INTEGER DEFAULT 0, - expires_at INTEGER NOT NULL, - created_at INTEGER NOT NULL + is_admin BOOLEAN DEFAULT FALSE, + expires_at BIGINT NOT NULL, + created_at BIGINT NOT NULL ); CREATE INDEX IF NOT EXISTS idx_sessions_user_id ON sessions(user_id); -CREATE TABLE IF NOT EXISTS email_verifications ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - email TEXT NOT NULL, - code TEXT NOT NULL, - expires_at TIMESTAMP NOT NULL, - used BOOLEAN DEFAULT FALSE, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); - CREATE TABLE IF NOT EXISTS captcha_codes ( - id INTEGER PRIMARY KEY AUTOINCREMENT, + id BIGSERIAL PRIMARY KEY, session_id TEXT NOT NULL, code TEXT NOT NULL, expires_at TIMESTAMP NOT NULL, @@ -55,40 +41,37 @@ CREATE TABLE IF NOT EXISTS captcha_codes ( CREATE TABLE IF NOT EXISTS cookies ( id TEXT PRIMARY KEY, value TEXT NOT NULL, - user_id INTEGER NOT NULL, - auto_confirm INTEGER DEFAULT 1, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + auto_confirm BOOLEAN DEFAULT TRUE, remark TEXT DEFAULT '', pause_duration INTEGER DEFAULT 10, username TEXT DEFAULT '', password TEXT DEFAULT '', - show_browser INTEGER DEFAULT 0, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + show_browser BOOLEAN DEFAULT FALSE, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX IF NOT EXISTS idx_cookies_user_id ON cookies(user_id); CREATE TABLE IF NOT EXISTS cookie_status ( - cookie_id TEXT PRIMARY KEY, + cookie_id TEXT PRIMARY KEY REFERENCES cookies(id) ON DELETE CASCADE, enabled BOOLEAN DEFAULT TRUE, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- 关键字回复 CREATE TABLE IF NOT EXISTS keywords ( - cookie_id TEXT, + cookie_id TEXT REFERENCES cookies(id) ON DELETE CASCADE, keyword TEXT, reply TEXT, item_id TEXT, type TEXT DEFAULT 'text', - image_url TEXT, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + image_url TEXT ); CREATE INDEX IF NOT EXISTS idx_keywords_cookie_id ON keywords(cookie_id); -- AI 回复 CREATE TABLE IF NOT EXISTS ai_reply_settings ( - cookie_id TEXT PRIMARY KEY, + cookie_id TEXT PRIMARY KEY REFERENCES cookies(id) ON DELETE CASCADE, ai_enabled BOOLEAN DEFAULT FALSE, model_name TEXT DEFAULT 'qwen-plus', api_key TEXT, @@ -98,13 +81,12 @@ CREATE TABLE IF NOT EXISTS ai_reply_settings ( max_bargain_rounds INTEGER DEFAULT 3, custom_prompts TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS ai_conversations ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - cookie_id TEXT NOT NULL, + id BIGSERIAL PRIMARY KEY, + cookie_id TEXT NOT NULL REFERENCES cookies(id) ON DELETE CASCADE, chat_id TEXT NOT NULL, user_id TEXT NOT NULL, item_id TEXT NOT NULL, @@ -112,21 +94,12 @@ CREATE TABLE IF NOT EXISTS ai_conversations ( content TEXT NOT NULL, intent TEXT, bargain_count INTEGER DEFAULT 0, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies (id) ON DELETE CASCADE + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -CREATE TABLE IF NOT EXISTS ai_item_cache ( - item_id TEXT PRIMARY KEY, - data TEXT NOT NULL, - price REAL, - description TEXT, - last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP -); - --- 卡券与发货规则 +-- 卡券 CREATE TABLE IF NOT EXISTS cards ( - id INTEGER PRIMARY KEY AUTOINCREMENT, + id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, type TEXT NOT NULL CHECK (type IN ('api', 'text', 'data', 'image')), api_config TEXT, @@ -139,26 +112,23 @@ CREATE TABLE IF NOT EXISTS cards ( is_multi_spec BOOLEAN DEFAULT FALSE, spec_name TEXT, spec_value TEXT, - user_id INTEGER NOT NULL DEFAULT 1, + user_id BIGINT NOT NULL DEFAULT 1 REFERENCES users(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES users (id) + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX IF NOT EXISTS idx_cards_user_id ON cards(user_id); CREATE TABLE IF NOT EXISTS delivery_rules ( - id INTEGER PRIMARY KEY AUTOINCREMENT, + id BIGSERIAL PRIMARY KEY, keyword TEXT NOT NULL, - card_id INTEGER NOT NULL, + card_id INTEGER NOT NULL REFERENCES cards(id) ON DELETE CASCADE, delivery_count INTEGER DEFAULT 1, enabled BOOLEAN DEFAULT TRUE, description TEXT, delivery_times INTEGER DEFAULT 0, - user_id INTEGER NOT NULL DEFAULT 1, + user_id BIGINT NOT NULL DEFAULT 1 REFERENCES users(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE CASCADE, - FOREIGN KEY (user_id) REFERENCES users(id) + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX IF NOT EXISTS idx_delivery_rules_user_id ON delivery_rules(user_id); CREATE INDEX IF NOT EXISTS idx_delivery_rules_card_id ON delivery_rules(card_id); @@ -173,7 +143,7 @@ CREATE TABLE IF NOT EXISTS orders ( quantity TEXT, amount TEXT, order_status TEXT DEFAULT 'unknown', - cookie_id TEXT, + cookie_id TEXT REFERENCES cookies(id) ON DELETE CASCADE, is_bargain INTEGER DEFAULT 0, receiver_name TEXT DEFAULT '', receiver_phone TEXT DEFAULT '', @@ -183,15 +153,14 @@ CREATE TABLE IF NOT EXISTS orders ( chat_id TEXT DEFAULT '', system_shipped INTEGER DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX IF NOT EXISTS idx_orders_cookie_id ON orders(cookie_id); -- 商品信息 CREATE TABLE IF NOT EXISTS item_info ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - cookie_id TEXT NOT NULL, + id BIGSERIAL PRIMARY KEY, + cookie_id TEXT NOT NULL REFERENCES cookies(id) ON DELETE CASCADE, item_id TEXT NOT NULL, item_title TEXT, item_description TEXT, @@ -202,40 +171,37 @@ CREATE TABLE IF NOT EXISTS item_info ( multi_quantity_delivery BOOLEAN DEFAULT FALSE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE, UNIQUE(cookie_id, item_id) ); -- 默认回复 CREATE TABLE IF NOT EXISTS default_replies ( - cookie_id TEXT PRIMARY KEY, + cookie_id TEXT PRIMARY KEY REFERENCES cookies(id) ON DELETE CASCADE, enabled BOOLEAN DEFAULT FALSE, reply_content TEXT, reply_image_url TEXT, reply_once BOOLEAN DEFAULT FALSE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); -- WS 原始消息记录 CREATE TABLE IF NOT EXISTS ws_messages ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - cookie_id TEXT NOT NULL, + id BIGSERIAL PRIMARY KEY, + cookie_id TEXT NOT NULL REFERENCES cookies(id) ON DELETE CASCADE, direction TEXT NOT NULL DEFAULT 'in', raw_text TEXT NOT NULL DEFAULT '', parsed_json TEXT NOT NULL DEFAULT '', message_kind TEXT NOT NULL DEFAULT '', parse_status TEXT NOT NULL DEFAULT 'raw', error TEXT NOT NULL DEFAULT '', - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX IF NOT EXISTS idx_ws_messages_cookie_id_created_at ON ws_messages(cookie_id, created_at DESC); -- 指定商品回复 CREATE TABLE IF NOT EXISTS item_replay ( - id INTEGER PRIMARY KEY AUTOINCREMENT, + id BIGSERIAL PRIMARY KEY, item_id TEXT NOT NULL, cookie_id TEXT NOT NULL, reply_content TEXT NOT NULL, @@ -246,42 +212,38 @@ CREATE INDEX IF NOT EXISTS idx_item_replay_cookie_item ON item_replay(cookie_id, -- 默认回复记录(防重复) CREATE TABLE IF NOT EXISTS default_reply_records ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - cookie_id TEXT NOT NULL, + id BIGSERIAL PRIMARY KEY, + cookie_id TEXT NOT NULL REFERENCES cookies(id) ON DELETE CASCADE, chat_id TEXT NOT NULL, replied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - UNIQUE(cookie_id, chat_id), - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + UNIQUE(cookie_id, chat_id) ); -- 通知 CREATE TABLE IF NOT EXISTS notification_channels ( - id INTEGER PRIMARY KEY AUTOINCREMENT, + id BIGSERIAL PRIMARY KEY, name TEXT NOT NULL, type TEXT NOT NULL CHECK (type IN ('qq','ding_talk','dingtalk','feishu','lark','bark','email','webhook','wechat','telegram')), config TEXT NOT NULL, enabled BOOLEAN DEFAULT TRUE, - user_id INTEGER NOT NULL DEFAULT 1, + user_id BIGINT NOT NULL DEFAULT 1 REFERENCES users(id), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES users(id) + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS message_notifications ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - cookie_id TEXT NOT NULL, - channel_id INTEGER NOT NULL, + id BIGSERIAL PRIMARY KEY, + cookie_id TEXT NOT NULL REFERENCES cookies(id) ON DELETE CASCADE, + channel_id BIGINT NOT NULL REFERENCES notification_channels(id) ON DELETE CASCADE, enabled BOOLEAN DEFAULT TRUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE, - FOREIGN KEY (channel_id) REFERENCES notification_channels(id) ON DELETE CASCADE, UNIQUE(cookie_id, channel_id) ); --- 系统设置 +-- 系统设置(key 是 PG 保留字,需双引号) CREATE TABLE IF NOT EXISTS system_settings ( - key TEXT PRIMARY KEY, + "key" TEXT PRIMARY KEY, value TEXT NOT NULL, description TEXT, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP @@ -289,33 +251,18 @@ CREATE TABLE IF NOT EXISTS system_settings ( -- 用户设置 CREATE TABLE IF NOT EXISTS user_settings ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER NOT NULL, - key TEXT NOT NULL, + id BIGSERIAL PRIMARY KEY, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, + "key" TEXT NOT NULL, value TEXT NOT NULL, description TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE, - UNIQUE(user_id, key) -); - --- 风控日志 -CREATE TABLE IF NOT EXISTS risk_control_logs ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - cookie_id TEXT NOT NULL, - event_type TEXT NOT NULL DEFAULT 'slider_captcha', - event_description TEXT, - processing_result TEXT, - processing_status TEXT DEFAULT 'processing', - error_message TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE + UNIQUE(user_id, "key") ); -- 默认系统设置(不含管理员口令——由 init-admin CLI 初始化) -INSERT OR IGNORE INTO system_settings (key, value, description) VALUES +INSERT INTO system_settings ("key", value, description) VALUES ('theme_color', 'blue', '主题颜色'), ('registration_enabled', 'true', '是否开启用户注册'), ('show_default_login_info', 'true', '是否显示默认登录信息'), @@ -330,11 +277,12 @@ INSERT OR IGNORE INTO system_settings (key, value, description) VALUES ('qq_reply_secret_key', '', 'QQ回复消息API秘钥(必须显式配置,无默认值)'), ('item_sync_enabled', 'true', '是否启用定时自动同步商品'), ('item_sync_interval', '600', '商品同步间隔时间(秒)'), - ('item_sync_max_pages', '5', '每次最多同步的页数'); + ('item_sync_max_pages', '5', '每次最多同步的页数') +ON CONFLICT ("key") DO NOTHING; -- +goose StatementEnd -- +goose Down -DROP TABLE IF EXISTS risk_control_logs; +-- +goose StatementBegin DROP TABLE IF EXISTS user_settings; DROP TABLE IF EXISTS system_settings; DROP TABLE IF EXISTS message_notifications; @@ -346,13 +294,12 @@ DROP TABLE IF EXISTS item_info; DROP TABLE IF EXISTS orders; DROP TABLE IF EXISTS delivery_rules; DROP TABLE IF EXISTS cards; -DROP TABLE IF EXISTS ai_item_cache; DROP TABLE IF EXISTS ai_conversations; DROP TABLE IF EXISTS ai_reply_settings; DROP TABLE IF EXISTS keywords; DROP TABLE IF EXISTS cookie_status; DROP TABLE IF EXISTS cookies; DROP TABLE IF EXISTS captcha_codes; -DROP TABLE IF EXISTS email_verifications; DROP TABLE IF EXISTS sessions; DROP TABLE IF EXISTS users; +-- +goose StatementEnd diff --git a/internal/db/migrations/postgres/00002_cookie_profile.sql b/internal/db/migrations/postgres/00002_cookie_profile.sql index 3a0e9b7..8f9ed19 100644 --- a/internal/db/migrations/postgres/00002_cookie_profile.sql +++ b/internal/db/migrations/postgres/00002_cookie_profile.sql @@ -3,8 +3,7 @@ ALTER TABLE cookies ADD COLUMN nickname TEXT DEFAULT ''; ALTER TABLE cookies ADD COLUMN avatar_url TEXT DEFAULT ''; ALTER TABLE cookies ADD COLUMN updated_at TIMESTAMP; -UPDATE cookies -SET updated_at = COALESCE(updated_at, created_at, CURRENT_TIMESTAMP); +UPDATE cookies SET updated_at = COALESCE(updated_at, created_at, CURRENT_TIMESTAMP); -- +goose StatementEnd -- +goose Down diff --git a/internal/db/migrations/postgres/00003_global_ai_settings.sql b/internal/db/migrations/postgres/00003_global_ai_settings.sql index 1041a7a..999fab5 100644 --- a/internal/db/migrations/postgres/00003_global_ai_settings.sql +++ b/internal/db/migrations/postgres/00003_global_ai_settings.sql @@ -1,8 +1,9 @@ -- +goose Up -INSERT OR IGNORE INTO system_settings (key, value, description) VALUES +INSERT INTO system_settings ("key", value, description) VALUES ('ai_api_url', 'https://dashscope.aliyuncs.com/compatible-mode/v1', 'AI OpenAI兼容API地址'), ('ai_api_key', '', 'AI API Key'), - ('ai_model', 'qwen-plus', 'AI模型名称'); + ('ai_model', 'qwen-plus', 'AI模型名称') +ON CONFLICT ("key") DO NOTHING; -- +goose Down -DELETE FROM system_settings WHERE key IN ('ai_api_url', 'ai_api_key', 'ai_model'); +DELETE FROM system_settings WHERE "key" IN ('ai_api_url', 'ai_api_key', 'ai_model'); diff --git a/internal/db/migrations/postgres/00004_delivery_rule_variants.sql b/internal/db/migrations/postgres/00004_delivery_rule_variants.sql index 153e528..75179f2 100644 --- a/internal/db/migrations/postgres/00004_delivery_rule_variants.sql +++ b/internal/db/migrations/postgres/00004_delivery_rule_variants.sql @@ -4,30 +4,28 @@ ALTER TABLE delivery_rules ADD COLUMN cookie_id TEXT NOT NULL DEFAULT ''; ALTER TABLE delivery_rules ADD COLUMN item_id TEXT NOT NULL DEFAULT ''; CREATE TABLE delivery_rule_variants ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - rule_id INTEGER NOT NULL, + id BIGSERIAL PRIMARY KEY, + rule_id BIGINT NOT NULL REFERENCES delivery_rules(id) ON DELETE CASCADE, spec_name TEXT NOT NULL DEFAULT '', spec_value TEXT NOT NULL DEFAULT '', - card_id INTEGER NOT NULL, + card_id BIGINT NOT NULL REFERENCES cards(id) ON DELETE RESTRICT, delivery_count INTEGER NOT NULL DEFAULT 1, enabled BOOLEAN NOT NULL DEFAULT TRUE, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (rule_id) REFERENCES delivery_rules(id) ON DELETE CASCADE, - FOREIGN KEY (card_id) REFERENCES cards(id) ON DELETE RESTRICT, UNIQUE(rule_id, spec_name, spec_value) ); -CREATE INDEX idx_delivery_rules_cookie_item ON delivery_rules(cookie_id, item_id); -CREATE INDEX idx_delivery_rule_variants_rule ON delivery_rule_variants(rule_id); -CREATE INDEX idx_delivery_rule_variants_card ON delivery_rule_variants(card_id); +CREATE INDEX IF NOT EXISTS idx_delivery_rules_cookie_item ON delivery_rules(cookie_id, item_id); +CREATE INDEX IF NOT EXISTS idx_delivery_rule_variants_rule ON delivery_rule_variants(rule_id); +CREATE INDEX IF NOT EXISTS idx_delivery_rule_variants_card ON delivery_rule_variants(card_id); -- 旧规则迁移为一条默认或指定规格映射。cards 上的规格字段仅保留兼容读取。 INSERT INTO delivery_rule_variants (rule_id, spec_name, spec_value, card_id, delivery_count, enabled) SELECT dr.id, - CASE WHEN COALESCE(c.is_multi_spec, 0) = 1 THEN COALESCE(c.spec_name, '') ELSE '' END, - CASE WHEN COALESCE(c.is_multi_spec, 0) = 1 THEN COALESCE(c.spec_value, '') ELSE '' END, + CASE WHEN COALESCE(c.is_multi_spec, FALSE) = TRUE THEN COALESCE(c.spec_name, '') ELSE '' END, + CASE WHEN COALESCE(c.is_multi_spec, FALSE) = TRUE THEN COALESCE(c.spec_value, '') ELSE '' END, dr.card_id, CASE WHEN dr.delivery_count > 0 THEN dr.delivery_count ELSE 1 END, dr.enabled diff --git a/internal/db/migrations/postgres/00005_ws_messages.sql b/internal/db/migrations/postgres/00005_ws_messages.sql index 11bb822..7e14272 100644 --- a/internal/db/migrations/postgres/00005_ws_messages.sql +++ b/internal/db/migrations/postgres/00005_ws_messages.sql @@ -1,22 +1,5 @@ -- +goose Up --- +goose StatementBegin -CREATE TABLE IF NOT EXISTS ws_messages ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - cookie_id TEXT NOT NULL, - direction TEXT NOT NULL DEFAULT 'in', - raw_text TEXT NOT NULL DEFAULT '', - parsed_json TEXT NOT NULL DEFAULT '', - message_kind TEXT NOT NULL DEFAULT '', - parse_status TEXT NOT NULL DEFAULT 'raw', - error TEXT NOT NULL DEFAULT '', - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (cookie_id) REFERENCES cookies(id) ON DELETE CASCADE -); -CREATE INDEX IF NOT EXISTS idx_ws_messages_cookie_id_created_at ON ws_messages(cookie_id, created_at DESC); --- +goose StatementEnd +-- ws_messages 表在 Postgres 版 00001 已创建,此迁移为空操作,保留版本号与 SQLite 对齐。 -- +goose Down --- +goose StatementBegin -DROP INDEX IF EXISTS idx_ws_messages_cookie_id_created_at; -DROP TABLE IF EXISTS ws_messages; --- +goose StatementEnd +-- 空操作:ws_messages 在 00001 创建,回滚由 00001 Down 处理。 diff --git a/internal/db/migrations/postgres/00006_item_publish_batches.sql b/internal/db/migrations/postgres/00006_item_publish_batches.sql index 35784dc..b54a6fc 100644 --- a/internal/db/migrations/postgres/00006_item_publish_batches.sql +++ b/internal/db/migrations/postgres/00006_item_publish_batches.sql @@ -2,7 +2,7 @@ -- +goose StatementBegin CREATE TABLE IF NOT EXISTS item_publish_batches ( id TEXT PRIMARY KEY, - user_id INTEGER NOT NULL, + user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, default_cookie_id TEXT NOT NULL DEFAULT '', filename TEXT NOT NULL DEFAULT '', upload_dir TEXT NOT NULL DEFAULT '', @@ -11,13 +11,12 @@ CREATE TABLE IF NOT EXISTS item_publish_batches ( success_count INTEGER NOT NULL DEFAULT 0, failed_count INTEGER NOT NULL DEFAULT 0, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS item_publish_batch_rows ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - batch_id TEXT NOT NULL, + id BIGSERIAL PRIMARY KEY, + batch_id TEXT NOT NULL REFERENCES item_publish_batches(id) ON DELETE CASCADE, row_no INTEGER NOT NULL, cookie_id TEXT NOT NULL DEFAULT '', title TEXT NOT NULL DEFAULT '', @@ -28,17 +27,13 @@ CREATE TABLE IF NOT EXISTS item_publish_batch_rows ( postage_mode TEXT NOT NULL DEFAULT 'free', postage TEXT NOT NULL DEFAULT '', images_json TEXT NOT NULL DEFAULT '[]', - auto_create_delivery_rule INTEGER NOT NULL DEFAULT 0, - card_group_id INTEGER NOT NULL DEFAULT 0, - delivery_count INTEGER NOT NULL DEFAULT 1, status TEXT NOT NULL DEFAULT 'pending', item_id TEXT NOT NULL DEFAULT '', item_url TEXT NOT NULL DEFAULT '', error_message TEXT NOT NULL DEFAULT '', raw_json TEXT NOT NULL DEFAULT '{}', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - FOREIGN KEY (batch_id) REFERENCES item_publish_batches(id) ON DELETE CASCADE + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE INDEX IF NOT EXISTS idx_item_publish_batches_user ON item_publish_batches(user_id, created_at); diff --git a/internal/db/migrations/postgres/00007_automation_center.sql b/internal/db/migrations/postgres/00007_automation_center.sql index 2d00311..9d05d55 100644 --- a/internal/db/migrations/postgres/00007_automation_center.sql +++ b/internal/db/migrations/postgres/00007_automation_center.sql @@ -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); diff --git a/internal/db/migrations/postgres/00008_drop_dead_schema.sql b/internal/db/migrations/postgres/00008_drop_dead_schema.sql index 70a4fcb..7b28a9e 100644 --- a/internal/db/migrations/postgres/00008_drop_dead_schema.sql +++ b/internal/db/migrations/postgres/00008_drop_dead_schema.sql @@ -1,51 +1,45 @@ -- +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(PostgreSQL 版)。 +-- 注意:Postgres 00006 未创建 auto_create_delivery_rule/card_group_id/delivery_count +-- (SQLite 历史遗留列,00008 在 SQLite 里删除,PG 版直接没建,故此处不删)。 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; +-- email_verifications / ai_item_cache / risk_control_logs 在 PG 00001 未创建, +-- IF EXISTS 对不存在的表是安全的。 +DROP TABLE IF EXISTS email_verifications; 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; -- +goose StatementEnd -- +goose Down -- +goose StatementBegin -- 仅重建结构,历史数据不可恢复。 CREATE TABLE IF NOT EXISTS delivery_rules ( - id INTEGER PRIMARY KEY AUTOINCREMENT, + id BIGSERIAL PRIMARY KEY, keyword TEXT NOT NULL, - card_id INTEGER NOT NULL, + card_id BIGINT NOT NULL, delivery_count INTEGER DEFAULT 1, enabled BOOLEAN DEFAULT TRUE, description TEXT, delivery_times INTEGER DEFAULT 0, - user_id INTEGER NOT NULL DEFAULT 1, + user_id BIGINT NOT NULL DEFAULT 1, cookie_id TEXT NOT NULL DEFAULT '', item_id TEXT NOT NULL DEFAULT '', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS delivery_rule_variants ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - rule_id INTEGER NOT NULL, + id BIGSERIAL PRIMARY KEY, + rule_id BIGINT NOT NULL, spec_name TEXT NOT NULL DEFAULT '', spec_value TEXT NOT NULL DEFAULT '', - card_id INTEGER NOT NULL, + card_id BIGINT NOT NULL, delivery_count INTEGER NOT NULL DEFAULT 1, enabled BOOLEAN NOT NULL DEFAULT TRUE, 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; -- +goose StatementEnd