Skip to content

feat: add constraint violation database exceptions#10359

Open
memleakd wants to merge 6 commits into
codeigniter4:4.8from
memleakd:feat/constraint-violation-exceptions
Open

feat: add constraint violation database exceptions#10359
memleakd wants to merge 6 commits into
codeigniter4:4.8from
memleakd:feat/constraint-violation-exceptions

Conversation

@memleakd

Copy link
Copy Markdown
Contributor

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:

  • UniqueConstraintViolationException
  • ForeignKeyConstraintViolationException
  • NotNullConstraintViolationException
  • CheckConstraintViolationException

All of them extend the new ConstraintViolationException, which still extends DatabaseException. UniqueConstraintViolationException keeps the same public purpose and remains catchable as a DatabaseException.

This lets applications handle common integrity errors directly with catch blocks 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 ConstraintViolationException is used instead. This keeps the behavior conservative.

The same typed exception is also available through getLastException() when DBDebug is disabled.

I kept the classification close to the existing UniqueConstraintViolationException implementation, but I am happy to adjust the hierarchy or naming if maintainers prefer a different shape.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

- 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 <121398829+memleakd@users.noreply.github.com>
@github-actions github-actions Bot added the 4.8 PRs that target the `4.8` branch. label Jun 30, 2026
memleakd added 2 commits June 30, 2026 19:02
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread system/Database/OCI8/Connection.php Outdated
Comment thread system/Database/MySQLi/Connection.php Outdated
Comment thread system/Database/MySQLi/Connection.php Outdated
@michalsn michalsn added the enhancement PRs that improve existing functionalities label Jun 30, 2026
memleakd added 2 commits June 30, 2026 23:52
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@memleakd

Copy link
Copy Markdown
Contributor Author

Thanks for the review @michalsn. I pushed updates for the cases you pointed out.

While checking the surrounding driver codes, I also added MySQL 3673 for the same NOT NULL bucket as 1048.

One small detail: I guarded MariaDB 4025 by message instead of treating the code alone as enough, because MariaDB documents it as ER_CONSTRAINT_FAILED, but MySQL uses the same numeric code for ER_INNODB_AUTOEXTEND_SIZE_OUT_OF_RANGE. I added a negative test for that MySQL case too.

References I checked:

@michalsn michalsn left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update.

Comment thread system/Database/MySQLi/Connection.php Outdated
Comment thread system/Database/SQLSRV/Connection.php Outdated
@memleakd memleakd force-pushed the feat/constraint-violation-exceptions branch from fdac7fa to 4986295 Compare July 1, 2026 08:33
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
@memleakd memleakd force-pushed the feat/constraint-violation-exceptions branch from 4986295 to ffb4b0d Compare July 1, 2026 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4.8 PRs that target the `4.8` branch. enhancement PRs that improve existing functionalities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants