1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- package models
- import (
- "fmt"
- "log/slog"
- )
- import (
- "gorm.io/driver/postgres"
- "gorm.io/gorm"
- )
- import (
- "protocol-server/common"
- "protocol-server/logger"
- )
- var (
- DB *gorm.DB
- err error
- )
- func panicHelper(err error) {
- if err != nil {
- panic(err)
- }
- }
- func migrate() {
- panicHelper(DB.AutoMigrate(&PatientType{}))
- var count int64
- DB.Model(&BodyPart{}).Count(&count)
- if count == 0 {
- panicHelper(initTable("p_patient_type_202505291630.sql"))
- }
- panicHelper(DB.AutoMigrate(&BodyPart{}))
- DB.Model(&BodyPart{}).Count(&count)
- if count == 0 {
- panicHelper(initTable("p_body_part_202505291606.sql"))
- }
- panicHelper(DB.AutoMigrate(&Procedure{}))
- }
- func SetupGorm(m bool) {
- slog.Info("setup gorm")
- dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Asia/Shanghai",
- common.PostgresConfig.Ip,
- common.PostgresConfig.Username,
- common.PostgresConfig.Password,
- common.PostgresConfig.Name,
- common.PostgresConfig.Port,
- )
- if common.LoggerConfig.LogLevel == "debug" {
- DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{
- Logger: logger.NewGormLogger(logger.WithGroup("gorm")),
- })
- } else {
- DB, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
- }
- if err != nil {
- panic(err)
- }
- if m {
- migrate()
- }
- slog.Info("setup gorm ok")
- }
|