-- =====================================================
-- 删除 cf_orgs 表中所有带数字的重复唯一约束
-- 保留 cf_orgs_username_key（不带数字的）
-- =====================================================

DO $$
DECLARE 
    constraint_record RECORD;
BEGIN
    FOR constraint_record IN 
        SELECT conname
        FROM pg_constraint 
        WHERE conrelid = 'cf_orgs'::regclass 
          AND conname LIKE 'cf_orgs_username_key%'
          AND conname != 'cf_orgs_username_key'
          AND conname ~ '[0-9]'  -- 包含数字的
        ORDER BY conname
    LOOP
        EXECUTE format('ALTER TABLE cf_orgs DROP CONSTRAINT IF EXISTS %I', constraint_record.conname);
        RAISE NOTICE '已删除约束: %', constraint_record.conname;
    END LOOP;
END $$;

-- =====================================================
-- 删除 cf_user 表中所有带数字的重复唯一约束
-- 保留 cf_user_username_key（不带数字的）
-- =====================================================

DO $$
DECLARE 
    constraint_record RECORD;
BEGIN
    FOR constraint_record IN 
        SELECT conname
        FROM pg_constraint 
        WHERE conrelid = 'cf_user'::regclass 
          AND conname LIKE 'cf_user_username_key%'
          AND conname != 'cf_user_username_key'
          AND conname ~ '[0-9]'  -- 包含数字的
        ORDER BY conname
    LOOP
        EXECUTE format('ALTER TABLE cf_user DROP CONSTRAINT IF EXISTS %I', constraint_record.conname);
        RAISE NOTICE '已删除约束: %', constraint_record.conname;
    END LOOP;
END $$;

-- =====================================================
-- 验证删除结果
-- =====================================================

-- 查看 cf_orgs 剩余的 username 约束
SELECT 'cf_orgs 表剩余的 username 约束:' AS info;
SELECT conname
FROM pg_constraint 
WHERE conrelid = 'cf_orgs'::regclass 
  AND conname LIKE 'cf_orgs_username_key%'
ORDER BY conname;

-- 查看 cf_user 剩余的 username 约束
SELECT 'cf_user 表剩余的 username 约束:' AS info;
SELECT conname
FROM pg_constraint 
WHERE conrelid = 'cf_user'::regclass 
  AND conname LIKE 'cf_user_username_key%'
ORDER BY conname;
-- =====================================================
-- 删除 kv_app_domain 表中所有带数字的重复唯一约束
-- 保留 kv_app_domain_domain_key（不带数字的）
-- =====================================================
DO $$
DECLARE 
    constraint_record RECORD;
BEGIN
    FOR constraint_record IN 
        SELECT conname
        FROM pg_constraint 
        WHERE conrelid = 'kv_app_domain'::regclass 
          AND conname LIKE 'kv_app_domain_domain_key%'
          AND conname != 'kv_app_domain_domain_key'
          AND conname ~ '[0-9]'  -- 包含数字的
        ORDER BY conname
    LOOP
        EXECUTE format('ALTER TABLE kv_app_domain DROP CONSTRAINT IF EXISTS %I', constraint_record.conname);
        RAISE NOTICE '已删除约束: %', constraint_record.conname;
    END LOOP;
END $$;

-- =====================================================
-- 删除 prompts 表中所有带数字的重复唯一约束
-- 保留 prompts_key_key（不带数字的）
-- =====================================================

DO $$
DECLARE 
    constraint_record RECORD;
BEGIN
    FOR constraint_record IN 
        SELECT conname
        FROM pg_constraint 
        WHERE conrelid = 'prompts'::regclass 
          AND conname LIKE 'prompts_key_key%'
          AND conname != 'prompts_key_key'
          AND conname ~ '[0-9]'  -- 包含数字的
        ORDER BY conname
    LOOP
        EXECUTE format('ALTER TABLE prompts DROP CONSTRAINT IF EXISTS %I', constraint_record.conname);
        RAISE NOTICE '已删除约束: %', constraint_record.conname;
    END LOOP;
END $$;

-- =====================================================
-- 删除 apps_trades 表中所有带数字的重复唯一约束
-- 保留 apps_trades_out_trade_no_key（不带数字的）
-- =====================================================

DO $$
DECLARE 
    constraint_record RECORD;
BEGIN
    FOR constraint_record IN 
        SELECT conname
        FROM pg_constraint 
        WHERE conrelid = 'apps_trades'::regclass 
          AND conname LIKE 'apps_trades_out_trade_no_key%'
          AND conname != 'apps_trades_out_trade_no_key'
          AND conname ~ '[0-9]'  -- 包含数字的
        ORDER BY conname
    LOOP
        EXECUTE format('ALTER TABLE apps_trades DROP CONSTRAINT IF EXISTS %I', constraint_record.conname);
        RAISE NOTICE '已删除约束: %', constraint_record.conname;
    END LOOP;
END $$;

-- =====================================================
-- 删除 ai_agent 表中所有带数字的重复唯一约束
-- 保留 ai_agent_key_key（不带数字的）
-- =====================================================

DO $$
DECLARE 
    constraint_record RECORD;
BEGIN
    FOR constraint_record IN 
        SELECT conname
        FROM pg_constraint 
        WHERE conrelid = 'ai_agent'::regclass 
          AND conname LIKE 'ai_agent_key_key%'
          AND conname != 'ai_agent_key_key'
          AND conname ~ '[0-9]'  -- 包含数字的
        ORDER BY conname
    LOOP
        EXECUTE format('ALTER TABLE ai_agent DROP CONSTRAINT IF EXISTS %I', constraint_record.conname);
        RAISE NOTICE '已删除约束: %', constraint_record.conname;
    END LOOP;
END $$;