Skip to content

Commit 85c5198

Browse files
committed
Merge branch 'main' of https://github.com/coder/coder into bq/refactor-few-services
2 parents f9ea677 + a229855 commit 85c5198

File tree

22 files changed

+1536
-123
lines changed

22 files changed

+1536
-123
lines changed

coderd/apikey.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func (api *API) postToken(rw http.ResponseWriter, r *http.Request) {
9191
TokenName: tokenName,
9292
})
9393
if err != nil {
94-
if database.IsUniqueViolation(err, database.UniqueIndexApiKeyName) {
94+
if database.IsUniqueViolation(err, database.UniqueIndexAPIKeyName) {
9595
httpapi.Write(ctx, rw, http.StatusConflict, codersdk.Response{
9696
Message: fmt.Sprintf("A token with name %q already exists.", tokenName),
9797
Validations: []codersdk.ValidationError{{

coderd/database/errors.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,28 @@ func IsUniqueViolation(err error, uniqueConstraints ...UniqueConstraint) bool {
3737
return false
3838
}
3939

40+
// IsForeignKeyViolation checks if the error is due to a foreign key violation.
41+
// If one or more specific foreign key constraints are given as arguments,
42+
// the error must be caused by one of them. If no constraints are given,
43+
// this function returns true for any foreign key violation.
44+
func IsForeignKeyViolation(err error, foreignKeyConstraints ...ForeignKeyConstraint) bool {
45+
var pqErr *pq.Error
46+
if errors.As(err, &pqErr) {
47+
if pqErr.Code.Name() == "foreign_key_violation" {
48+
if len(foreignKeyConstraints) == 0 {
49+
return true
50+
}
51+
for _, fc := range foreignKeyConstraints {
52+
if pqErr.Constraint == string(fc) {
53+
return true
54+
}
55+
}
56+
}
57+
}
58+
59+
return false
60+
}
61+
4062
// IsQueryCanceledError checks if the error is due to a query being canceled.
4163
func IsQueryCanceledError(err error) bool {
4264
var pqErr *pq.Error

coderd/database/foreign_key_constraint.go

Lines changed: 49 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/unique_constraint.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)