feat: 自动迁移表结构

This commit is contained in:
Blizzard
2025-04-26 11:58:41 +08:00
parent 248ed10c62
commit 5e41df7dc2
7 changed files with 77 additions and 9 deletions
+16
View File
@@ -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")
}