Skip to content

Commit 1c2b3f2

Browse files
committed
Use native MigrationFailedException instead of PDOMigrationFailed
1 parent 7a3e909 commit 1c2b3f2

File tree

4 files changed

+30
-46
lines changed

4 files changed

+30
-46
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FiveLab Migrator package
5+
*
6+
* (c) FiveLab
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code
10+
*/
11+
12+
declare(strict_types = 1);
13+
14+
namespace FiveLab\Component\Migrator\Exception;
15+
16+
class MigrationFailedException extends \Exception
17+
{
18+
}

src/Exception/PdoMigrationFailedException.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Migration/AbstractPdoMigration.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace FiveLab\Component\Migrator\Migration;
1515

16-
use FiveLab\Component\Migrator\Exception\PdoMigrationFailedException;
16+
use FiveLab\Component\Migrator\Exception\MigrationFailedException;
1717

1818
abstract readonly class AbstractPdoMigration extends AbstractMigration
1919
{
@@ -65,7 +65,7 @@ final protected function addSql(string $sql, array $parameters = []): void
6565
*
6666
* @param array{"0": string, 1: array<string|int, string|int|float>} $entry
6767
*
68-
* @throws PdoMigrationFailedException
68+
* @throws MigrationFailedException
6969
*/
7070
private function executeEntry(array $entry): void
7171
{
@@ -74,7 +74,14 @@ private function executeEntry(array $entry): void
7474
try {
7575
$stmt->execute($entry[1]);
7676
} catch (\Throwable $error) {
77-
throw new PdoMigrationFailedException($entry[0], $entry[1], $error);
77+
$message = \sprintf(
78+
'Migration failed - "%s", parameters: %s with message: %s.',
79+
$entry[0],
80+
\json_encode($entry[1], \JSON_THROW_ON_ERROR),
81+
\rtrim($error->getMessage(), '.')
82+
);
83+
84+
throw new MigrationFailedException($message, 0, $error);
7885
}
7986
}
8087
}

tests/Functional/Migrator/MigratorMySqlTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace FiveLab\Component\Migrator\Tests\Functional\Migrator;
1515

16-
use FiveLab\Component\Migrator\Exception\PdoMigrationFailedException;
16+
use FiveLab\Component\Migrator\Exception\MigrationFailedException;
1717
use FiveLab\Component\Migrator\Locator\FilesystemMigrationsLocator;
1818
use FiveLab\Component\Migrator\MigrateDirection;
1919
use FiveLab\Component\Migrator\Migrator;
@@ -132,7 +132,7 @@ public function shouldSuccessWrapPdoException(): void
132132
{
133133
$migrator = $this->createMigrator(__DIR__.'/../../Migrations/DataSet03', 'Database');
134134

135-
$this->expectException(PdoMigrationFailedException::class);
135+
$this->expectException(MigrationFailedException::class);
136136
$this->expectExceptionMessage('Migration failed - "SELECT * FROM not_existing_table WHERE label = :foo", parameters: {"foo":"bar"} with message: SQLSTATE[42S02]: Base table or view not found:');
137137

138138
$migrator->migrate(MigrateDirection::Up, null);

0 commit comments

Comments
 (0)