diff --git a/internal/db/project.go b/internal/db/project.go index 04bf4d5..f1884fe 100644 --- a/internal/db/project.go +++ b/internal/db/project.go @@ -2,6 +2,7 @@ package db import ( "context" + "errors" "fmt" "github.com/gadget-inc/dateilager/internal/pb" @@ -13,6 +14,13 @@ func CreateProject(ctx context.Context, tx pgx.Tx, project int64, packPatterns [ INSERT INTO dl.projects (id, latest_version, pack_patterns) VALUES ($1, 0, $2) `, project, packPatterns) + + var projectExistsError = errors.New("ERROR: duplicate key value violates unique constraint \"projects_pkey\" (SQLSTATE 23505)") + + if err != nil && err.Error() == projectExistsError.Error() { + return fmt.Errorf("project id already exists") + } + if err != nil { return fmt.Errorf("create project %v, packPatterns %v: %w", project, packPatterns, err) } diff --git a/test/client_new_test.go b/test/client_new_test.go index 9263174..79db2f5 100644 --- a/test/client_new_test.go +++ b/test/client_new_test.go @@ -42,31 +42,31 @@ func TestClientNewProjectEmptyPackPattern(t *testing.T) { } func TestClientNewProjectDuplicateReportsError(t *testing.T) { - tc := util.NewTestCtx(t, auth.Admin, 1) + projectId := int64(1) + + tc := util.NewTestCtx(t, auth.Admin, projectId) defer tc.Close() c, _, close := createTestClient(tc) defer close() - err := c.NewProject(tc.Context(), 1, nil, nil) + err := c.NewProject(tc.Context(), projectId, nil, nil) require.NoError(t, err, "NewProject") /** Create Project Again**/ - tcSecond := util.NewTestCtx(t, auth.Admin, 1) + tcSecond := util.NewTestCtx(t, auth.Admin, projectId) defer tc.Close() cSecond, _, closeSecond := createTestClient(tc) defer closeSecond() - errSecond := cSecond.NewProject(tcSecond.Context(), 1, nil, nil) + errSecond := cSecond.NewProject(tcSecond.Context(), projectId, nil, nil) + want := "project id already exists" - want := "Project with this id already exists" + require.Error(t, errSecond, "NewProject") - if errSecond.Error() != want { + if errSecond == nil { t.Errorf("got %s want %s", errSecond, want) } - - require.NoError(t, err, "NewProject") - }