You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
2.4 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package initialize
import (
"context"
adapter "github.com/casbin/gorm-adapter/v3"
"gorm.io/gorm"
"miniapp/model/example"
sysModel "miniapp/model/system"
"miniapp/service/system"
)
const initOrderEnsureTables = system.InitOrderExternal - 1
type ensureTables struct{}
// auto run
func init() {
system.RegisterInit(initOrderEnsureTables, &ensureTables{})
}
func (ensureTables) InitializerName() string {
return "ensure_tables_created"
}
func (e *ensureTables) InitializeData(ctx context.Context) (next context.Context, err error) {
return ctx, nil
}
func (e *ensureTables) DataInserted(ctx context.Context) bool {
return true
}
func (e *ensureTables) MigrateTable(ctx context.Context) (context.Context, error) {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return ctx, system.ErrMissingDBContext
}
tables := []interface{}{
sysModel.SysApi{},
sysModel.SysUser{},
sysModel.SysBaseMenu{},
sysModel.SysAuthority{},
sysModel.JwtBlacklist{},
sysModel.SysDictionary{},
sysModel.SysAutoCodeHistory{},
sysModel.SysOperationRecord{},
sysModel.SysDictionaryDetail{},
sysModel.SysBaseMenuParameter{},
sysModel.SysBaseMenuBtn{},
sysModel.SysAuthorityBtn{},
sysModel.SysAutoCode{},
sysModel.SysChatGptOption{},
adapter.CasbinRule{},
example.ExaFile{},
example.ExaCustomer{},
example.ExaFileChunk{},
example.ExaFileUploadAndDownload{},
}
for _, t := range tables {
_ = db.AutoMigrate(&t)
// 视图 authority_menu 会被当成表来创建引发冲突错误更新版本的gorm似乎不会
// 由于 AutoMigrate() 基本无需考虑错误,因此显式忽略
}
return ctx, nil
}
func (e *ensureTables) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
tables := []interface{}{
sysModel.SysApi{},
sysModel.SysUser{},
sysModel.SysBaseMenu{},
sysModel.SysAuthority{},
sysModel.JwtBlacklist{},
sysModel.SysDictionary{},
sysModel.SysAutoCodeHistory{},
sysModel.SysOperationRecord{},
sysModel.SysDictionaryDetail{},
sysModel.SysBaseMenuParameter{},
sysModel.SysBaseMenuBtn{},
sysModel.SysAuthorityBtn{},
sysModel.SysAutoCode{},
sysModel.SysChatGptOption{},
adapter.CasbinRule{},
example.ExaFile{},
example.ExaCustomer{},
example.ExaFileChunk{},
example.ExaFileUploadAndDownload{},
}
yes := true
for _, t := range tables {
yes = yes && db.Migrator().HasTable(t)
}
return yes
}