Skip to content

Commit

Permalink
Fix VARCHAR -> VARCHAR truncation and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PineappleIOnic committed Aug 30, 2024
1 parent 4ecdda9 commit 50aed1c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -2245,7 +2245,8 @@ protected function processException(PDOException $e): void
}

// Data is too big for column resize
if ($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) {
if (($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) ||
($e->getCode() === '01000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1265)) {
throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e);
}

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 @@ -110,7 +110,8 @@ protected function processException(PDOException $e): void
}

// Data is too big for column resize
if ($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) {
if (($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) ||
($e->getCode() === '01000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1265)) {
throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e);
}

Expand Down
11 changes: 11 additions & 0 deletions tests/e2e/Adapter/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -5964,6 +5964,17 @@ public function testUpdateAttributeSize(): void
// Test going down in size when data isn't too big.
static::getDatabase()->updateDocument('resize_test', $document->getId(), $document->setAttribute('resize_me', $this->createRandomString(128)));
static::getDatabase()->updateAttribute('resize_test', 'resize_me', Database::VAR_STRING, 128, true);

// VARCHAR -> VARCHAR Truncation Test
static::getDatabase()->updateAttribute('resize_test', 'resize_me', Database::VAR_STRING, 1000, true);
static::getDatabase()->updateDocument('resize_test', $document->getId(), $document->setAttribute('resize_me', $this->createRandomString(1000)));

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());
}
}

/**
Expand Down

0 comments on commit 50aed1c

Please sign in to comment.