Skip to content

Commit c6e1147

Browse files
committed
Fix(migrations) Handle transaction on PDO and PHPv8
1 parent f47a790 commit c6e1147

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

lib/Doctrine/Export.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ public function exportClasses(array $classes)
12211221
}
12221222
}
12231223

1224-
$connection->commit();
1224+
Doctrine_TransactionHelper::commitIfInTransaction($connection);
12251225
}
12261226
}
12271227

lib/Doctrine/Migration.php

+17-19
Original file line numberDiff line numberDiff line change
@@ -335,24 +335,21 @@ public function migrate($to = null, $dryRun = false)
335335

336336
if ($dryRun) {
337337
return false;
338-
} else {
339-
$this->_throwErrorsException();
340-
}
341-
} else {
342-
if ($dryRun) {
343-
$this->_connection->rollback();
344-
if ($this->hasErrors()) {
345-
return false;
346-
} else {
347-
return $to;
348-
}
349-
} else {
350-
$this->_connection->commit();
351-
$this->setCurrentVersion($to);
352-
return $to;
353338
}
339+
340+
$this->_throwErrorsException();
341+
}
342+
343+
if ($dryRun) {
344+
$this->_connection->rollback();
345+
346+
return !$this->hasErrors();
354347
}
355-
return false;
348+
349+
Doctrine_TransactionHelper::commitIfInTransaction($this->_connection);
350+
$this->setCurrentVersion($to);
351+
352+
return true;
356353
}
357354

358355
/**
@@ -437,8 +434,9 @@ public function getMigrationClass($num)
437434
/**
438435
* Throw an exception with all the errors trigged during the migration
439436
*
440-
* @return void
441-
* @throws Doctrine_Migration_Exception $e
437+
* @throws Doctrine_Migration_Exception
438+
*
439+
* @return never
442440
*/
443441
protected function _throwErrorsException()
444442
{
@@ -559,4 +557,4 @@ protected function _createMigrationTable()
559557
return false;
560558
}
561559
}
562-
}
560+
}

lib/Doctrine/TransactionHelper.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* Helper for transactions handling.
5+
*
6+
* @author Kyle McGrogan <[email protected]>
7+
*/
8+
final class Doctrine_TransactionHelper
9+
{
10+
/**
11+
* Execute a commit on the given connection, only if a transaction already started.
12+
*/
13+
public static function commitIfInTransaction(Doctrine_Connection $connection): void
14+
{
15+
$handler = $connection->getDbh();
16+
17+
// Attempt to commit while no transaction is running results in exception since PHP 8 + pdo_mysql combination
18+
if ($handler instanceof PDO && !$handler->inTransaction()) {
19+
return;
20+
}
21+
22+
$connection->commit();
23+
}
24+
}

0 commit comments

Comments
 (0)