From 0df5b8637f5fbc2eb86719708b705cd47bc21d1e Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Sat, 21 Nov 2026 12:38:24 +1300 Subject: [PATCH] fix(sqlite): map a raced create reported as SQLITE_SCHEMA to a duplicate 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 --- src/Database/Adapter/SQLite.php | 32 +++-- .../Adapter/SQLiteSchemaChangeRaceTest.php | 123 ++++++++++++++++++ tests/unit/Adapter/SchemaChangeRacingPDO.php | 56 ++++++++ tests/unit/EngineErrorMappingTest.php | 15 +++ 4 files changed, 213 insertions(+), 13 deletions(-) create mode 100644 tests/unit/Adapter/SQLiteSchemaChangeRaceTest.php create mode 100644 tests/unit/Adapter/SchemaChangeRacingPDO.php diff --git a/src/Database/Adapter/SQLite.php b/src/Database/Adapter/SQLite.php index a887e5b10..22692dece 100644 --- a/src/Database/Adapter/SQLite.php +++ b/src/Database/Adapter/SQLite.php @@ -99,6 +99,16 @@ class SQLite extends SQL */ private const int REGEXP_PATTERN_CACHE_LIMIT = 256; + private const int SQLITE_ERROR = 1; + + private const int SQLITE_BUSY = 5; + + private const int SQLITE_SCHEMA = 17; + + private const int SQLITE_TOOBIG = 18; + + private const int SQLITE_CONSTRAINT = 19; + private const array MISSING_AGGREGATES = [ Method::Stddev, Method::StddevPop, @@ -1708,37 +1718,33 @@ protected function compileAdapterFilter(Query $query, string $collection, string #[\Override] protected function processException(PDOException $e): Exception { - if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1 && stripos($e->getMessage(), 'already exists') !== false) { + if ($e->getCode() === 'HY000' && \in_array($e->errorInfo[1] ?? null, [self::SQLITE_ERROR, self::SQLITE_SCHEMA], true) && stripos($e->getMessage(), 'already exists') !== false) { return new DuplicateException('Collection already exists', $e->getCode(), $e); } - if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1 && stripos($e->getMessage(), 'no such table') !== false) { + if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === self::SQLITE_ERROR && stripos($e->getMessage(), 'no such table') !== false) { return new NotFoundException('Collection not found', $e->getCode(), $e); } - if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1 && stripos($e->getMessage(), 'no such column') !== false) { + if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === self::SQLITE_ERROR && stripos($e->getMessage(), 'no such column') !== false) { return new NotFoundException('Attribute not found', $e->getCode(), $e); } - if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1 && stripos($e->getMessage(), 'duplicate column name') !== false) { + if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === self::SQLITE_ERROR && stripos($e->getMessage(), 'duplicate column name') !== false) { return new DuplicateException('Attribute already exists', $e->getCode(), $e); } - if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1 && stripos($e->getMessage(), 'integer overflow') !== false) { + if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === self::SQLITE_ERROR && stripos($e->getMessage(), 'integer overflow') !== false) { return new LimitException('Value out of range', $e->getCode(), $e); } - // Duplicate - SQLite uses various error codes for constraint violations: - // - Error code 19 is SQLITE_CONSTRAINT (includes UNIQUE violations) - // - Error code 1 is also used for some duplicate cases - // - SQL state '23000' is integrity constraint violation if ( - ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && ($e->errorInfo[1] === 1 || $e->errorInfo[1] === 19)) || + ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && ($e->errorInfo[1] === self::SQLITE_ERROR || $e->errorInfo[1] === self::SQLITE_CONSTRAINT)) || $e->getCode() === '23000' ) { $message = $e->getMessage(); if ( - (isset($e->errorInfo[1]) && $e->errorInfo[1] === 19) || + (isset($e->errorInfo[1]) && $e->errorInfo[1] === self::SQLITE_CONSTRAINT) || $e->getCode() === '23000' || stripos($message, 'unique') !== false || stripos($message, 'duplicate') !== false @@ -1752,11 +1758,11 @@ protected function processException(PDOException $e): Exception } } - if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 18) { + if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === self::SQLITE_TOOBIG) { return new LimitException('Value too large', $e->getCode(), $e); } - if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 5) { + if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === self::SQLITE_BUSY) { return new ContentionException('Database is locked', $e->getCode(), $e); } diff --git a/tests/unit/Adapter/SQLiteSchemaChangeRaceTest.php b/tests/unit/Adapter/SQLiteSchemaChangeRaceTest.php new file mode 100644 index 000000000..71f128d23 --- /dev/null +++ b/tests/unit/Adapter/SQLiteSchemaChangeRaceTest.php @@ -0,0 +1,123 @@ +pdo = new SchemaChangeRacingPDO(); + } + + public function testACreateThatLosesToASchemaChangeIsADuplicateAndKeepsTheWinnersTables(): void + { + $winner = $this->adapter(new SQLite($this->pdo)); + $winner->createCollection(self::COLLECTION, [Attribute::string('name', size: 64)]); + + $loser = $this->adapter(new SQLite($this->pdo)); + + try { + $loser->createCollection(self::COLLECTION, [Attribute::string('name', size: 64)]); + $this->fail('A create that lost the race must be reported'); + } catch (DuplicateException $error) { + $this->assertSame('Collection already exists', $error->getMessage()); + $previous = $error->getPrevious(); + $this->assertInstanceOf(PDOException::class, $previous); + $this->assertSame(17, $previous->errorInfo[1] ?? null); + } + + $this->assertSame([self::NAMESPACE.'_'.self::COLLECTION, self::NAMESPACE.'_'.self::COLLECTION.'_perms'], $this->tables()); + } + + public function testASharedTableCreateThatLosesToASchemaChangeStillRegistersTheLosingTenant(): void + { + $winner = $this->database(new SQLite($this->pdo), 1); + $winner->createCollection($this->collection()); + + $loser = $this->database(new SQLite($this->pdo), 2); + $loser->createCollection($this->collection()); + + $this->assertSame(self::COLLECTION, $loser->getCollection(self::COLLECTION)->getId()); + $loser->createDocument(self::COLLECTION, new Document(['$id' => 'loser', 'name' => 'loser'])); + $this->assertSame(1, $loser->count(self::COLLECTION)); + $this->assertSame(0, $winner->count(self::COLLECTION)); + } + + private function adapter(SQLite $adapter): SQLite + { + $adapter->setDatabase('main'); + $adapter->setNamespace(self::NAMESPACE); + + return $adapter; + } + + private function database(SQLite $adapter, int $tenant): Database + { + $database = new Database($adapter, new Cache(new None())); + $database + ->setDatabase('main') + ->setNamespace(self::NAMESPACE) + ->setSharedTables(true) + ->setTenant($tenant) + ->setAuthorization(new Authorization()); + $database->create(); + + return $database; + } + + private function collection(): Collection + { + return Collection::create( + id: self::COLLECTION, + attributes: [Attribute::string('name', size: 64)], + permissions: [ + Permission::create(Role::any()), + Permission::read(Role::any()), + ], + ); + } + + /** + * @return list + */ + private function tables(): array + { + $statement = $this->pdo->query("SELECT name FROM sqlite_master WHERE type = 'table' AND name LIKE '".self::NAMESPACE."\\_%' ESCAPE '\\' ORDER BY name"); + $this->assertInstanceOf(\PDOStatement::class, $statement); + + $names = []; + foreach ($statement->fetchAll(PDO::FETCH_COLUMN) as $name) { + if (\is_string($name)) { + $names[] = $name; + } + } + + return $names; + } +} diff --git a/tests/unit/Adapter/SchemaChangeRacingPDO.php b/tests/unit/Adapter/SchemaChangeRacingPDO.php new file mode 100644 index 000000000..730feb659 --- /dev/null +++ b/tests/unit/Adapter/SchemaChangeRacingPDO.php @@ -0,0 +1,56 @@ + $options + */ + #[\Override] + public function prepare(string $query, array $options = []): PDOStatement|false + { + try { + return parent::prepare($query, $options); + } catch (PDOException $error) { + if (($error->errorInfo[1] ?? null) !== self::SQLITE_ERROR || \stripos($error->getMessage(), 'already exists') === false) { + throw $error; + } + + throw self::schemaChanged($error); + } + } + + private static function schemaChanged(PDOException $error): PDOException + { + $message = \str_replace('General error: '.self::SQLITE_ERROR.' ', 'General error: '.self::SQLITE_SCHEMA.' ', $error->getMessage()); + + $schemaChanged = new class ($message, $error) extends PDOException { + public function __construct(string $message, PDOException $previous) + { + parent::__construct($message, previous: $previous); + $this->code = $previous->getCode(); + } + }; + $schemaChanged->errorInfo = [$error->errorInfo[0] ?? 'HY000', self::SQLITE_SCHEMA, $error->errorInfo[2] ?? $message]; + + return $schemaChanged; + } +} diff --git a/tests/unit/EngineErrorMappingTest.php b/tests/unit/EngineErrorMappingTest.php index 79f95cdb2..51f3ffe3a 100644 --- a/tests/unit/EngineErrorMappingTest.php +++ b/tests/unit/EngineErrorMappingTest.php @@ -23,6 +23,7 @@ use Utopia\Database\Document; use Utopia\Database\Exception\Character as CharacterException; use Utopia\Database\Exception\Contention as ContentionException; +use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Index as IndexException; use Utopia\Database\Exception\Limit as LimitException; use Utopia\Database\Exception\NotFound as NotFoundException; @@ -349,6 +350,20 @@ public function testSQLiteDoesNotTreatTheMySQLTimeoutCodeAsATimeout(): void $this->assertSame($error, self::sqlite()($error)); } + public function testSQLiteCreateRacedIntoASchemaChangeIsADuplicateCollection(): void + { + $error = self::engineError('HY000', 17, 'SQLSTATE[HY000]: General error: 17 table `shared_changelog_tombstones` already exists'); + + $this->assertMapped(self::sqlite(), $error, DuplicateException::class, 'Collection already exists'); + } + + public function testSQLiteSchemaChangeWithoutAnExistingTableStaysRaw(): void + { + $error = self::engineError('HY000', 17, 'SQLSTATE[HY000]: General error: 17 database schema has changed'); + + $this->assertSame($error, self::sqlite()($error)); + } + public function testPostgresDeleteCollectionWithoutItsTableIsNotFoundAndStillDropsThePermissionsTable(): void { $statements = [];