SET @has_subject_id := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'class_subject_teachers'
    AND COLUMN_NAME = 'subject_id'
);--> statement-breakpoint
SET @sql := IF(@has_subject_id = 0, 'ALTER TABLE `class_subject_teachers` ADD COLUMN `subject_id` VARCHAR(128) NULL;', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;--> statement-breakpoint

INSERT INTO `subjects` (`id`, `name`, `code`)
SELECT UUID(), '语文', 'CHINESE' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '语文' OR `code` = 'CHINESE')
UNION ALL
SELECT UUID(), '数学', 'MATH' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '数学' OR `code` = 'MATH')
UNION ALL
SELECT UUID(), '英语', 'ENG' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '英语' OR `code` = 'ENG')
UNION ALL
SELECT UUID(), '美术', 'ART' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '美术' OR `code` = 'ART')
UNION ALL
SELECT UUID(), '体育', 'PE' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '体育' OR `code` = 'PE')
UNION ALL
SELECT UUID(), '科学', 'SCI' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '科学' OR `code` = 'SCI')
UNION ALL
SELECT UUID(), '社会', 'SOC' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '社会' OR `code` = 'SOC')
UNION ALL
SELECT UUID(), '音乐', 'MUSIC' FROM DUAL
WHERE NOT EXISTS (SELECT 1 FROM `subjects` WHERE `name` = '音乐' OR `code` = 'MUSIC');--> statement-breakpoint

SET @has_subject_col := (
  SELECT COUNT(*) FROM information_schema.COLUMNS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'class_subject_teachers'
    AND COLUMN_NAME = 'subject'
);--> statement-breakpoint
SET @sql := IF(@has_subject_col = 1, '
  UPDATE `class_subject_teachers` cst
  JOIN `subjects` s ON (
    s.`name` = cst.`subject`
    OR s.`code` = CASE cst.`subject`
      WHEN ''语文'' THEN ''CHINESE''
      WHEN ''数学'' THEN ''MATH''
      WHEN ''英语'' THEN ''ENG''
      WHEN ''美术'' THEN ''ART''
      WHEN ''体育'' THEN ''PE''
      WHEN ''科学'' THEN ''SCI''
      WHEN ''社会'' THEN ''SOC''
      WHEN ''音乐'' THEN ''MUSIC''
      ELSE NULL
    END
  )
  SET cst.`subject_id` = s.`id`
  WHERE cst.`subject_id` IS NULL;
', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;--> statement-breakpoint

SET @subject_id_nulls := (
  SELECT COUNT(*) FROM `class_subject_teachers`
  WHERE `subject_id` IS NULL
);--> statement-breakpoint
SET @sql := IF(@subject_id_nulls = 0, 'ALTER TABLE `class_subject_teachers` MODIFY COLUMN `subject_id` VARCHAR(128) NOT NULL;', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;--> statement-breakpoint

SET @sql := IF(@subject_id_nulls = 0, 'ALTER TABLE `class_subject_teachers` DROP PRIMARY KEY;', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;--> statement-breakpoint
SET @sql := IF(@subject_id_nulls = 0, 'ALTER TABLE `class_subject_teachers` ADD PRIMARY KEY (`class_id`, `subject_id`);', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;--> statement-breakpoint

SET @sql := IF(@subject_id_nulls = 0 AND @has_subject_col = 1, 'ALTER TABLE `class_subject_teachers` DROP COLUMN `subject`;', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;--> statement-breakpoint

SET @has_subject_id_idx := (
  SELECT COUNT(*) FROM information_schema.STATISTICS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'class_subject_teachers'
    AND INDEX_NAME = 'class_subject_teachers_subject_id_idx'
);--> statement-breakpoint
SET @sql := IF(@subject_id_nulls = 0 AND @has_subject_id_idx = 0, 'CREATE INDEX `class_subject_teachers_subject_id_idx` ON `class_subject_teachers` (`subject_id`);', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;--> statement-breakpoint

SET @has_subject_fk := (
  SELECT COUNT(*) FROM information_schema.TABLE_CONSTRAINTS
  WHERE TABLE_SCHEMA = DATABASE()
    AND TABLE_NAME = 'class_subject_teachers'
    AND CONSTRAINT_NAME = 'cst_s_fk'
    AND CONSTRAINT_TYPE = 'FOREIGN KEY'
);--> statement-breakpoint
SET @sql := IF(@subject_id_nulls = 0 AND @has_subject_fk = 0, 'ALTER TABLE `class_subject_teachers` ADD CONSTRAINT `cst_s_fk` FOREIGN KEY (`subject_id`) REFERENCES `subjects`(`id`) ON DELETE CASCADE;', 'SELECT 1');--> statement-breakpoint
PREPARE stmt FROM @sql;--> statement-breakpoint
EXECUTE stmt;--> statement-breakpoint
DEALLOCATE PREPARE stmt;
