diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 83779ef49..4eeb576b1 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -787,9 +787,9 @@ public function createDocument(string $collection, Document $document): Document $stmtPermissions->execute(); } } catch (Throwable $e) { + $this->getPDO()->rollBack(); switch ($e->getCode()) { case 23505: - $this->getPDO()->rollBack(); throw new Duplicate('Duplicated document: ' . $e->getMessage()); default: throw $e; diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 801c8b089..7fd27ca71 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -1410,6 +1410,40 @@ public function testCreateDocumentDefaults(): void static::getDatabase()->deleteCollection('defaults'); } + public function testInvalidUtfCharacters(): void + { + // This test is a reminder we can not insert non UTF chars to UTF field + $collection = 'invalid_characters'; + + static::getDatabase()->createCollection($collection); + static::getDatabase()->createAttribute($collection, 'str', Database::VAR_STRING, 128, true); + + // Test insert of null + $str = "\x00"; // Use double quotes! + $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([ + 'str' => $str + ]))); + + // Test insert of null + $str = "\u0000"; // Use double quotes! + $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([ + 'str' => $str + ]))); + + // Test insert of null Use double quotes + $str = "\000"; // Use double quotes! + $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([ + 'str' => $str, + ]))); + + // Suggestion for fix: cleanup non-utf chars + $str = "\xE2\x94"; // Use double quotes! + $str = mb_convert_encoding($str, 'UTF-8', 'UTF-8'); + $this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createDocument($collection, new Document([ + 'str' => $str + ]))); + } + /** * @throws AuthorizationException|LimitException|DuplicateException|StructureException|Exception|Throwable */