Files
YschcmsService/sql/upgrade_r1_school_code.sql
T

36 lines
2.0 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.
-- ============================================================
-- R1 迁移脚本:学校主站编码改 5 位数字码(1 + 学校ID左补零至4位)
-- 例:sch_id=13 → code/path = 10013;幂等(只处理编码≠目标值的行)
-- 前置:W4 嵌套 path 机制已落地(cms_site.path 列已回填,uk_site_path 已建)
-- 旧链接提示:执行后 /portal/sch-13/ 不再解析(301 兼容仅覆盖子站按 code
-- 回查,主站不在其列);已对外发链需在 Caddy 层加旧前缀 301 或接受断链。
-- ============================================================
-- 0) 前置校验:先人工看一眼存量主站
-- ① 同一 sch_id 挂多个顶层站 → 改码后触发 uk_site_path 冲突,需先合并/停用;
-- ② sch_id 为空的顶层站(如演示站)会被本脚本跳过、保持原编码。
SELECT id, code, sch_id, path, status FROM cms_site WHERE parent_site_id = 0 OR parent_site_id IS NULL;
-- 1) 顶层主站:code 与 path 一起改为目标 5 位码(sch_id 全局唯一 ⇒ 天然满足
-- uk_site_path / uk_site_parent_code;sch_id 为空的顶层站跳过)
UPDATE cms_site
SET code = CONCAT('1', LPAD(sch_id, 4, '0')),
path = CONCAT('1', LPAD(sch_id, 4, '0'))
WHERE (parent_site_id = 0 OR parent_site_id IS NULL)
AND sch_id IS NOT NULL
AND code <> CONCAT('1', LPAD(sch_id, 4, '0'));
-- 2) 子站 path 级联重算(code 不动,仅换父前缀;幂等)
UPDATE cms_site s JOIN cms_site p ON s.parent_site_id = p.id
SET s.path = CONCAT(p.path, '/', s.code)
WHERE s.parent_site_id > 0
AND s.path <> CONCAT(p.path, '/', s.code);
-- 3) 校验(两条都应返回 0 行)
SELECT id, code, path FROM cms_site WHERE path = '' OR path IS NULL;
SELECT id, code, path FROM cms_site WHERE parent_site_id = 0 OR parent_site_id IS NULL;
-- 4) 收尾:缓存为进程内 Caffeine(站点快照 TTL 30min、旧 key 自然过期);
-- 想立即生效可在站点管理保存一次任一站点(触发 SiteContentChangedEvent 清缓存),
-- 或重启 cms 容器。