From 0169a16adac94bec9db8b1a81d28f29155ade6db Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Mon, 25 May 2026 11:07:06 -0700 Subject: [PATCH] fix: make refund migration compatible with legacy schemas Co-authored-by: multica-agent --- .../02150_refund_pending_withdrawals.down.sql | 4 ++-- .../02150_refund_pending_withdrawals.up.sql | 24 +++++++++++++++++-- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/initialize/migrate/database/02150_refund_pending_withdrawals.down.sql b/initialize/migrate/database/02150_refund_pending_withdrawals.down.sql index f4ce8ac..2c8eecc 100644 --- a/initialize/migrate/database/02150_refund_pending_withdrawals.down.sql +++ b/initialize/migrate/database/02150_refund_pending_withdrawals.down.sql @@ -14,6 +14,6 @@ SET u.commission = u.commission - p.pending_total WHERE p.pending_total > 0; -- Remove the migration log entries written by the up migration. -DELETE FROM system_log -WHERE type = 3 +DELETE FROM system_logs +WHERE type = 33 AND content LIKE '%migration: refund pending withdrawal commission (HIF-22)%'; diff --git a/initialize/migrate/database/02150_refund_pending_withdrawals.up.sql b/initialize/migrate/database/02150_refund_pending_withdrawals.up.sql index 861d28b..79ec196 100644 --- a/initialize/migrate/database/02150_refund_pending_withdrawals.up.sql +++ b/initialize/migrate/database/02150_refund_pending_withdrawals.up.sql @@ -11,6 +11,26 @@ -- Running this script multiple times is safe only if no new pending -- withdrawals are created between runs; deploy new code immediately after. +-- Compatibility: some historical databases missed migration 02122, so the +-- withdrawals table may not exist yet. Create it idempotently before the +-- refund logic so this migration can self-heal older installations. +CREATE TABLE IF NOT EXISTS `withdrawals` ( + `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT 'Primary Key', + `user_id` BIGINT NOT NULL COMMENT 'User ID', + `amount` BIGINT NOT NULL COMMENT 'Withdrawal Amount', + `content` TEXT COMMENT 'Withdrawal Content', + `status` TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Withdrawal Status', + `reason` VARCHAR(500) NOT NULL DEFAULT '' COMMENT 'Rejection Reason', + `created_at` DATETIME NOT NULL COMMENT 'Creation Time', + `updated_at` DATETIME NOT NULL COMMENT 'Update Time', + PRIMARY KEY (`id`), + KEY `idx_user_id` (`user_id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +INSERT IGNORE INTO `system` (`category`, `key`, `value`, `type`, `desc`, `created_at`, `updated_at`) +VALUES + ('invite', 'WithdrawalMethod', '', 'string', 'withdrawal method', '2025-04-22 14:25:16.637', '2025-04-22 14:25:16.637'); + -- Step 1: refund commission for all users with pending withdrawals. UPDATE `user` u JOIN ( @@ -23,9 +43,9 @@ SET u.commission = u.commission + p.pending_total WHERE p.pending_total > 0; -- Step 2: write a migration log entry for each refunded user. -INSERT INTO system_log (type, date, object_id, content, created_at) +INSERT INTO system_logs (type, date, object_id, content, created_at) SELECT - 3 AS type, + 33 AS type, DATE(NOW()) AS date, p.user_id AS object_id, JSON_OBJECT(