chore: 同步本地 v9 整改与运营能力
This commit is contained in:
@@ -34,6 +34,9 @@ func Init(cfg *config.Config) error {
|
||||
&model.Threshold{}, &model.Alarm{}, &model.Camera{}, &model.VideoClip{},
|
||||
&model.AuditLog{}, &model.Telemetry{},
|
||||
&model.Permission{}, &model.RolePermission{},
|
||||
&model.Organization{}, &model.OrganizationMember{},
|
||||
&model.DeviceMaintenanceRecord{}, &model.ProductionLossRecord{},
|
||||
&model.CaseStudy{}, &model.LaboratoryResult{},
|
||||
&model.Disease{}, &model.KnowledgeArticle{},
|
||||
&model.InspectionRecord{},
|
||||
&model.OutboxEvent{},
|
||||
@@ -60,6 +63,7 @@ func Init(cfg *config.Config) error {
|
||||
}
|
||||
slog.Warn("开发环境 AutoMigrate 已启用,SQL 迁移仍是生产 schema 事实来源")
|
||||
}
|
||||
seedOrganizations(db)
|
||||
// 初始化权限种子数据
|
||||
seedPermissions(db)
|
||||
// 初始化知识库种子数据
|
||||
@@ -68,6 +72,28 @@ func Init(cfg *config.Config) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// seedOrganizations 保证默认组织存在,并给所有现有用户补齐成员关系(幂等)。
|
||||
func seedOrganizations(db *gorm.DB) {
|
||||
var org model.Organization
|
||||
if err := db.Where("code = ?", "default").First(&org).Error; err != nil {
|
||||
org = model.Organization{Name: "默认组织", Code: "default", Status: "active"}
|
||||
desc := "系统升级时创建的默认组织,用于兼容历史数据"
|
||||
org.Description = &desc
|
||||
if err := db.Create(&org).Error; err != nil {
|
||||
slog.Warn("创建默认组织失败", "err", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
db.Exec(`
|
||||
INSERT INTO organization_members (organization_id, user_id, role)
|
||||
SELECT ?, u.id, 'member' FROM users u
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM organization_members om
|
||||
WHERE om.organization_id = ? AND om.user_id = u.id
|
||||
)
|
||||
`, org.ID, org.ID)
|
||||
}
|
||||
|
||||
// seedKnowledge 初始化知识库种子数据(幂等:按名称/类型+标题判重)
|
||||
func seedKnowledge(db *gorm.DB) {
|
||||
for _, d := range model.SeedDiseases {
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
// CurrentSchemaVersion 是当前后端代码期望的迁移版本。
|
||||
const CurrentSchemaVersion = "8"
|
||||
const CurrentSchemaVersion = "9"
|
||||
|
||||
// RunMigrations 使用嵌入式 SQL 迁移文件将数据库升级到最新版本。
|
||||
func RunMigrations(db *gorm.DB) error {
|
||||
|
||||
@@ -124,4 +124,8 @@ func TestEmbeddedMigrationsIncludeBaseline(t *testing.T) {
|
||||
if err != nil || next != 8 {
|
||||
t.Fatalf("expected governance migration version 8, got %d (err %v)", next, err)
|
||||
}
|
||||
next, err = driver.Next(next)
|
||||
if err != nil || next != 9 {
|
||||
t.Fatalf("expected object authority migration version 9, got %d (err %v)", next, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user