From f2ab3129952004a66122862b7dd990cb8980bd71 Mon Sep 17 00:00:00 2001 From: Jesse Peterson Date: Tue, 13 Aug 2024 12:04:58 -0700 Subject: [PATCH] fix not found status for retrieving workflow status in MySQL backend (#63) --- engine/storage/mysql/storage.go | 4 +++- engine/storage/storage.go | 1 + engine/storage/test/event.go | 8 ++++++++ engine/storage/test/test.go | 6 ++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/engine/storage/mysql/storage.go b/engine/storage/mysql/storage.go index 5e6f00b..22d9df1 100644 --- a/engine/storage/mysql/storage.go +++ b/engine/storage/mysql/storage.go @@ -246,7 +246,9 @@ func (s *MySQLStorage) CancelSteps(ctx context.Context, id, workflowName string) // RetrieveWorkflowStarted returns the last time a workflow was started for id. func (s *MySQLStorage) RetrieveWorkflowStarted(ctx context.Context, id, workflowName string) (time.Time, error) { ret, err := s.q.GetWorkflowLastStarted(ctx, sqlc.GetWorkflowLastStartedParams{EnrollmentID: id, WorkflowName: workflowName}) - if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return time.Time{}, nil + } else if err != nil { return time.Time{}, err } parsedTime, err := time.Parse(mySQLTimestampFormat, ret) diff --git a/engine/storage/storage.go b/engine/storage/storage.go index 32c3c4e..d6ddf2d 100644 --- a/engine/storage/storage.go +++ b/engine/storage/storage.go @@ -143,6 +143,7 @@ type StepResult struct { type WorkflowStatusStorage interface { // RetrieveWorkflowStarted returns the last time a workflow was started for id. + // Returned time should be nil with no error if workflowName has not yet been started for id. RetrieveWorkflowStarted(ctx context.Context, id, workflowName string) (time.Time, error) // RecordWorkflowStarted stores the started time for workflowName for ids. diff --git a/engine/storage/test/event.go b/engine/storage/test/event.go index 25bccb8..7829412 100644 --- a/engine/storage/test/event.go +++ b/engine/storage/test/event.go @@ -8,6 +8,14 @@ import ( "github.com/micromdm/nanocmd/workflow" ) +func TestEventStatusStorage(t *testing.T, ctx context.Context, store storage.WorkflowStatusStorage) { + _, err := store.RetrieveWorkflowStarted(ctx, "id.should.not.exist", "wfname.whaa") + if err != nil { + // should not error for a non-found item + t.Fatal(err) + } +} + func TestEventStorage(t *testing.T, store storage.EventSubscriptionStorage) { ctx := context.Background() diff --git a/engine/storage/test/test.go b/engine/storage/test/test.go index c7ab3cc..63b8eba 100644 --- a/engine/storage/test/test.go +++ b/engine/storage/test/test.go @@ -42,6 +42,12 @@ func TestEngineStorage(t *testing.T, newStorage func() storage.AllStorage) { t.Run("testEvent", func(t *testing.T) { TestEventStorage(t, s) }) + + ctx := context.Background() + + t.Run("testEventStatus", func(t *testing.T) { + TestEventStatusStorage(t, ctx, s) + }) } func mainTest(t *testing.T, s storage.AllStorage) {