62 lines
3.3 KiB
Go
62 lines
3.3 KiB
Go
package business
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"kra/pkg/database/migration"
|
|
)
|
|
|
|
func Migrations() []migration.Step {
|
|
return []migration.Step{{ID: "202611040017_business_groups_goals_effectiveness", Migrate: migrateBusinessManagement}, {ID: "202611040018_business_effectiveness_query_index", Migrate: migrateEffectivenessQueryIndex}, {ID: "202611040015_business_approved_game_index", Migrate: migrateApprovedGameIndexes}, {ID: "202611040014_business_report_build_indexes", Migrate: migrateReportBuildQueryIndexes}, {ID: "202611040013_business_query_indexes", Migrate: migrateBusinessQueryIndexes}, {ID: "202611040012_business_server_report_index", Migrate: migrateServerReportIndex}, {ID: "202611040011_business_registration_index", Migrate: migrateRegistrationIndex}, {ID: "202611040010_business_alert_trigger_locks", Migrate: func(db *gorm.DB) error { return migration.CreateMissingTables(db, &alertTriggerLockPO{}) }}, {ID: "202611040009_business_menu_icons", Migrate: migrateBusinessMenuIcons}, {ID: "202611040008_business_report_build_locks", Migrate: func(db *gorm.DB) error { return migration.CreateMissingTables(db, &reportBuildLockPO{}) }}, {ID: "202611030007_business_alert_attachments", Migrate: migrateAlertAttachments}, {ID: "202611030006_business_alert_schema", Migrate: migrateAlertSchema}, {ID: "202611030005_business_password_reset", Migrate: func(db *gorm.DB) error {
|
|
return migration.CreateMissingTables(db, &resetCodePO{}, &resetTokenPO{})
|
|
}}, {ID: "202611030004_business_binding_locks", Migrate: func(db *gorm.DB) error {
|
|
return migration.CreateMissingTables(db, &bindingLockPO{})
|
|
}}, {ID: "202611030003_business_bindings", Migrate: func(db *gorm.DB) error {
|
|
return migration.CreateMissingTables(db, &bindingPO{})
|
|
}}, {ID: "202611030001_business_preference", Migrate: func(db *gorm.DB) error {
|
|
// 旧表沿用;已迁移数据库不重建或重置用户偏好。
|
|
if err := migration.CreateMissingTables(db, &preferencePO{}); err != nil {
|
|
return err
|
|
}
|
|
if !db.Migrator().HasTable("sys_authorities") || !db.Migrator().HasTable("casbin_rule") {
|
|
return nil
|
|
}
|
|
return db.Transaction(func(tx *gorm.DB) error {
|
|
// 两个接口仅操作当前身份,旧系统对所有已登录用户开放。
|
|
for _, method := range []string{"GET", "PUT"} {
|
|
if err := tx.Exec(`INSERT INTO casbin_rule (ptype,v0,v1,v2,v3,v4,v5)
|
|
SELECT 'p', a.authority_id, ?, ?, '', '', '' FROM sys_authorities a
|
|
WHERE a.deleted_at IS NULL AND NOT EXISTS (
|
|
SELECT 1 FROM casbin_rule p WHERE p.ptype='p' AND p.v0=a.authority_id AND p.v1=? AND p.v2=?)`,
|
|
"/sw/user/preference", method, "/sw/user/preference", method).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
}}}
|
|
}
|
|
|
|
func migrateAlertSchema(db *gorm.DB) error {
|
|
if err := migration.CreateMissingTables(db, &alertRulePO{}, &alertRecordPO{}, &alertLogPO{}); err != nil {
|
|
return err
|
|
}
|
|
if db.Dialector.Name() != "mysql" {
|
|
return nil
|
|
}
|
|
columns, err := db.Migrator().ColumnTypes(&alertRulePO{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, column := range columns {
|
|
if column.Name() == "mom_threshold" {
|
|
precision, scale, ok := column.DecimalSize()
|
|
if ok && precision >= 7 && scale >= 2 {
|
|
return nil
|
|
}
|
|
// 原页面允许 9999%,旧 decimal(5,2) 装不下;扩大精度且不改变已有值。
|
|
return db.Migrator().AlterColumn(&alertRulePO{}, "MomThreshold")
|
|
}
|
|
}
|
|
return nil
|
|
}
|