-- 玩家+物品+VIP状态 联合唯一
ALTER TABLE player_item
ADD CONSTRAINT uk_player_item_player_item_vip
UNIQUE (player_id, item_id, is_vip);


-- 玩家+挖矿 联合唯一
CREATE UNIQUE INDEX uk_talk_player ON player_talk (talk_id, player_id);


-- 玩家+任务 联合唯一
CREATE UNIQUE INDEX uk_player_task ON player_task (player_id, task_id);
-- 玩家+称号 联合唯一
CREATE UNIQUE INDEX uk_player_title ON player_title (player_id, is_vip) WHERE deleted_at IS NULL;

-- 玩家+精灵 联合唯一
CREATE UNIQUE INDEX uk_player_pet ON player_pet (player_id, is_vip, catch_time) WHERE deleted_at IS NULL;

-- 玩家+CDK 联合唯一
CREATE UNIQUE INDEX uk_player_cdk_log
ON player_cdk_log (player_id, code_id, is_vip)
WHERE deleted_at IS NULL;
 

-- 玩家孵蛋 联合唯一
 CREATE UNIQUE INDEX uk_player_egg 
ON player_egg (player_id, is_vip) 
WHERE deleted_at IS NULL;

---PVP索引
CREATE UNIQUE INDEX uk_player_pvp
ON player_pvp (player_id, season)
WHERE deleted_at IS NULL;

--签到
CREATE UNIQUE INDEX uk_player_sign_in_log
ON player_sign_in_log (player_id, sign_in_id, is_vip)
WHERE deleted_at IS NULL;
--房间索引
CREATE UNIQUE INDEX uk_player_room_house
ON player_room_house (player_id, is_vip)
WHERE deleted_at IS NULL;

-- 集市权限角色 联合唯一
-- 先清理历史重复授权，保留每组 userId + roleId 中 id 最小的一条
DELETE FROM base_sys_user_role a
USING base_sys_user_role b
WHERE a."userId" = b."userId"
  AND a."roleId" = b."roleId"
  AND a.id > b.id;

CREATE UNIQUE INDEX IF NOT EXISTS uk_base_sys_user_role_user_role
ON base_sys_user_role ("userId", "roleId");
