admin/sql/fix_data_perm_role_permission_bad_guids.sql
2026-07-03 11:58:24 +08:00

33 lines
1.5 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 修复 role_permission.CreateUserId = '0' 导致登录失败
-- 症状:POST /rbac/auth/login → Could not parse CHAR(36) value as Guid: 0
-- 原因:seed_data_perm_rbac_menu.sql 曾写入 CreateUserId = 0
--
-- ⚠️ 在 RBAC 库(fake_simple_admin 或 API 连接的 RBAC 库)执行
SET @ADMIN_USER_ID = (
SELECT `Id` FROM `user`
WHERE `Account` = 'admin' AND (`IsDeleted` = 0 OR `IsDeleted` IS NULL)
LIMIT 1
);
SET @ADMIN_USER_ID = IFNULL(
@ADMIN_USER_ID,
(SELECT `Id` FROM `user` WHERE `IsDeleted` = 0 OR `IsDeleted` IS NULL ORDER BY `CreateTime` LIMIT 1)
);
SELECT @ADMIN_USER_ID AS admin_user_id_used;
SELECT 'before fix' AS stage, rp.`Id`, rp.`RoleId`, rp.`PermissionCode`, rp.`CreateUserId`
FROM `role_permission` rp
WHERE CAST(rp.`CreateUserId` AS CHAR) = '0'
OR (rp.`CreateUserId` IS NOT NULL AND rp.`CreateUserId` NOT REGEXP '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');
UPDATE `role_permission`
SET `CreateUserId` = @ADMIN_USER_ID
WHERE CAST(`CreateUserId` AS CHAR) = '0'
OR (`CreateUserId` IS NOT NULL AND `CreateUserId` NOT REGEXP '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');
SELECT 'after fix (should be empty)' AS stage, rp.`Id`, rp.`RoleId`, rp.`PermissionCode`, rp.`CreateUserId`
FROM `role_permission` rp
WHERE CAST(rp.`CreateUserId` AS CHAR) = '0'
OR (rp.`CreateUserId` IS NOT NULL AND rp.`CreateUserId` NOT REGEXP '^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$');