feat: 添加 seed.go 初始化文件 — 默认用户+审批流程+系统设置
- seedDefaultUsers: 4个默认用户(admin/dba/auditor/developer, 密码admin123) - seedDefaultWorkflow: 默认审批流程(提交→DBA审核→执行) - seedDefaultSettings: 数据脱敏默认配置 - 所有操作幂等,已有数据不覆盖 - main.go调用seedAll()统一初始化
This commit is contained in:
1 parent
86eea25031
commit
ad12f30355
228 files changed
+118
-58764
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"sql-platform/internal/model"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// seedAll initializes the database with default data (idempotent).
|
||||
// Safe to run on every startup — skips existing records.
|
||||
func seedAll(db *gorm.DB) {
|
||||
seedDefaultUsers(db)
|
||||
seedDefaultWorkflow(db)
|
||||
seedDefaultSettings(db)
|
||||
log.Println("[seed] Default data initialized")
|
||||
}
|
||||
|
||||
// seedDefaultUsers creates default users if they don't exist.
|
||||
func seedDefaultUsers(db *gorm.DB) {
|
||||
type seedUser struct {
|
||||
username string
|
||||
displayName string
|
||||
email string
|
||||
role string
|
||||
password string
|
||||
active bool
|
||||
}
|
||||
users := []seedUser{
|
||||
{"admin", "超级管理员", "admin@opsx.local", "admin", "admin123", true},
|
||||
{"dba", "DBA管理员", "dba@opsx.local", "dba", "admin123", true},
|
||||
{"auditor", "审计员", "auditor@opsx.local", "auditor", "admin123", true},
|
||||
{"developer", "开发者", "developer@opsx.local", "developer", "admin123", true},
|
||||
}
|
||||
|
||||
for _, u := range users {
|
||||
var existing model.User
|
||||
if db.Where("username = ?", u.username).First(&existing).Error != nil {
|
||||
hash, _ := bcrypt.GenerateFromPassword([]byte(u.password), bcrypt.DefaultCost)
|
||||
email := u.email
|
||||
active := u.active
|
||||
user := model.User{
|
||||
Username: u.username,
|
||||
DisplayName: u.displayName,
|
||||
Email: &email,
|
||||
HashedPassword: string(hash),
|
||||
Role: u.role,
|
||||
IsActive: &active,
|
||||
}
|
||||
if err := db.Create(&user).Error; err != nil {
|
||||
log.Printf("[seed] Failed to create user %s: %v", u.username, err)
|
||||
} else {
|
||||
log.Printf("[seed] Created user: %s (%s)", u.username, u.role)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// seedDefaultWorkflow creates a default approval workflow if none exists.
|
||||
func seedDefaultWorkflow(db *gorm.DB) {
|
||||
var count int64
|
||||
db.Model(&model.WorkflowDefinition{}).Count(&count)
|
||||
if count > 0 {
|
||||
return // Already has workflows
|
||||
}
|
||||
|
||||
// Create default workflow
|
||||
t := true
|
||||
wf := model.WorkflowDefinition{
|
||||
Name: "默认审批流程",
|
||||
Description: strPtr("系统默认审批流程:开发者提交 → DBA审核 → 执行"),
|
||||
IsDefault: &t,
|
||||
IsActive: &t,
|
||||
}
|
||||
if err := db.Create(&wf).Error; err != nil {
|
||||
log.Printf("[seed] Failed to create default workflow: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
// Create steps
|
||||
steps := []model.WorkflowStep{
|
||||
{
|
||||
WorkflowID: wf.ID,
|
||||
StepNumber: 1,
|
||||
Name: "提交审核",
|
||||
StepType: "submit",
|
||||
IsRequired: &t,
|
||||
TimeoutHours: intPtr(0),
|
||||
},
|
||||
{
|
||||
WorkflowID: wf.ID,
|
||||
StepNumber: 2,
|
||||
Name: "DBA审核",
|
||||
StepType: "audit",
|
||||
ApproverRole: strPtr("dba"),
|
||||
IsRequired: &t,
|
||||
TimeoutHours: intPtr(24),
|
||||
},
|
||||
{
|
||||
WorkflowID: wf.ID,
|
||||
StepNumber: 3,
|
||||
Name: "执行变更",
|
||||
StepType: "execute",
|
||||
IsRequired: &t,
|
||||
TimeoutHours: intPtr(0),
|
||||
},
|
||||
}
|
||||
for _, s := range steps {
|
||||
db.Create(&s)
|
||||
}
|
||||
log.Printf("[seed] Created default workflow: %s (id=%d)", wf.Name, wf.ID)
|
||||
}
|
||||
|
||||
func intPtr(v int) *int { return &v }
|
||||
Reference in new issue
Block a user