kra-new/internal/data/task/migrations.go

41 lines
1.0 KiB
Go

package task
import (
"context"
"strings"
"kra/pkg/database/migration"
platformmodule "kra/pkg/module"
"gorm.io/gorm"
)
func Migrations() []migration.Step {
return []migration.Step{{
ID: "202608220001_task_schema",
Migrate: func(db *gorm.DB) error {
return migration.CreateMissingTables(db, &taskPO{}, &taskLogPO{})
},
}}
}
// SeedDefaults inserts module-contributed timed tasks without making the
// system data package own task table shapes.
func SeedDefaults(ctx context.Context, db *gorm.DB, defaults []platformmodule.TimedTask) error {
if db == nil {
return nil
}
return db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
for _, item := range defaults {
if strings.TrimSpace(item.Name) == "" {
continue
}
row := taskPO{Name: item.Name, Description: item.Description, Spec: item.Spec, WithSeconds: item.WithSeconds, ExecutorType: "method", MethodName: item.MethodName, Enabled: item.Enabled}
if err := tx.Where("name = ?", row.Name).FirstOrCreate(&row).Error; err != nil {
return err
}
}
return nil
})
}