From 4100062ba888e4e5fe110f002e4a54de09b33115 Mon Sep 17 00:00:00 2001 From: john Date: Mon, 24 Aug 2026 17:29:03 +0800 Subject: [PATCH] fix(db): repair legacy h5_intent_drafts stub schema on migrate Drop tables created with intent_kind instead of layer/draft_type so ITL draft inserts succeed after one-off migrate scripts. Co-authored-by: Cursor --- db.mjs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/db.mjs b/db.mjs index 71b23b2..52806d1 100644 --- a/db.mjs +++ b/db.mjs @@ -1138,6 +1138,35 @@ export async function migrateSchema(pool) { CONSTRAINT fk_h5_user_feedback_user FOREIGN KEY (user_id) REFERENCES h5_users(id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci `); + + // One-off migrate stub used intent_kind; ITL expects layer/draft_type/action_level/title. + if (await tableExists(pool, 'h5_intent_drafts') && !(await columnExists(pool, 'h5_intent_drafts', 'layer'))) { + await pool.query('DROP TABLE h5_intent_drafts'); + } + await pool.query(` + CREATE TABLE IF NOT EXISTS h5_intent_drafts ( + id CHAR(36) PRIMARY KEY, + user_id CHAR(36) NOT NULL, + layer ENUM('L0', 'L1', 'L2', 'L3', 'ambiguous') NOT NULL, + draft_type VARCHAR(64) NOT NULL, + action_level TINYINT UNSIGNED NOT NULL DEFAULT 1, + title VARCHAR(255) NOT NULL, + payload_json JSON NOT NULL, + card_text TEXT NOT NULL, + status ENUM('draft', 'confirmed', 'committed', 'cancelled', 'expired') NOT NULL DEFAULT 'draft', + source_channel VARCHAR(32) NOT NULL DEFAULT 'wechat', + source_message_id VARCHAR(128) NULL, + source_text TEXT NULL, + committed_ref_json JSON NULL, + event_log_json JSON NULL, + expires_at BIGINT NULL, + created_at BIGINT NOT NULL, + updated_at BIGINT NOT NULL, + KEY idx_intent_draft_user_status (user_id, status, created_at), + KEY idx_intent_draft_expires (status, expires_at), + CONSTRAINT fk_intent_draft_user FOREIGN KEY (user_id) REFERENCES h5_users(id) ON DELETE CASCADE + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci + `); } export async function initSchema(pool) {