From 199590abf941645b9b0aad42118149274e3c24d6 Mon Sep 17 00:00:00 2001 From: Jake Barnby Date: Mon, 23 Nov 2026 16:08:14 +1300 Subject: [PATCH] fix(adapter): map engine table-limit errors to Limit MySQL raises 1118 (row size too large) against InnoDB's 8126-byte inline row ceiling, well below the 65535 bytes the width pre-check counts, so a wide table of varchar columns reaches the engine and comes back as a raw PDOException. Callers that classify failures by exception type (the Appwrite databases worker treats Schema as a rejection of the request) cannot tell it apart from an outage, so the job was retried until its delivery budget ran out. Map ER_TOO_BIG_ROWSIZE, ER_TOO_MANY_FIELDS, ER_TOO_MANY_KEYS, ER_TOO_MANY_KEY_PARTS and ER_TOO_BIG_FIELDLENGTH on MariaDB/MySQL, and 54011 (too many columns) on Postgres, to Limit. These are matched on the engine errno alone because the SQLSTATE differs between them (1117 is HY000, the rest 42000). Co-Authored-By: Claude Opus 5.5 --- src/Database/Adapter/MariaDB.php | 18 ++++++++++++++ src/Database/Adapter/Postgres.php | 4 +++ tests/unit/EngineErrorMappingTest.php | 35 ++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 7b86d6d00..f8bca6ce6 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -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; @@ -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); } diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 81e749670..60a3f7718 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -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); } diff --git a/tests/unit/EngineErrorMappingTest.php b/tests/unit/EngineErrorMappingTest.php index 79f95cdb2..5452efb77 100644 --- a/tests/unit/EngineErrorMappingTest.php +++ b/tests/unit/EngineErrorMappingTest.php @@ -221,6 +221,39 @@ public function testIndexKeyTooLongIsAnIndexError(Closure $map): void $this->assertMapped($map, $error, IndexException::class, 'Index key length exceeds the maximum'); } + /** + * @return iterable + */ + 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"'); @@ -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)); }