feat: 自动迁移表结构
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
package initialize
|
||||
|
||||
import (
|
||||
"go.uber.org/zap"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"sundynix-go/global"
|
||||
"sundynix-go/model/system"
|
||||
)
|
||||
|
||||
// Gorm 根据全局配置中的数据库类型返回对应的 *gorm.DB 实例。
|
||||
@@ -22,3 +25,16 @@ func Gorm() *gorm.DB {
|
||||
return GormMysql() // 默认返回 MySQL 数据库的 *gorm.DB 实例
|
||||
}
|
||||
}
|
||||
|
||||
// MigrateTable 创建数据库表结构。
|
||||
func MigrateTable() {
|
||||
db := global.DB
|
||||
err := db.AutoMigrate(
|
||||
system.User{},
|
||||
)
|
||||
if err != nil {
|
||||
global.Logger.Error("Migrate table failed,err:", zap.Error(err))
|
||||
os.Exit(0)
|
||||
}
|
||||
global.Logger.Info("Migrate table success")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user