Skip to content

Commit 4986295

Browse files
committed
refactor: avoid message-based checks
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent 658a40b commit 4986295

4 files changed

Lines changed: 25 additions & 28 deletions

File tree

system/Database/MySQLi/Connection.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,7 @@ protected function isCheckConstraintViolation(int|string $code, string $message)
134134
}
135135

136136
// MariaDB reports CHECK failures as ER_CONSTRAINT_FAILED, while MySQL uses 4025 for other errors.
137-
if ($code !== 4025) {
138-
return false;
139-
}
140-
141-
$message = strtolower($message);
142-
143-
return str_contains($message, 'constraint') && str_contains($message, 'failed');
137+
return $code === 4025 && str_contains(strtolower($this->getVersion()), 'mariadb');
144138
}
145139

146140
/**

system/Database/SQLSRV/Connection.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,6 @@ protected function isUniqueConstraintViolation(int|string $code, string $message
117117
return false;
118118
}
119119

120-
/**
121-
* Checks whether the native database error represents a foreign key constraint violation.
122-
*/
123-
protected function isForeignKeyConstraintViolation(int|string $code, string $message): bool
124-
{
125-
return $this->getVendorErrorCode($code) === 547
126-
&& $this->hasSQLState($code, '23000')
127-
&& str_contains($message, 'FOREIGN KEY constraint');
128-
}
129-
130120
/**
131121
* Checks whether the native database error represents a NOT NULL constraint violation.
132122
*/

tests/system/Database/ConstraintViolationExceptionTest.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public static function provideCreatesConstraintViolationExceptions(): iterable
121121
];
122122

123123
yield 'MySQLi check MariaDB' => [
124-
self::connection(MySQLiConnection::class, 'MySQLi'),
124+
self::mysqliConnectionWithVersion('10.11.0-MariaDB'),
125125
4025,
126126
'CONSTRAINT `positive_amount` failed.',
127127
CheckConstraintViolationException::class,
@@ -225,13 +225,6 @@ public static function provideCreatesConstraintViolationExceptions(): iterable
225225
UniqueConstraintViolationException::class,
226226
];
227227

228-
yield 'SQLSRV foreign key' => [
229-
self::connection(SQLSRVConnection::class, 'SQLSRV'),
230-
'23000/547',
231-
'The INSERT statement conflicted with the FOREIGN KEY constraint.',
232-
ForeignKeyConstraintViolationException::class,
233-
];
234-
235228
yield 'SQLSRV generic constraint' => [
236229
self::connection(SQLSRVConnection::class, 'SQLSRV'),
237230
'23000/547',
@@ -309,7 +302,7 @@ public function testCreatesBaseDatabaseExceptionForNonConstraintError(): void
309302

310303
public function testCreatesBaseDatabaseExceptionForMySQLiNonConstraint4025(): void
311304
{
312-
$exception = self::connection(MySQLiConnection::class, 'MySQLi')
305+
$exception = self::mysqliConnectionWithVersion('8.4.0')
313306
->createDatabaseException('InnoDB autoextend size is out of range.', 4025);
314307

315308
$this->assertInstanceOf(DatabaseException::class, $exception);
@@ -348,6 +341,21 @@ private static function connection(string $connectionClass, string $driver): Bas
348341
return new $connectionClass(self::config($driver));
349342
}
350343

344+
private static function mysqliConnectionWithVersion(string $version): BaseConnection
345+
{
346+
return new class (self::config('MySQLi'), $version) extends MySQLiConnection {
347+
public function __construct(array $params, private readonly string $version)
348+
{
349+
parent::__construct($params);
350+
}
351+
352+
public function getVersion(): string
353+
{
354+
return $this->version;
355+
}
356+
};
357+
}
358+
351359
/**
352360
* @return array<string, mixed>
353361
*/

tests/system/Database/Live/ConstraintViolationExceptionTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Database\Live;
1515

16+
use CodeIgniter\Database\Exceptions\ConstraintViolationException;
1617
use CodeIgniter\Database\Exceptions\ForeignKeyConstraintViolationException;
1718
use CodeIgniter\Database\Exceptions\NotNullConstraintViolationException;
1819
use CodeIgniter\Database\Forge;
@@ -70,12 +71,16 @@ public function testThrowsNotNullConstraintViolationExceptionForUpdateWithDebugE
7071
->update(['name' => null]);
7172
}
7273

73-
public function testThrowsForeignKeyConstraintViolationExceptionWithDebugEnabled(): void
74+
public function testThrowsConstraintViolationExceptionForForeignKeyWithDebugEnabled(): void
7475
{
7576
$this->enableDBDebug();
7677
$this->createForeignKeyTables();
7778

78-
$this->expectException(ForeignKeyConstraintViolationException::class);
79+
$expectedException = $this->db->DBDriver === 'SQLSRV'
80+
? ConstraintViolationException::class
81+
: ForeignKeyConstraintViolationException::class;
82+
83+
$this->expectException($expectedException);
7984

8085
$this->db->table('cv_child')->insert([
8186
'id' => 1,

0 commit comments

Comments
 (0)