-- 管理员端“教务管理-课程管理”增量数据库脚本(MySQL 5.7+/8.0)。 -- -- 执行顺序:先执行class_management.sql补齐班级培训日期等字段,再执行本脚本。 -- 设计说明: -- 1. 项目已存在class_subject表,本脚本调整课时精度、补充排课组、审计列和常用索引。 -- 2. 旧数据允许审计时间为空;新增/编辑接口会自动维护create_time和update_time。 -- 3. 上午、下午仍是独立class_subject记录,schedule_group_id只负责聚合页面中的同一排课行。 -- 4. 项目历史表主要由服务层维护关系完整性,本脚本不额外引入外键,避免阻断旧数据迁移。 delimiter $$ drop procedure if exists migrate_course_management$$ create procedure migrate_course_management() begin -- 页面允许0.5课时,原int列会截断小数,因此统一调整为一位小数。 if exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'subject_hour' ) then alter table class_subject modify column subject_hour decimal(5,1) null comment '课时数,支持0.5课时粒度'; end if; -- -- 新版页面允许一行展示上午、下午两条独立课程。旧记录逐条分组,不按日期自动合并, -- 可以避免把历史上同日但业务含义不同的课程错误组合到同一编辑单元。 -- if not exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'schedule_group_id' ) then alter table class_subject add column schedule_group_id varchar(32) null comment '课程管理页面排课组ID,组内课程仍保留独立主键' after class_id; end if; -- 编辑弹窗仅携带全局唯一的排课组ID,组ID前导索引避免首次定位班级时扫描全表。 if not exists ( select 1 from information_schema.statistics where table_schema = database() and table_name = 'class_subject' and index_name = 'idx_subject_schedule_group' ) then alter table class_subject add index idx_subject_schedule_group (schedule_group_id, id); end if; update class_subject set schedule_group_id = concat('legacy-', id) where schedule_group_id is null or trim(schedule_group_id) = ''; -- 回填完成后设置非空,确保后续新增数据不会出现无法聚合的课程。 if exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'schedule_group_id' and is_nullable = 'YES' ) then alter table class_subject modify column schedule_group_id varchar(32) not null comment '课程管理页面排课组ID,组内课程仍保留独立主键'; end if; if not exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'create_time' ) then alter table class_subject add column create_time datetime null comment '创建时间' after type; end if; -- 排课组详情按班级和组ID读取,日期、ID作为稳定排序后缀。 if not exists ( select 1 from information_schema.statistics where table_schema = database() and table_name = 'class_subject' and index_name = 'idx_subject_class_schedule' ) then alter table class_subject add index idx_subject_class_schedule (class_id, schedule_group_id, date, id); end if; if not exists ( select 1 from information_schema.columns where table_schema = database() and table_name = 'class_subject' and column_name = 'update_time' ) then alter table class_subject add column update_time datetime null comment '更新时间' after create_time; end if; -- 班级课程表按class_id和date过滤、排序,该索引同时服务课程导出和日期范围查询。 if not exists ( select 1 from information_schema.statistics where table_schema = database() and table_name = 'class_subject' and index_name = 'idx_subject_class_date' ) then alter table class_subject add index idx_subject_class_date (class_id, date, id); end if; -- 教师冲突校验和教师日历按teacher_id、date缩小候选集合。 if not exists ( select 1 from information_schema.statistics where table_schema = database() and table_name = 'class_subject' and index_name = 'idx_subject_teacher_date' ) then alter table class_subject add index idx_subject_teacher_date (teacher_id, date, id); end if; end$$ call migrate_course_management()$$ drop procedure if exists migrate_course_management$$ delimiter ; -- 旧记录没有可靠的原始创建时间,使用当前时间仅回填update_time,create_time保持NULL表示未知。 update class_subject set update_time = coalesce(update_time, now()) where update_time is null; -- 部署后核验:以下两个结果都应为0;查询只读,不改变业务数据。 select count(*) as missing_schedule_group_count from class_subject where schedule_group_id is null or trim(schedule_group_id) = ''; select count(*) as cross_class_schedule_group_count from ( select schedule_group_id from class_subject group by schedule_group_id having count(distinct class_id) > 1 ) invalid_groups;