fix(migration): 使数据库迁移脚本幂等化,支持重复执行

将 008_add_co_managed_wallet_fields.up.sql 改为幂等脚本:
- 使用 DO $$ ... IF NOT EXISTS 检查列是否存在再添加
- 使用 CREATE INDEX IF NOT EXISTS 创建索引
- 使用 DROP CONSTRAINT IF EXISTS 删除约束

这确保迁移脚本可以安全地多次执行,不会因列/索引已存在而失败。

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2025-12-28 05:26:38 -08:00
parent 75a9ffadef
commit 6c4a40c42d
1 changed files with 17 additions and 7 deletions

View File

@ -1,14 +1,24 @@
-- Migration: 008_add_co_managed_wallet_fields
-- Description: Add wallet_name and invite_code fields for co-managed wallet sessions
-- and extend session_type to support 'co_managed_keygen'
-- This migration is idempotent - safe to run multiple times
-- Add new columns for co-managed wallet sessions
ALTER TABLE mpc_sessions
ADD COLUMN wallet_name VARCHAR(255),
ADD COLUMN invite_code VARCHAR(50);
-- Add new columns for co-managed wallet sessions (idempotent)
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name = 'mpc_sessions' AND column_name = 'wallet_name') THEN
ALTER TABLE mpc_sessions ADD COLUMN wallet_name VARCHAR(255);
END IF;
-- Create index for invite_code lookups
CREATE INDEX idx_mpc_sessions_invite_code ON mpc_sessions(invite_code) WHERE invite_code IS NOT NULL;
IF NOT EXISTS (SELECT 1 FROM information_schema.columns
WHERE table_name = 'mpc_sessions' AND column_name = 'invite_code') THEN
ALTER TABLE mpc_sessions ADD COLUMN invite_code VARCHAR(50);
END IF;
END $$;
-- Create index for invite_code lookups (idempotent)
CREATE INDEX IF NOT EXISTS idx_mpc_sessions_invite_code ON mpc_sessions(invite_code) WHERE invite_code IS NOT NULL;
-- Drop the existing session_type constraint
ALTER TABLE mpc_sessions DROP CONSTRAINT IF EXISTS chk_session_type;
@ -17,6 +27,6 @@ ALTER TABLE mpc_sessions DROP CONSTRAINT IF EXISTS chk_session_type;
ALTER TABLE mpc_sessions
ADD CONSTRAINT chk_session_type CHECK (session_type IN ('keygen', 'sign', 'co_managed_keygen'));
-- Add comment for the new columns
-- Add comment for the new columns (safe to run multiple times)
COMMENT ON COLUMN mpc_sessions.wallet_name IS 'Wallet name for co-managed wallet sessions';
COMMENT ON COLUMN mpc_sessions.invite_code IS 'Invite code for co-managed wallet sessions - used for participants to join';