Skip to content

Commit

Permalink
Fix(migrations) Handle transaction on PDO and PHPv8
Browse files Browse the repository at this point in the history
  • Loading branch information
thePanz committed Jun 27, 2024
1 parent f47a790 commit c6e1147
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lib/Doctrine/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,7 @@ public function exportClasses(array $classes)
}
}

$connection->commit();
Doctrine_TransactionHelper::commitIfInTransaction($connection);
}
}

Expand Down
36 changes: 17 additions & 19 deletions lib/Doctrine/Migration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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()
{
Expand Down Expand Up @@ -559,4 +557,4 @@ protected function _createMigrationTable()
return false;
}
}
}
}
24 changes: 24 additions & 0 deletions lib/Doctrine/TransactionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* Helper for transactions handling.
*
* @author Kyle McGrogan <[email protected]>
*/
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();
}
}

0 comments on commit c6e1147

Please sign in to comment.