-
Notifications
You must be signed in to change notification settings - Fork 986
initial address for #1310 - allow the user to check if an error is a … #1316
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good @rotemtam. Thanks for adding this. I've added minor suggestions.
Please use the following commit format when it's possible, it makes my life easier when I'm releasing a new version.
<package>: <message>
For example
dialect/sql/sqlgraph: add constraint error checks
Fixed #ISSUE-NUMBER
dialect/sql/sqlgraph/errors.go
Outdated
"mysql": "Error 1062", | ||
"postgres": "violates unique constraint", | ||
"sqlite": "UNIQUE constraint failed", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use dialect.MySQL
, dialect.PostgreSQL
, etc instead of these strings. Why do we need this, and not just [3]string{...}
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we don't, i'm removing
dialect/sql/sqlgraph/errors.go
Outdated
} | ||
|
||
// IsUniquenessConstraintError returns true if the error resulted from a DB uniqueness constraint error | ||
func IsUniquenessConstraintError(err error) bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func IsUniquenessConstraintError(err error) bool { | |
func IsUniqueConstraintError(err error) bool { |
Uniqueness is too long. Let's change it to Unique
.
dialect/sql/sqlgraph/errors.go
Outdated
// IsConstraintError returns true if the error resulted from a DB constraint error | ||
func IsConstraintError(err error) bool { | ||
return IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// IsConstraintError returns true if the error resulted from a DB constraint error | |
func IsConstraintError(err error) bool { | |
return IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err) | |
} | |
// IsConstraintError returns true if the error resulted from a DB constraint error. | |
func IsConstraintError(err error) bool { | |
var e *ConstraintError | |
return errors.As(err, &e) || IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err) | |
} |
It does make sense to return true is the error is type ConstraintError
as well. Also, please add more details regarding the possible constraint errors.
dialect/sql/sqlgraph/errors.go
Outdated
return IsUniquenessConstraintError(err) || IsForeignKeyConstraintError(err) | ||
} | ||
|
||
// IsUniquenessConstraintError returns true if the error resulted from a DB uniqueness constraint error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// IsUniquenessConstraintError returns true if the error resulted from a DB uniqueness constraint error | |
// IsUniqueConstraintError reports if the error resulted from a DB uniqueness constraint violation. e.g. duplicate value in unique index. |
dialect/sql/sqlgraph/errors.go
Outdated
return false | ||
} | ||
|
||
// IsForeignKeyConstraintError returns true if the error resulted from a DB FK constraint error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// IsForeignKeyConstraintError returns true if the error resulted from a DB FK constraint error | |
// IsForeignKeyConstraintError reports if the error resulted from a DB FK constraint violation. e.g. parent row does not exist. |
Initial work on ent#1310
ad85e8c
to
282d7bb
Compare
require.False(t, pets[2].QueryOwner().ExistX(ctx)) | ||
} | ||
|
||
func ConstraintChecks(t *testing.T, client *ent.Client) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's remove them after it's added to templates (it should be in sqlgraph).
…constraint error and if it is related to uniqueness or FK