fix: synchronize database schema with gorm

This commit is contained in:
Eric 1549169735@qq.com
2026-08-19 22:48:23 +08:00
parent 1320e30cfb
commit 7526b72c63
3 changed files with 35 additions and 7 deletions

View File

@@ -52,7 +52,7 @@ make docker-up
## 宠物医生问诊问药示范场景
数据库迁移和管理员初始化完成后执行:
数据库结构由 GORM 在应用启动时自动同步,管理员初始化完成后执行:
```bash
make seed-pet-doctor

View File

@@ -7,6 +7,7 @@ import (
"time"
"git.iwork-ai.com/xdc/iqudo-top1/internal/config"
"git.iwork-ai.com/xdc/iqudo-top1/internal/model"
"go.uber.org/zap"
"gorm.io/driver/mysql"
"gorm.io/gorm"
@@ -37,6 +38,37 @@ func Open(cfg config.DatabaseConfig, log *zap.Logger) (*gorm.DB, error) {
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 {
log *zap.Logger
level gormlogger.LogLevel

View File

@@ -16,14 +16,10 @@ import (
"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/logger"
"git.iwork-ai.com/xdc/iqudo-top1/internal/migration"
"git.iwork-ai.com/xdc/iqudo-top1/internal/multitable"
"go.uber.org/zap"
)
//go:embed migrations/*.sql
var migrationFiles embed.FS
//go:embed web/dist/*
var frontendFiles embed.FS
@@ -54,8 +50,8 @@ func main() {
if err != nil {
log.Fatal("connect database", zap.Error(err))
}
if err := migration.Run(db, migrationFiles); err != nil {
log.Fatal("run database migrations", zap.Error(err))
if err := database.AutoMigrate(db); err != nil {
log.Fatal("synchronize database schema", zap.Error(err))
}
if err := auth.Seed(db, cfg.Seed); err != nil {
log.Fatal("seed administrator", zap.Error(err))