Skip to content

Commit

Permalink
Merge pull request #445 from utopia-php/feat-resizing-string-attributes
Browse files Browse the repository at this point in the history
Add Truncate Error class for resizing string attributes
# Conflicts:
#	src/Database/Adapter/MariaDB.php
#	src/Database/Adapter/MySQL.php
#	src/Database/Adapter/Postgres.php
  • Loading branch information
abnegate committed Sep 12, 2024
1 parent 46fa933 commit 6e2547a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Truncate as TruncateException;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;

Expand Down Expand Up @@ -2263,7 +2264,7 @@ protected function processException(PDOException $e): \Exception
// Data is too big for column resize
if (($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) ||
($e->getCode() === '01000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1265)) {
return new DatabaseException('Resize would result in data truncation', $e->getCode(), $e);
return new TruncateException('Resize would result in data truncation', $e->getCode(), $e);
}

// Duplicate index
Expand Down
3 changes: 2 additions & 1 deletion src/Database/Adapter/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Truncate as TruncateException;

class MySQL extends MariaDB
{
Expand Down Expand Up @@ -108,7 +109,7 @@ protected function processException(PDOException $e): \Exception
// Data is too big for column resize
if (($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) ||
($e->getCode() === '01000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1265)) {
return new DatabaseException('Resize would result in data truncation', $e->getCode(), $e);
return new TruncateException('Resize would result in data truncation', $e->getCode(), $e);
}

return $e;
Expand Down
1 change: 1 addition & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Duplicate as DuplicateException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Truncate as TruncateException;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;

Expand Down
9 changes: 9 additions & 0 deletions src/Database/Exception/Truncate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Utopia\Database\Exception;

use Utopia\Database\Exception;

class Truncate extends Exception
{
}
7 changes: 3 additions & 4 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Utopia\Database\Exception\Restricted as RestrictedException;
use Utopia\Database\Exception\Structure as StructureException;
use Utopia\Database\Exception\Timeout as TimeoutException;
use Utopia\Database\Exception\Truncate as TruncateException;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
Expand Down Expand Up @@ -5922,8 +5923,7 @@ public function testUpdateAttributeSize(): void
try {
static::getDatabase()->updateAttribute('resize_test', 'resize_me', Database::VAR_STRING, 128, true);
$this->fail('Succeeded updating attribute size to smaller size with data that is too big');
} catch (DatabaseException $e) {
$this->assertEquals('Resize would result in data truncation', $e->getMessage());
} catch (TruncateException $e) {
}

// Test going down in size when data isn't too big.
Expand All @@ -5937,8 +5937,7 @@ public function testUpdateAttributeSize(): void
try {
static::getDatabase()->updateAttribute('resize_test', 'resize_me', Database::VAR_STRING, 128, true);
$this->fail('Succeeded updating attribute size to smaller size with data that is too big');
} catch (DatabaseException $e) {
$this->assertEquals('Resize would result in data truncation', $e->getMessage());
} catch (TruncateException $e) {
}
}

Expand Down

0 comments on commit 6e2547a

Please sign in to comment.