fix: synchronize database schema with gorm
This commit is contained in:
@@ -52,7 +52,7 @@ make docker-up
|
|||||||
|
|
||||||
## 宠物医生问诊问药示范场景
|
## 宠物医生问诊问药示范场景
|
||||||
|
|
||||||
数据库迁移和管理员初始化完成后执行:
|
数据库结构由 GORM 在应用启动时自动同步,管理员初始化完成后执行:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
make seed-pet-doctor
|
make seed-pet-doctor
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/config"
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/config"
|
||||||
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -37,6 +38,37 @@ func Open(cfg config.DatabaseConfig, log *zap.Logger) (*gorm.DB, error) {
|
|||||||
return db, nil
|
return db, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AutoMigrate synchronizes the application schema from the GORM models.
|
||||||
|
// GORM only adds missing tables, columns and indexes; it does not drop
|
||||||
|
// existing columns, which keeps production data safe during application
|
||||||
|
// upgrades.
|
||||||
|
func AutoMigrate(db *gorm.DB) error {
|
||||||
|
return db.AutoMigrate(
|
||||||
|
&model.Tenant{},
|
||||||
|
&model.User{},
|
||||||
|
&model.Role{},
|
||||||
|
&model.TenantMember{},
|
||||||
|
&model.Scenario{},
|
||||||
|
&model.ScenarioField{},
|
||||||
|
&model.ScenarioRule{},
|
||||||
|
&model.SOP{},
|
||||||
|
&model.SOPVersion{},
|
||||||
|
&model.SOPNode{},
|
||||||
|
&model.SOPEdge{},
|
||||||
|
&model.KnowledgeCard{},
|
||||||
|
&model.KnowledgeCardVersion{},
|
||||||
|
&model.KnowledgeItem{},
|
||||||
|
&model.KnowledgeRelation{},
|
||||||
|
&model.SOPRun{},
|
||||||
|
&model.SOPRunEvent{},
|
||||||
|
&model.PublicRunSession{},
|
||||||
|
&model.SOPFeedback{},
|
||||||
|
&model.AuditLog{},
|
||||||
|
&model.MultiTableOutbox{},
|
||||||
|
&model.RefreshToken{},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
type zapGORMLogger struct {
|
type zapGORMLogger struct {
|
||||||
log *zap.Logger
|
log *zap.Logger
|
||||||
level gormlogger.LogLevel
|
level gormlogger.LogLevel
|
||||||
|
|||||||
8
main.go
8
main.go
@@ -16,14 +16,10 @@ import (
|
|||||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/database"
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/database"
|
||||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/httpserver"
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/httpserver"
|
||||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/logger"
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/logger"
|
||||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/migration"
|
|
||||||
"git.iwork-ai.com/xdc/iqudo-top1/internal/multitable"
|
"git.iwork-ai.com/xdc/iqudo-top1/internal/multitable"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed migrations/*.sql
|
|
||||||
var migrationFiles embed.FS
|
|
||||||
|
|
||||||
//go:embed web/dist/*
|
//go:embed web/dist/*
|
||||||
var frontendFiles embed.FS
|
var frontendFiles embed.FS
|
||||||
|
|
||||||
@@ -54,8 +50,8 @@ func main() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("connect database", zap.Error(err))
|
log.Fatal("connect database", zap.Error(err))
|
||||||
}
|
}
|
||||||
if err := migration.Run(db, migrationFiles); err != nil {
|
if err := database.AutoMigrate(db); err != nil {
|
||||||
log.Fatal("run database migrations", zap.Error(err))
|
log.Fatal("synchronize database schema", zap.Error(err))
|
||||||
}
|
}
|
||||||
if err := auth.Seed(db, cfg.Seed); err != nil {
|
if err := auth.Seed(db, cfg.Seed); err != nil {
|
||||||
log.Fatal("seed administrator", zap.Error(err))
|
log.Fatal("seed administrator", zap.Error(err))
|
||||||
|
|||||||
Reference in New Issue
Block a user