CreateTasks creates tasks for a plan.
(ctx context.Context, projectID string, planUID int64, tasks []*TaskMessage)
| 497 | |
| 498 | // CreateTasks creates tasks for a plan. |
| 499 | func (s *Store) CreateTasks(ctx context.Context, projectID string, planUID int64, tasks []*TaskMessage) ([]*TaskMessage, error) { |
| 500 | tx, err := s.GetDB().BeginTx(ctx, nil) |
| 501 | if err != nil { |
| 502 | return nil, errors.Wrapf(err, "failed to begin tx") |
| 503 | } |
| 504 | defer tx.Rollback() |
| 505 | |
| 506 | // Check existing tasks to avoid duplicates |
| 507 | existingTasks, err := s.listTasksImpl(ctx, tx, &TaskFind{ |
| 508 | ProjectID: projectID, |
| 509 | PlanID: &planUID, |
| 510 | }) |
| 511 | if err != nil { |
| 512 | return nil, errors.Wrapf(err, "failed to list existing tasks") |
| 513 | } |
| 514 | |
| 515 | type taskKey struct { |
| 516 | instance string |
| 517 | database string |
| 518 | sheet string |
| 519 | } |
| 520 | |
| 521 | createdTasks := map[taskKey]struct{}{} |
| 522 | for _, task := range existingTasks { |
| 523 | k := taskKey{ |
| 524 | instance: task.InstanceID, |
| 525 | sheet: task.Payload.GetSheetSha256(), |
| 526 | } |
| 527 | if task.DatabaseName != nil { |
| 528 | k.database = *task.DatabaseName |
| 529 | } |
| 530 | createdTasks[k] = struct{}{} |
| 531 | } |
| 532 | |
| 533 | var taskCreateList []*TaskMessage |
| 534 | |
| 535 | for _, taskCreate := range tasks { |
| 536 | k := taskKey{ |
| 537 | instance: taskCreate.InstanceID, |
| 538 | sheet: taskCreate.Payload.GetSheetSha256(), |
| 539 | } |
| 540 | if taskCreate.DatabaseName != nil { |
| 541 | k.database = *taskCreate.DatabaseName |
| 542 | } |
| 543 | |
| 544 | if _, ok := createdTasks[k]; ok { |
| 545 | continue |
| 546 | } |
| 547 | taskCreate.ProjectID = projectID |
| 548 | taskCreate.PlanID = planUID |
| 549 | taskCreateList = append(taskCreateList, taskCreate) |
| 550 | } |
| 551 | |
| 552 | if len(taskCreateList) > 0 { |
| 553 | tasks, err := s.createTasksTx(ctx, tx, taskCreateList...) |
| 554 | if err != nil { |
| 555 | return nil, errors.Wrap(err, "failed to create tasks") |
| 556 | } |
no test coverage detected