Skip to content

Commit 94484a0

Browse files
committed
refactor unique constraint error mocking a little
1 parent 9c769f8 commit 94484a0

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@ import (
3333

3434
var validProxyByHostnameRegex = regexp.MustCompile(`^[a-zA-Z0-9._-]+$`)
3535

36-
var errForeignKeyConstraint = &pq.Error{
37-
Code: "23503",
38-
Message: "update or delete on table violates foreign key constraint",
39-
}
40-
41-
var errDuplicateKey = &pq.Error{
42-
Code: "23505",
43-
Message: "duplicate key value violates unique constraint",
44-
}
36+
// A full mapping of error codes from pq v1.10.9 can be found here:
37+
// https://github.com/lib/pq/blob/2a217b94f5ccd3de31aec4152a541b9ff64bed05/error.go#L75
38+
var (
39+
errForeignKeyConstraint = &pq.Error{
40+
Code: "23503", // "foreign_key_violation"
41+
Message: "update or delete on table violates foreign key constraint",
42+
}
43+
errUniqueConstraint = &pq.Error{
44+
Code: "23505", // "unique_violation"
45+
Message: "duplicate key value violates unique constraint",
46+
}
47+
)
4548

4649
// New returns an in-memory fake of the database.
4750
func New() database.Store {
@@ -5809,7 +5812,7 @@ func (q *FakeQuerier) InsertDBCryptKey(_ context.Context, arg database.InsertDBC
58095812

58105813
for _, key := range q.dbcryptKeys {
58115814
if key.Number == arg.Number {
5812-
return errDuplicateKey
5815+
return errUniqueConstraint
58135816
}
58145817
}
58155818

@@ -5913,7 +5916,7 @@ func (q *FakeQuerier) InsertGroup(_ context.Context, arg database.InsertGroupPar
59135916
for _, group := range q.groups {
59145917
if group.OrganizationID == arg.OrganizationID &&
59155918
group.Name == arg.Name {
5916-
return database.Group{}, errDuplicateKey
5919+
return database.Group{}, errUniqueConstraint
59175920
}
59185921
}
59195922

@@ -5944,7 +5947,7 @@ func (q *FakeQuerier) InsertGroupMember(_ context.Context, arg database.InsertGr
59445947
for _, member := range q.groupMembers {
59455948
if member.GroupID == arg.GroupID &&
59465949
member.UserID == arg.UserID {
5947-
return errDuplicateKey
5950+
return errUniqueConstraint
59485951
}
59495952
}
59505953

@@ -6028,7 +6031,7 @@ func (q *FakeQuerier) InsertOAuth2ProviderApp(_ context.Context, arg database.In
60286031

60296032
for _, app := range q.oauth2ProviderApps {
60306033
if app.Name == arg.Name {
6031-
return database.OAuth2ProviderApp{}, errDuplicateKey
6034+
return database.OAuth2ProviderApp{}, errUniqueConstraint
60326035
}
60336036
}
60346037

@@ -6390,7 +6393,7 @@ func (q *FakeQuerier) InsertUser(_ context.Context, arg database.InsertUserParam
63906393

63916394
for _, user := range q.users {
63926395
if user.Username == arg.Username && !user.Deleted {
6393-
return database.User{}, errDuplicateKey
6396+
return database.User{}, errUniqueConstraint
63946397
}
63956398
}
63966399

@@ -6803,7 +6806,7 @@ func (q *FakeQuerier) InsertWorkspaceProxy(_ context.Context, arg database.Inser
68036806
lastRegionID := int32(0)
68046807
for _, p := range q.workspaceProxies {
68056808
if !p.Deleted && p.Name == arg.Name {
6806-
return database.WorkspaceProxy{}, errDuplicateKey
6809+
return database.WorkspaceProxy{}, errUniqueConstraint
68076810
}
68086811
if p.RegionID > lastRegionID {
68096812
lastRegionID = p.RegionID
@@ -7197,7 +7200,7 @@ func (q *FakeQuerier) UpdateOAuth2ProviderAppByID(_ context.Context, arg databas
71977200

71987201
for _, app := range q.oauth2ProviderApps {
71997202
if app.Name == arg.Name && app.ID != arg.ID {
7200-
return database.OAuth2ProviderApp{}, errDuplicateKey
7203+
return database.OAuth2ProviderApp{}, errUniqueConstraint
72017204
}
72027205
}
72037206

@@ -7258,11 +7261,7 @@ func (q *FakeQuerier) UpdateOrganization(_ context.Context, arg database.UpdateO
72587261
// non-unique names during updates.
72597262
for _, org := range q.organizations {
72607263
if org.Name == arg.Name && org.ID != arg.ID {
7261-
// https://github.com/lib/pq/blob/3d613208bca2e74f2a20e04126ed30bcb5c4cc27/error.go#L178
7262-
return database.Organization{}, &pq.Error{
7263-
Code: pq.ErrorCode("23505"), // "unique_violation"
7264-
Constraint: string(database.UniqueOrganizationsName),
7265-
}
7264+
return database.Organization{}, errUniqueConstraint
72667265
}
72677266
}
72687267

@@ -7873,7 +7872,7 @@ func (q *FakeQuerier) UpdateWorkspace(_ context.Context, arg database.UpdateWork
78737872
continue
78747873
}
78757874
if other.Name == arg.Name {
7876-
return database.Workspace{}, errDuplicateKey
7875+
return database.Workspace{}, errUniqueConstraint
78777876
}
78787877
}
78797878

@@ -8213,7 +8212,7 @@ func (q *FakeQuerier) UpdateWorkspaceProxy(_ context.Context, arg database.Updat
82138212

82148213
for _, p := range q.workspaceProxies {
82158214
if p.Name == arg.Name && p.ID != arg.ID {
8216-
return database.WorkspaceProxy{}, errDuplicateKey
8215+
return database.WorkspaceProxy{}, errUniqueConstraint
82178216
}
82188217
}
82198218

0 commit comments

Comments
 (0)