Files
dify-plus/admin/server/initialize/ensure_tables.go
T

50 lines
1.0 KiB
Go

package initialize
import (
"context"
"github.com/flipped-aurora/gin-vue-admin/server/service/system"
"gorm.io/gorm"
)
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
}
RegisterTables(db)
return ctx, nil
}
func (e *ensureTables) TableCreated(ctx context.Context) bool {
db, ok := ctx.Value("db").(*gorm.DB)
if !ok {
return false
}
yes := true
for _, t := range tables {
yes = yes && db.Migrator().HasTable(t)
}
return yes
}