feat(dbsql): Uniqueness and foreign key violation detections - #246
Open
onelapahead wants to merge 2 commits into
Open
feat(dbsql): Uniqueness and foreign key violation detections#246onelapahead wants to merge 2 commits into
onelapahead wants to merge 2 commits into
Conversation
Callers that want to return a 409 when a row already exists currently either run a pre-insert existence query (which races against concurrent inserts and yields an opaque FF00177 500 for the loser) or string-match the driver's error text. Both are fragile. This adds Database.IsUniqueViolation and Database.IsForeignKeyViolation, which inspect the full error chain for an integrity constraint violation so callers can rely on the database constraint for enforcement and map the resulting error to a meaningful response: - SQLFeatures.ConstraintViolationClassifier is an optional provider hook that returns the SQLSTATE and, where the driver exposes it, the violated constraint name - When no classifier is set, the fallback matches SQLSTATE 23505 / 23503 via a SQLState() method on the error (lib/pq and pgx both implement it), without adding a Postgres driver dependency to this module - The SQLite provider gets a built-in classifier using the driver's extended constraint codes - Constraint-name filtering fails closed when the name cannot be determined Also adds Unwrap() to i18n's ffError so errors.Is/errors.As can reach the original cause through WrapError - previously the embedded error interface hid the underlying Unwrap and the chain stopped at the ffError. Signed-off-by: hfuss <hayden.fuss@kaleido.io>
Signed-off-by: hfuss <hayden.fuss@kaleido.io>
Contributor
Author
|
On the point about
So this would fix those bugs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Callers that want to return a 409 when a row already exists currently either run a pre-insert existence query (which races against concurrent inserts and yields an opaque 500 for the loser) or string-match the driver's error text. Neither are what we want.
This adds
Database.IsUniqueViolationandDatabase.IsForeignKeyViolation, which inspect the full error chain for an integrity constraint violation so callers can rely on the database constraint for enforcement and map the resulting error to a meaningful response.For PostgreSQL, you can do something like:
Register that in your DB. So then an
Insertcan look roughly like:so we can actually detect such specific violations and test them more thoroughly. For multiple uniqueness constraints,etc. we can use
switch.Behavior change
i18nsupportingUnwrapis really really useful so when we doi18n.WrapError, we can still doerror.Is(err, sentinel)for sentinel pattern errors (whether from other libs, or sentinel errors we create).If there is code that relies on the unwrap not being supported, and has written itself as such thats a breaking change. But an edge case that could be considered a bug, and has made
i18nhave some limitations as I try to use it, I think its a breaking change that can be documented in a minor release since it is contained and small. And its behavior that is hard to rely on - you would have to have code that could either returni18nerrors or unwrapped errors, andifconditions trying to distinguish/check for the unwrapped errors.