admin/sql/fix_xhs_menu_bad_guids.sql

57 lines
2.4 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.

-- 修复 menu 表 CreateUserId / UpdateUserId 非法 GUID(如 "0")
-- 症状:登录或 GET /rbac/menu/get-menu-tree → Could not parse CHAR(36) value as Guid: 0
--
-- ⚠️ 在 RBAC 库执行(不是 OMS 业务库 fake_simple_admin)
-- ⚠️ WHERE 中禁止写 CreateUserId = 0(整数),会触发 1292 GUID 转数字错误
-- 1) 管理员 GUID:优先 admin 账号,否则取第一个用户
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;
-- 2) 诊断:修复前查看脏数据
SELECT `Id`, `Name`, `Route`, `CreateUserId`, `UpdateUserId`
FROM `menu`
WHERE (`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}$')
OR (`UpdateUserId` IS NOT NULL AND `UpdateUserId` 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}$');
-- 3) 小红书种子菜单(按 Id 精确修复)
UPDATE `menu`
SET `CreateUserId` = @ADMIN_USER_ID,
`UpdateUserId` = @ADMIN_USER_ID
WHERE `Id` IN (
'a1b2c3d4-e5f6-7890-abcd-ef1234567801',
'a1b2c3d4-e5f6-7890-abcd-ef1234567802',
'a1b2c3d4-e5f6-7890-abcd-ef1234567803',
'a1b2c3d4-e5f6-7890-abcd-ef1234567804',
'a1b2c3d4-e5f6-7890-abcd-ef1234567805',
'a1b2c3d4-e5f6-7890-abcd-ef1234567806',
'a1b2c3d4-e5f6-7890-abcd-ef1234567807',
'a1b2c3d4-e5f6-7890-abcd-ef1234567808'
);
-- 4) 兜底:全表非法 GUID(字符串比较,勿用 = 0)
UPDATE `menu`
SET `CreateUserId` = @ADMIN_USER_ID
WHERE `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}$';
UPDATE `menu`
SET `UpdateUserId` = @ADMIN_USER_ID
WHERE `UpdateUserId` IS NOT NULL
AND `UpdateUserId` 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}$';
-- 5) 修复后应无结果
SELECT `Id`, `Name`, `CreateUserId`, `UpdateUserId`
FROM `menu`
WHERE (`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}$')
OR (`UpdateUserId` IS NOT NULL AND `UpdateUserId` 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}$');