This commit is contained in:
Eric 1549169735@qq.com
2026-09-13 20:08:13 +08:00
parent 7cc06976ab
commit da3f16e6db
65 changed files with 3855 additions and 2153 deletions

View File

@@ -55,6 +55,9 @@ func AutoMigrate(db *gorm.DB) error {
if err := prepareSingleVersionSchema(db); err != nil {
return err
}
if err := dropLegacyKnowledgeSchema(db); err != nil {
return err
}
return db.AutoMigrate(
&model.Tenant{},
&model.User{},
@@ -66,8 +69,16 @@ func AutoMigrate(db *gorm.DB) error {
&model.SOP{},
&model.SOPNode{},
&model.SOPEdge{},
&model.KnowledgeItem{},
&model.KnowledgeRelation{},
&model.ScriptPackage{},
&model.PackageDimension{},
&model.DimensionValue{},
&model.PackageStage{},
&model.StageScript{},
&model.ScriptOption{},
&model.DimensionLinkage{},
&model.PackageAdapter{},
&model.RunDimension{},
&model.ScriptFeedback{},
&model.SOPRun{},
&model.SOPRunEvent{},
&model.PublicRunSession{},
@@ -78,6 +89,24 @@ func AutoMigrate(db *gorm.DB) error {
)
}
// dropLegacyKnowledgeSchema removes the knowledge-graph tables and the run
// snapshot column that the script-package rewrite no longer uses.
func dropLegacyKnowledgeSchema(db *gorm.DB) error {
for _, table := range []string{"knowledge_relations", "knowledge_items"} {
if db.Migrator().HasTable(table) {
if err := db.Exec("DROP TABLE " + quoteIdentifier(table)).Error; err != nil {
return fmt.Errorf("drop legacy table %s: %w", table, err)
}
}
}
if db.Migrator().HasTable("sop_runs") && db.Migrator().HasColumn("sop_runs", "knowledge_snapshot") {
if err := db.Exec("ALTER TABLE sop_runs DROP COLUMN knowledge_snapshot").Error; err != nil {
return fmt.Errorf("drop sop_runs.knowledge_snapshot: %w", err)
}
}
return nil
}
// dropUnusedLegacyTables removes schema objects that are no longer part of
// the scenario knowledge graph. Drop the version table before its parent so
// this remains safe for databases that still have a historical constraint.