Skip to content
Merged
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
18 changes: 18 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,19 @@ protected function decodePolygon(string $wkb): array
*/
protected const array INVALID_REGEX_ERRORS = [1139];

/**
* ER_TOO_MANY_KEYS, ER_TOO_MANY_KEY_PARTS, ER_TOO_BIG_FIELDLENGTH, ER_TOO_MANY_FIELDS and ER_TOO_BIG_ROWSIZE: a
* table definition the engine refuses for exceeding one of its own limits. InnoDB's row size ceiling (8126
* bytes inline) sits below the 65535 bytes the width check counts against, so these reach the engine.
*/
private const array SCHEMA_LIMIT_ERRORS = [
1069 => 'Index limit reached',
1070 => 'Index attribute limit reached',
1074 => 'Attribute size limit reached',
1117 => 'Column limit reached',
1118 => 'Row width limit reached',
];

/** The session timeout last set, in milliseconds. */
private int $appliedTimeout = 0;

Expand Down Expand Up @@ -1590,6 +1603,11 @@ protected function processException(PDOException $e): Exception
return new DuplicateException('Index already exists', $e->getCode(), $e);
}

$limit = \is_int($e->errorInfo[1] ?? null) ? self::SCHEMA_LIMIT_ERRORS[$e->errorInfo[1]] ?? null : null;
if ($limit !== null) {
return new LimitException($limit, $e->getCode(), $e);
}

if ($e->getCode() === '42000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1071) {
return new IndexException('Index key length exceeds the maximum', $e->getCode(), $e);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -2014,6 +2014,10 @@ protected function processException(PDOException $e): Exception
return new LimitException('Datetime field overflow', $e->getCode(), $e);
}

if ($e->getCode() === '54011' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 7) {
return new LimitException('Column limit reached', $e->getCode(), $e);
}

if ($e->getCode() === '54000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 7 && \str_contains($e->getMessage(), 'index row')) {
return new LimitException('Index row size exceeds the maximum', $e->getCode(), $e);
}
Expand Down
35 changes: 34 additions & 1 deletion tests/unit/EngineErrorMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,39 @@ public function testIndexKeyTooLongIsAnIndexError(Closure $map): void
$this->assertMapped($map, $error, IndexException::class, 'Index key length exceeds the maximum');
}

/**
* @return iterable<string, array{0: Closure(PDOException): Throwable, 1: PDOException, 2: string}>
*/
public static function schemaLimitProvider(): iterable
{
$errors = [
'row size too large' => [self::engineError('42000', 1118, 'SQLSTATE[42000]: Syntax error or access violation: 1118 Row size too large. The maximum row size for the used table type, not counting BLOBs, is 8126. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs'), 'Row width limit reached'],
'too many columns' => [self::engineError('HY000', 1117, 'SQLSTATE[HY000]: General error: 1117 Too many columns'), 'Column limit reached'],
'too many keys' => [self::engineError('42000', 1069, 'SQLSTATE[42000]: Syntax error or access violation: 1069 Too many keys specified; max 64 keys allowed'), 'Index limit reached'],
'too many key parts' => [self::engineError('42000', 1070, 'SQLSTATE[42000]: Syntax error or access violation: 1070 Too many key parts specified; max 16 parts allowed'), 'Index attribute limit reached'],
'column length too big' => [self::engineError('42000', 1074, "SQLSTATE[42000]: Syntax error or access violation: 1074 Column length too big for column 'notes' (max = 16383); use BLOB or TEXT instead"), 'Attribute size limit reached'],
];
foreach (self::mariaDBFamilyProvider() as $engine => [$map]) {
foreach ($errors as $name => [$error, $message]) {
yield "{$engine} {$name}" => [$map, $error, $message];
}
}
yield 'Postgres too many columns' => [
self::postgres(),
self::engineError('54011', 7, 'SQLSTATE[54011]: Too many columns: 7 ERROR: tables can have at most 1600 columns'),
'Column limit reached',
];
}

/**
* @param Closure(PDOException): Throwable $map
*/
#[DataProvider('schemaLimitProvider')]
public function testTableDefinitionPastAnEngineLimitIsALimit(Closure $map, PDOException $error, string $message): void
{
$this->assertMapped($map, $error, LimitException::class, $message);
}

public function testPostgresIndexRowTooLargeIsALimit(): void
{
$error = self::engineError('54000', 7, 'SQLSTATE[54000]: Program limit exceeded: 7 ERROR: index row size 8016 exceeds btree version 4 maximum 2704 for index "engine_orders_by_note"');
Expand All @@ -230,7 +263,7 @@ public function testPostgresIndexRowTooLargeIsALimit(): void

public function testPostgresProgramLimitOtherThanAnIndexRowStaysRaw(): void
{
$error = self::engineError('54000', 7, 'SQLSTATE[54000]: Program limit exceeded: 7 ERROR: tables can have at most 1600 columns');
$error = self::engineError('54000', 7, 'SQLSTATE[54000]: Program limit exceeded: 7 ERROR: row is too big: size 8168, maximum size 8160');

$this->assertSame($error, self::postgres()($error));
}
Expand Down
Loading