Merge pull request #1130 from trheyi/main

Refactor job and category saving logic to improve ID handling
This commit is contained in:
Max 2025-08-31 15:58:29 +08:00 committed by GitHub
commit 95f957d477
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -110,8 +110,11 @@ func SaveJob(job *Job) error {
// Create new job // Create new job
if job.JobID == "" { if job.JobID == "" {
job.JobID = uuid.New().String() job.JobID = uuid.New().String()
data["job_id"] = job.JobID
} }
// Remove ID field from data to let database auto-increment
delete(data, "id")
data["job_id"] = job.JobID
data["created_at"] = now data["created_at"] = now
data["updated_at"] = now data["updated_at"] = now
@ -129,7 +132,7 @@ func SaveJob(job *Job) error {
param := model.QueryParam{ param := model.QueryParam{
Wheres: []model.QueryWhere{ Wheres: []model.QueryWhere{
{Column: "id", Value: job.ID}, {Column: "job_id", Value: job.JobID},
}, },
Limit: 1, Limit: 1,
} }
@ -289,8 +292,11 @@ func SaveCategory(category *Category) error {
// Create new category // Create new category
if category.CategoryID == "" { if category.CategoryID == "" {
category.CategoryID = uuid.New().String() category.CategoryID = uuid.New().String()
data["category_id"] = category.CategoryID
} }
// Remove ID field from data to let database auto-increment
delete(data, "id")
data["category_id"] = category.CategoryID
data["created_at"] = now data["created_at"] = now
data["updated_at"] = now data["updated_at"] = now
@ -308,7 +314,7 @@ func SaveCategory(category *Category) error {
param := model.QueryParam{ param := model.QueryParam{
Wheres: []model.QueryWhere{ Wheres: []model.QueryWhere{
{Column: "id", Value: category.ID}, {Column: "category_id", Value: category.CategoryID},
}, },
Limit: 1, Limit: 1,
} }
@ -398,7 +404,7 @@ func ListLogs(jobID string, param model.QueryParam, page int, pagesize int) (map
return mod.Paginate(param, page, pagesize) return mod.Paginate(param, page, pagesize)
} }
// SaveLog save log // SaveLog save log (always creates new log entry)
func SaveLog(log *Log) error { func SaveLog(log *Log) error {
mod := model.Select("__yao.job.log") mod := model.Select("__yao.job.log")
if mod == nil { if mod == nil {
@ -408,39 +414,22 @@ func SaveLog(log *Log) error {
data := structToMap(log) data := structToMap(log)
now := time.Now() now := time.Now()
if log.ID == 0 { // Always create new log entry
// Create new log // Remove ID field from data to let database auto-increment
data["created_at"] = now delete(data, "id")
data["updated_at"] = now data["created_at"] = now
if log.Timestamp.IsZero() { data["updated_at"] = now
log.Timestamp = now if log.Timestamp.IsZero() {
data["timestamp"] = now log.Timestamp = now
} data["timestamp"] = now
id, err := mod.Create(data)
if err != nil {
return fmt.Errorf("failed to create log: %w", err)
}
log.ID = uint(id)
} else {
// Update existing log (rare case)
data["updated_at"] = now
delete(data, "id")
delete(data, "created_at")
param := model.QueryParam{
Wheres: []model.QueryWhere{
{Column: "id", Value: log.ID},
},
Limit: 1,
}
_, err := mod.UpdateWhere(param, data)
if err != nil {
return fmt.Errorf("failed to update log: %w", err)
}
} }
id, err := mod.Create(data)
if err != nil {
return fmt.Errorf("failed to create log: %w", err)
}
log.ID = uint(id)
return nil return nil
} }
@ -604,8 +593,11 @@ func SaveExecution(execution *Execution) error {
// Create new execution // Create new execution
if execution.ExecutionID == "" { if execution.ExecutionID == "" {
execution.ExecutionID = uuid.New().String() execution.ExecutionID = uuid.New().String()
data["execution_id"] = execution.ExecutionID
} }
// Remove ID field from data to let database auto-increment
delete(data, "id")
data["execution_id"] = execution.ExecutionID
data["created_at"] = now data["created_at"] = now
data["updated_at"] = now data["updated_at"] = now
@ -623,7 +615,7 @@ func SaveExecution(execution *Execution) error {
param := model.QueryParam{ param := model.QueryParam{
Wheres: []model.QueryWhere{ Wheres: []model.QueryWhere{
{Column: "id", Value: execution.ID}, {Column: "execution_id", Value: execution.ExecutionID},
}, },
Limit: 1, Limit: 1,
} }