Skip to content
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
14 changes: 14 additions & 0 deletions src/Exception/CatastrophicMigrationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use Throwable;

interface CatastrophicMigrationException
{

public function getAdditionalException(): Throwable;
public function setAdditionalException(Throwable $additionalException): static;
}
24 changes: 24 additions & 0 deletions src/Exception/CatastrophicMigrationExceptionTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use Throwable;

trait CatastrophicMigrationExceptionTrait
{
protected Throwable $additionalException;

public function getAdditionalException(): Throwable

Check warning on line 13 in src/Exception/CatastrophicMigrationExceptionTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/CatastrophicMigrationExceptionTrait.php#L13

Added line #L13 was not covered by tests
{
return $this->additionalException;

Check warning on line 15 in src/Exception/CatastrophicMigrationExceptionTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/CatastrophicMigrationExceptionTrait.php#L15

Added line #L15 was not covered by tests
}

public function setAdditionalException(Throwable $additionalException): static

Check warning on line 18 in src/Exception/CatastrophicMigrationExceptionTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/CatastrophicMigrationExceptionTrait.php#L18

Added line #L18 was not covered by tests
{
$this->additionalException = $additionalException;

Check warning on line 20 in src/Exception/CatastrophicMigrationExceptionTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/CatastrophicMigrationExceptionTrait.php#L20

Added line #L20 was not covered by tests

return $this;

Check warning on line 22 in src/Exception/CatastrophicMigrationExceptionTrait.php

View check run for this annotation

Codecov / codecov/patch

src/Exception/CatastrophicMigrationExceptionTrait.php#L22

Added line #L22 was not covered by tests
}
}
12 changes: 12 additions & 0 deletions src/Exception/TransactionRollbackException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\Migrations\Exception;

use RuntimeException;

class TransactionRollbackException extends RuntimeException implements CatastrophicMigrationException
{
use CatastrophicMigrationExceptionTrait;
}
46 changes: 36 additions & 10 deletions src/Version/DbalExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Doctrine\Migrations\AbstractMigration;
use Doctrine\Migrations\EventDispatcher;
use Doctrine\Migrations\Events;
use Doctrine\Migrations\Exception\CatastrophicMigrationException;
use Doctrine\Migrations\Exception\TransactionRollbackException;
use Doctrine\Migrations\Exception\SkipMigration;
use Doctrine\Migrations\Metadata\MigrationPlan;
use Doctrine\Migrations\Metadata\Storage\MetadataStorage;
Expand Down Expand Up @@ -228,8 +230,19 @@
{
$migration = $plan->getMigration();
if ($migration->isTransactional()) {
//only rollback transaction if in transactional mode
TransactionHelper::rollbackIfInTransaction($this->connection);
try {
//only rollback transaction if in transactional mode
TransactionHelper::rollbackIfInTransaction($this->connection);
} catch (\Exception $transactionRollbackException) {
$this->logResult(
(new TransactionRollbackException(
'The migration transaction could not be rolled back after an encountered exception during its execution.',
previous: $transactionRollbackException
))->setAdditionalException($e),
$result,
$plan
);

Check warning on line 244 in src/Version/DbalExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Version/DbalExecutor.php#L236-L244

Added lines #L236 - L244 were not covered by tests
}
}

$plan->markAsExecuted($result);
Expand All @@ -254,14 +267,27 @@
],
);
} elseif ($result->hasError()) {
$this->logger->error(
'Migration {version} failed during {state}. Error: "{error}"',
[
'version' => (string) $plan->getVersion(),
'error' => $e->getMessage(),
'state' => $this->getExecutionStateAsString($result->getState()),
],
);
if ($e instanceof CatastrophicMigrationException) {
$this->logger->error(
'Migration {version} failed catastrophically at {state} during the error handling routine.'
. ' Error: "{error}". Additional error: {additionalError}',
[
'version' => (string)$plan->getVersion(),
'error' => $e->getMessage(),
'additionalError' => $e->getAdditionalException()->getMessage(),
'state' => $this->getExecutionStateAsString($result->getState()),
],
);

Check warning on line 280 in src/Version/DbalExecutor.php

View check run for this annotation

Codecov / codecov/patch

src/Version/DbalExecutor.php#L271-L280

Added lines #L271 - L280 were not covered by tests
} else {
$this->logger->error(
'Migration {version} failed during {state}. Error: "{error}"',
[
'version' => (string)$plan->getVersion(),
'error' => $e->getMessage(),
'state' => $this->getExecutionStateAsString($result->getState()),
],
);
}
}
}

Expand Down
Loading