From 9f139c70f478a3d6d27cb8d9b6660f29372412d8 Mon Sep 17 00:00:00 2001 From: Bartek Fabiszewski Date: Thu, 22 Apr 2021 09:25:36 +0200 Subject: [PATCH] Fix PHP 8 PDO exception 'There is no active transaction' when autocommitted (as MySQL does when schema changes). --- .tests/tests/MigrateTest.php | 9 +++++++-- scripts/migrate_to_1_x.php | 11 ++++++++--- scripts/setup.php | 9 +++++++-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/.tests/tests/MigrateTest.php b/.tests/tests/MigrateTest.php index 9873d56..ebf38dd 100644 --- a/.tests/tests/MigrateTest.php +++ b/.tests/tests/MigrateTest.php @@ -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; } } diff --git a/scripts/migrate_to_1_x.php b/scripts/migrate_to_1_x.php index 9f722cc..c888419 100644 --- a/scripts/migrate_to_1_x.php +++ b/scripts/migrate_to_1_x.php @@ -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; diff --git a/scripts/setup.php b/scripts/setup.php index ff39582..224e11c 100644 --- a/scripts/setup.php +++ b/scripts/setup.php @@ -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[] = "{$langSetup["dbqueryfailed"]}"; $messages[] = sprintf($langSetup["serversaid"], "" . htmlentities($e->getMessage()) . ""); $error = true;