Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(migrations) Handle transaction within migrations on MySQL and php v8 #139

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading