Fix PHP 8 PDO exception 'There is no active transaction' when autocommitted (as MySQL does when schema changes).

This commit is contained in:
Bartek Fabiszewski 2021-04-22 09:25:36 +02:00
parent 326d025e8d
commit 9f139c70f4
3 changed files with 22 additions and 7 deletions

View File

@ -129,9 +129,14 @@ class MigrateTest extends UloggerDatabaseTestCase {
foreach ($queries[0] as $query) {
uDb::getInstance()->exec($query);
}
uDb::getInstance()->commit();
// make sure the transaction wasn't autocommitted
if (uDb::getInstance()->inTransaction()) {
uDb::getInstance()->commit();
}
} catch (PDOException $e) {
uDb::getInstance()->rollBack();
if (uDb::getInstance()->inTransaction()) {
uDb::getInstance()->rollBack();
}
throw $e;
}
}

View File

@ -89,11 +89,16 @@ function updateSchemas() {
foreach ($queries as $query) {
uDb::getInstance()->exec($query);
}
uDb::getInstance()->commit();
// make sure the transaction wasn't autocommitted
if (uDb::getInstance()->inTransaction()) {
uDb::getInstance()->commit();
}
} catch (PDOException $e) {
echo "Database query failed: {$e->getMessage()}" . PHP_EOL;
echo "Reverting changes..." . PHP_EOL;
uDb::getInstance()->rollBack();
if (uDb::getInstance()->inTransaction()) {
echo "Reverting changes..." . PHP_EOL;
uDb::getInstance()->rollBack();
}
return false;
}
echo PHP_EOL;

View File

@ -96,9 +96,14 @@ switch ($command) {
foreach ($queries as $query) {
$pdo->exec($query);
}
$pdo->commit();
// MySQL autocommits queries that change schema
if ($pdo->inTransaction()) {
$pdo->commit();
}
} catch (PDOException $e) {
$pdo->rollBack();
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
$messages[] = "<span class=\"warn\">{$langSetup["dbqueryfailed"]}</span>";
$messages[] = sprintf($langSetup["serversaid"], "<b>" . htmlentities($e->getMessage()) . "</b>");
$error = true;