Skip to content

feat(connection): add abstract getLastInsertId() method#168

Merged
usernane merged 4 commits into
mainfrom
dev
Jun 13, 2026
Merged

feat(connection): add abstract getLastInsertId() method#168
usernane merged 4 commits into
mainfrom
dev

Conversation

@usernane

@usernane usernane commented Jun 13, 2026

Copy link
Copy Markdown
Member

Summary

Add an abstract getLastInsertId(): int method to the Connection base class with engine-specific implementations, and expose it at the Database class level.

Motivation

Repositories resorted to SELECT MAX(id) queries which are susceptible to race conditions under concurrent inserts. Closes #167.

Changes

  • Added abstract getLastInsertId(): int to Connection
  • Implemented in MySQLConnection using mysqli_insert_id()
  • Implemented in MSSQLConnection using SELECT SCOPE_IDENTITY()
  • SQLiteConnection already had the implementation
  • Added Database::getLastInsertId() convenience method
  • Refactored SchemaChangeRepository to use the new method instead of SELECT MAX(id)
  • Updated transactions example to demonstrate usage
  • Added test for Database::getLastInsertId()

How to Test / Verify

Unit tests cover SQLite (run locally). MySQL/MSSQL tests require running database servers via CI.

php vendor/bin/phpunit -c tests/phpunit.xml --testsuite="SQLite Tests"

Breaking Changes and Migration Steps

None. The only impact is on classes that extend Connection directly — they must now implement getLastInsertId(): int.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed) Docs Repo
  • I ran lint/cs-fixer (if applicable) (composer fix-cs)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #167

@codecov

codecov Bot commented Jun 13, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 88.99%. Comparing base (2b88023) to head (a156977).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
WebFiori/Database/MsSql/MSSQLConnection.php 87.50% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #168      +/-   ##
============================================
+ Coverage     88.53%   88.99%   +0.45%     
+ Complexity     2325     2301      -24     
============================================
  Files            59       59              
  Lines          5749     5770      +21     
============================================
+ Hits           5090     5135      +45     
+ Misses          659      635      -24     
Flag Coverage Δ
php-8.1 88.99% <92.30%> (?)
php-8.2 88.80% <92.30%> (?)
php-8.3 88.80% <92.30%> (+0.26%) ⬆️
php-8.4 88.80% <92.30%> (+0.26%) ⬆️
php-8.5 88.80% <92.30%> (+0.26%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Add getLastInsertId(): int to the Connection base class with
engine-specific implementations:
- MySQLConnection: uses mysqli_insert_id()
- MSSQLConnection: uses SELECT SCOPE_IDENTITY()
- SQLiteConnection: already implemented

Also adds Database::getLastInsertId() convenience method and
refactors SchemaChangeRepository to use it instead of SELECT MAX(id).

Closes #167
Ibrahim BinAlshikh added 3 commits June 13, 2026 04:05
The phpunit.xml uses PHPUnit 10.5 schema but PHP 8.1/8.2 workflows
installed phpunit:9.5.20 globally, which can't parse the config.
This caused clover.xml to never be generated, breaking coverage upload.

Fix: use Composer-installed vendor/bin/phpunit across all workflows
and remove global phpunit tool installations.
By default, release-please only includes feat and fix in the
changelog. Adding "hidden": false to other types ensures ci, test,
refactor, docs, and custom types are visible in release notes.
The test assumed both rows would have the same created_on timestamp,
but when the insert crosses a second boundary they differ. Use each
row's actual timestamp instead.
@sonarqubecloud

Copy link
Copy Markdown

@usernane usernane merged commit dc9843a into main Jun 13, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(connection): Add abstract getLastInsertId() method to Connection class

1 participant