Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
123 changes: 123 additions & 0 deletions tests/unit/Adapter/SQLiteSchemaChangeRaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Tests\Unit\Adapter;

use PDO;
use PDOException;
use PHPUnit\Framework\TestCase;
use Utopia\Cache\Adapter\None;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\SQLite;
use Utopia\Database\Attribute;
use Utopia\Database\Collection;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Permission;
use Utopia\Database\Role;
use Utopia\Database\Validator\Authorization;

/**
* Two connections racing to create the same table can see the loser's CREATE TABLE fail as SQLITE_SCHEMA (17)
* rather than SQLITE_ERROR (1). Both mean the table exists, so both must reach callers as a duplicate.
*/
final class SQLiteSchemaChangeRaceTest extends TestCase
{
private const string NAMESPACE = 'schema_race';

private const string COLLECTION = 'tombstones';

private PDO $pdo;

#[\Override]
protected function setUp(): void
{
$this->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<string>
*/
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;
}
}
56 changes: 56 additions & 0 deletions tests/unit/Adapter/SchemaChangeRacingPDO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Tests\Unit\Adapter;

use PDO;
use PDOException;
use PDOStatement;

/**
* Reports a CREATE TABLE for a table that already exists the way SQLite does when the losing create is re-prepared
* while the winner's next schema change lands: SQLITE_SCHEMA (17) carrying the "already exists" message.
*/
final class SchemaChangeRacingPDO extends PDO
{
private const int SQLITE_ERROR = 1;

private const int SQLITE_SCHEMA = 17;

public function __construct()
{
parent::__construct('sqlite::memory:');
}

/**
* @param array<int, mixed> $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;
}
}
15 changes: 15 additions & 0 deletions tests/unit/EngineErrorMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [];
Expand Down
Loading