From c6e1147bd9abdb31ab50a1ef20e54c108ce01aa5 Mon Sep 17 00:00:00 2001 From: thePanz Date: Thu, 27 Jun 2024 16:41:45 +0200 Subject: [PATCH] Fix(migrations) Handle transaction on PDO and PHPv8 --- lib/Doctrine/Export.php | 2 +- lib/Doctrine/Migration.php | 36 ++++++++++++++---------------- lib/Doctrine/TransactionHelper.php | 24 ++++++++++++++++++++ 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 lib/Doctrine/TransactionHelper.php diff --git a/lib/Doctrine/Export.php b/lib/Doctrine/Export.php index ab56d6620..f09610451 100644 --- a/lib/Doctrine/Export.php +++ b/lib/Doctrine/Export.php @@ -1221,7 +1221,7 @@ public function exportClasses(array $classes) } } - $connection->commit(); + Doctrine_TransactionHelper::commitIfInTransaction($connection); } } diff --git a/lib/Doctrine/Migration.php b/lib/Doctrine/Migration.php index 0208ce540..8a6fa1d74 100644 --- a/lib/Doctrine/Migration.php +++ b/lib/Doctrine/Migration.php @@ -335,24 +335,21 @@ public function migrate($to = null, $dryRun = false) if ($dryRun) { return false; - } else { - $this->_throwErrorsException(); - } - } else { - if ($dryRun) { - $this->_connection->rollback(); - if ($this->hasErrors()) { - return false; - } else { - return $to; - } - } else { - $this->_connection->commit(); - $this->setCurrentVersion($to); - return $to; } + + $this->_throwErrorsException(); + } + + if ($dryRun) { + $this->_connection->rollback(); + + return !$this->hasErrors(); } - return false; + + Doctrine_TransactionHelper::commitIfInTransaction($this->_connection); + $this->setCurrentVersion($to); + + return true; } /** @@ -437,8 +434,9 @@ public function getMigrationClass($num) /** * Throw an exception with all the errors trigged during the migration * - * @return void - * @throws Doctrine_Migration_Exception $e + * @throws Doctrine_Migration_Exception + * + * @return never */ protected function _throwErrorsException() { @@ -559,4 +557,4 @@ protected function _createMigrationTable() return false; } } -} \ No newline at end of file +} diff --git a/lib/Doctrine/TransactionHelper.php b/lib/Doctrine/TransactionHelper.php new file mode 100644 index 000000000..c594a91ff --- /dev/null +++ b/lib/Doctrine/TransactionHelper.php @@ -0,0 +1,24 @@ + + */ +final class Doctrine_TransactionHelper +{ + /** + * Execute a commit on the given connection, only if a transaction already started. + */ + public static function commitIfInTransaction(Doctrine_Connection $connection): void + { + $handler = $connection->getDbh(); + + // Attempt to commit while no transaction is running results in exception since PHP 8 + pdo_mysql combination + if ($handler instanceof PDO && !$handler->inTransaction()) { + return; + } + + $connection->commit(); + } +}