This commit is contained in:
2026-09-03 17:55:42 +08:00
parent 7c38e84e30
commit c80a3d4a2d
35 changed files with 6795 additions and 597 deletions
+17
View File
@@ -45,6 +45,19 @@ func Init() error {
DB = db
if !cfg.DBAutoMigrate {
log.Println("[database] mysql connected (auto migrate skipped)")
return nil
}
log.Println("[database] running auto migration and seeding...")
// 增量保护:确保花名册新表及 row_count 字段迁移
_ = db.AutoMigrate(&model.ClassRoster{})
if !db.Migrator().HasColumn(&model.Class{}, "row_count") {
_ = db.Migrator().AddColumn(&model.Class{}, "row_count")
}
// 数据迁移:旧版 bcrypt 密码(60字符) 超过新版 md5(32字符) 列宽,
// 在 AutoMigrate 缩列前先清空旧密码,后续 ensureAdmin 会重新设置。
// 注意:salt 列此时可能还不存在,只更新 password_hash。
@@ -55,6 +68,9 @@ func Init() error {
return fmt.Errorf("auto migrate: %w", err)
}
// 历史数据回填:school_types 多学段字段由原单一 school_type 初始化
db.Exec("UPDATE yz_pw_schools SET school_types = school_type WHERE (school_types IS NULL OR school_types = '') AND school_type <> ''")
// 创建平台管理员
if err := ensureAdmin(db); err != nil {
return fmt.Errorf("ensure admin: %w", err)
@@ -79,6 +95,7 @@ func autoMigrate(db *gorm.DB) error {
&model.College{},
&model.Class{},
&model.ClassMember{},
&model.ClassRoster{},
&model.ClassPhoto{},
&model.Invitation{},
&model.AuditLog{},