Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.3.x' into 3.0.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	lib/Doctrine/Migrations/Migrator.php
#	lib/Doctrine/Migrations/Tracking/TableUpdater.php
#	lib/Doctrine/Migrations/Version/Executor.php
#	lib/Doctrine/Migrations/Version/Factory.php
  • Loading branch information
ostrolucky committed Mar 14, 2021
2 parents f424cc7 + c4c46f7 commit 0e37888
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/en/reference/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ All or Nothing Transaction

.. note::

This is only works if your database supports transactions for DDL statements.
This only works if your database supports transactions for DDL statements.

When using the ``all_or_nothing`` option, multiple migrations ran at the same time will be wrapped in a single
transaction. If one migration fails, all migrations will be rolled back
Expand Down
10 changes: 10 additions & 0 deletions docs/en/reference/migration-classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ Override this method if you want to disable transactions in a migration. It defa
return false;
}
.. note::

Some database platforms like MySQL or Oracle do not support DDL
statements in transactions and may or may not implicitly commit the
transaction opened by this library as soon as they encounter such a
statement, and before running it. Make sure to read the manual of
your database platform to know what is actually happening.
``isTransactional()`` does not guarantee that statements are wrapped
in a single transaction.

getDescription
~~~~~~~~~~~~~~

Expand Down
3 changes: 2 additions & 1 deletion lib/Doctrine/Migrations/DbalMigrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Migrations\Metadata\MigrationPlanList;
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Tools\BytesFormatter;
use Doctrine\Migrations\Tools\TransactionHelper;
use Doctrine\Migrations\Version\Executor;
use Psr\Log\LoggerInterface;
use Symfony\Component\Stopwatch\Stopwatch;
Expand Down Expand Up @@ -82,7 +83,7 @@ private function executeMigrations(
}

if ($allOrNothing) {
$this->connection->commit();
TransactionHelper::commitIfInTransaction($this->connection);
}

return $sql;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\Common\EventSubscriber;
use Doctrine\Migrations\Event\MigrationsEventArgs;
use Doctrine\Migrations\Events;
use Doctrine\Migrations\Tools\TransactionHelper;

/**
* Listens for `onMigrationsMigrated` and, if the connection has autocommit
Expand All @@ -25,7 +26,7 @@ public function onMigrationsMigrated(MigrationsEventArgs $args): void
return;
}

$conn->commit();
TransactionHelper::commitIfInTransaction($conn);
}

/** {@inheritdoc} */
Expand Down
26 changes: 26 additions & 0 deletions lib/Doctrine/Migrations/Tools/TransactionHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Tools;

use Doctrine\DBAL\Connection;
use PDO;

/**
* @internal
*/
final class TransactionHelper
{
public static function commitIfInTransaction(Connection $connection): void
{
$wrappedConnection = $connection->getWrappedConnection();

// Attempt to commit while no transaction is running results in exception since PHP 8 + pdo_mysql combination
if ($wrappedConnection instanceof PDO && ! $wrappedConnection->inTransaction()) {
return;
}

$connection->commit();
}
}
4 changes: 2 additions & 2 deletions lib/Doctrine/Migrations/Version/DbalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Doctrine\Migrations\Provider\SchemaDiffProvider;
use Doctrine\Migrations\Query\Query;
use Doctrine\Migrations\Tools\BytesFormatter;
use Doctrine\Migrations\Tools\TransactionHelper;
use Psr\Log\LoggerInterface;
use Symfony\Component\Stopwatch\Stopwatch;
use Throwable;
Expand Down Expand Up @@ -211,8 +212,7 @@ private function executeMigration(
}

if ($migration->isTransactional()) {
//commit only if running in transactional mode
$this->connection->commit();
TransactionHelper::commitIfInTransaction($this->connection);
}

$plan->markAsExecuted($result);
Expand Down

0 comments on commit 0e37888

Please sign in to comment.