feat: add constraint violation database exceptions#10359
Conversation
michalsn
left a comment
There was a problem hiding this comment.
Overall, I like this direction. Could we add a few more live tests where the existing table structure allows it, particularly for NOT NULL updates and foreign-key violations?
|
Thanks for the review @michalsn. I pushed updates for the cases you pointed out. While checking the surrounding driver codes, I also added MySQL One small detail: I guarded MariaDB References I checked:
|
4986295 to
ffb4b0d
Compare
- Add a ConstraintViolationException hierarchy for typed integrity errors. - Classify foreign key, not-null, check, unique, and generic constraint violations per driver. - Keep getLastException() aligned with thrown exceptions when DBDebug is disabled. - Update database docs, examples, changelog, and focused tests. Signed-off-by: memleakd <[email protected]>
Signed-off-by: memleakd <[email protected]>
Signed-off-by: memleakd <[email protected]>
Signed-off-by: memleakd <[email protected]>
Signed-off-by: memleakd <[email protected]>
Signed-off-by: memleakd <[email protected]>
Signed-off-by: memleakd <[email protected]>
d01051a to
2e5fa9d
Compare
Signed-off-by: memleakd <[email protected]>
Description
This builds on #9979 by extending the same typed-exception approach to other common database constraint violations.
Database constraint failures can now be represented by more specific exception types when the driver provides enough information:
UniqueConstraintViolationExceptionForeignKeyConstraintViolationExceptionNotNullConstraintViolationExceptionCheckConstraintViolationExceptionAll of them extend the new
ConstraintViolationException, which still extendsDatabaseException.UniqueConstraintViolationExceptionkeeps the same public purpose and remains catchable as aDatabaseException.This lets applications handle common integrity errors directly with
catchblocks instead of inspecting raw driver-specific error codes or matching error messages.When a driver reports an integrity constraint violation that CodeIgniter cannot safely classify more precisely, the parent
ConstraintViolationExceptionis used instead. This keeps the behavior conservative.The same typed exception is also available through
getLastException()whenDBDebugis disabled.I kept the classification close to the existing
UniqueConstraintViolationExceptionimplementation, but I am happy to adjust the hierarchy or naming if maintainers prefer a different shape.Checklist: