Files
sundynix-plant-be/initialize/gorm.go
T

69 lines
1.9 KiB
Go

package initialize
import (
"os"
"sundynix-go/global"
"sundynix-go/model/plant"
"sundynix-go/model/system"
"go.uber.org/zap"
"gorm.io/gorm"
)
// Gorm 根据全局配置中的数据库类型返回对应的 *gorm.DB 实例。
// 该函数通过检查 global.Config.System.DbType 的值来决定使用哪种数据库连接。
//
// 返回值:
// - *gorm.DB: 返回对应数据库类型的 *gorm.DB 实例
func Gorm() *gorm.DB {
switch global.Config.System.DbType {
case "mysql":
return GormMysql() // 返回 MySQL 数据库的 *gorm.DB 实例
case "pgsql":
return GromPgsql() // 返回 PostgreSQL 数据库的 *gorm.DB 实例
case "sqlite":
return GormSqlite() // 返回 SQLite 数据库的 *gorm.DB 实例
default:
return GormMysql() // 默认返回 MySQL 数据库的 *gorm.DB 实例
}
}
// MigrateTable 创建数据库表结构。
func MigrateTable() {
db := global.DB
err := db.AutoMigrate(
system.User{},
system.Client{},
system.Role{},
system.Menu{},
system.SysOperationRecord{},
system.Oss{},
plant.MyPlant{}, //我的植物
plant.CarePlan{}, //植物养护计划
plant.CareTask{}, //植物养护任务
plant.CareRecord{}, //植物养护记录
plant.GrowthRecord{}, //植物成长记录
plant.MediaCheckResult{}, //媒体安全检测结果
plant.Topic{}, //帖子话题
plant.Post{}, //帖子
plant.PostLike{}, //帖子点赞
plant.PostComment{}, //帖子评论
plant.Class{}, //百科分类
plant.Wiki{}, //百科植物
plant.ClassifyRecord{}, //植物识别记录
plant.LevelConfig{}, //等级配置
plant.BadgeConfig{}, //徽章配置
plant.UserProfile{}, //用户资料
plant.UserBadge{}, //用户徽章
plant.UserStar{}, //用户收藏
)
if err != nil {
global.Logger.Error("Migrate table failed,err:", zap.Error(err))
os.Exit(0)
}
global.Logger.Info("Migrate table success")
}