Repository navigation
Conversation
When two connections race to create the same table, the losing CREATE TABLE can be re-prepared while the winner's next schema change lands, and SQLite then reports "table ... already exists" under SQLITE_SCHEMA (17) instead of SQLITE_ERROR (1). processException() only mapped code 1, so the raw PDOException escaped createCollection() and the shared-tables create path never got the duplicate it handles by registering only the losing tenant's metadata. MySQL and MariaDB always raise 1050 here, so only SQLite was affected. Other code 17 errors stay raw. The result codes processException() matches are now named constants. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Code review
SQLite exception mapping now recognizes an existing-table error reported with Walkthrough (4)
Reviewed Tip Comment |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|

Problem
When two connections race to create the same table on SQLite, for example two tenants creating the same collection on a fresh shared-tables schema, the losing
createCollection()can throw a rawPDOExceptioninstead ofUtopia\Database\Exception\Duplicate. A probe in a downstream test harness caught it fromSQLite::createCollection():Cause
The losing
CREATE TABLEcan be re-prepared while the winner's next schema change lands. SQLite then reports "already exists" underSQLITE_SCHEMA(17), notSQLITE_ERROR(1).SQLite::processException()only mapped code 1 + "already exists" to a duplicate, so the code 17 error escaped unmapped.createCollectionInSchema()never saw the duplicate it handles by reusing the shared table and writing only the losing tenant's metadata. MySQL and MariaDB always raise 1050 here, so only SQLite is affected.Fix
processException()maps "already exists" under code 1 or 17 to the sameDuplicate('Collection already exists'). Other code 17 errors, such as "database schema has changed", still pass through raw.processException()matches are now named constants (SQLITE_ERROR,SQLITE_BUSY,SQLITE_SCHEMA,SQLITE_TOOBIG,SQLITE_CONSTRAINT) instead of bare numbers.Tests
EngineErrorMappingTest::testSQLiteCreateRacedIntoASchemaChangeIsADuplicateCollectionfeeds the reported error throughprocessException().EngineErrorMappingTest::testSQLiteSchemaChangeWithoutAnExistingTableStaysRawchecks that other code 17 errors are not swallowed.Adapter/SQLiteSchemaChangeRaceTestruns the adapter andDatabasepaths againstSchemaChangeRacingPDO. That PDO raises an existing-table create the way the race does (code 17). A losingSQLite::createCollection()gets a duplicate and leaves the winner's tables in place. On shared tables, a second tenant'screate()andcreateCollection()both succeed, and its documents stay isolated from the first tenant's.SQLite's timing for this race can't be reproduced deterministically, so the double injects the code 17 SQLite reports.
Before the fix, the mapping test failed (the raw
PDOExceptioncame back) and both race tests errored with the rawPDOException. After the fix, all of them pass. Pint and both PHPStan configurations are clean.Downstream
appwrite-labs/cloud PR #6690 works around this in its test harness at
tests/unit/Database/TimedSQLite.php::processException(). That workaround can be removed once cloud pins a release that includes this fix.🤖 Generated with Claude Code
Summary by CodeRabbit