Skip to content

Commit af3f46d

Browse files
committed
Fix DatabaseTestTrait for MariaDB
1 parent 10db2ee commit af3f46d

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/Traits/DatabaseTestTrait.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,32 @@ protected function getConnection(): PDO
6262
*/
6363
private function unsetStatsExpiry()
6464
{
65-
if (version_compare($this->getMySqlVersion(), '8.0.0') >= 0) {
65+
$isMySql = strpos($this->getDatabaseVariable('version_comment'), 'MySQL') !== false;
66+
$version = $this->getDatabaseVariable('version');
67+
68+
if ($isMySql && version_compare($version, '8.0.0') >= 0) {
6669
$this->getConnection()->exec('SET information_schema_stats_expiry=0;');
6770
}
6871
}
6972

7073
/**
71-
* Get MySql version.
74+
* Get database variable.
7275
*
73-
* @throws UnexpectedValueException
76+
* @param string $variable The variable
7477
*
75-
* @return string The version
78+
* @return string The value
7679
*/
77-
private function getMySqlVersion(): string
80+
protected function getDatabaseVariable(string $variable): string
7881
{
79-
$statement = $this->getConnection()->query("SHOW VARIABLES LIKE 'version';");
80-
81-
if ($statement === false) {
82-
throw new UnexpectedValueException('Invalid sql statement');
82+
$statement = $this->getConnection()->prepare('SHOW VARIABLES LIKE ?');
83+
if (!$statement || $statement->execute([$variable]) === false) {
84+
throw new UnexpectedValueException('Invalid SQL statement');
8385
}
8486

8587
$row = $statement->fetch(PDO::FETCH_ASSOC);
8688

8789
if ($row === false) {
88-
throw new UnexpectedValueException('Version not found');
90+
throw new UnexpectedValueException(sprintf('Database variable not defined: %s', $variable));
8991
}
9092

9193
return (string)$row['Value'];

0 commit comments

Comments
 (0)