Skip to content

Commit 0c23c92

Browse files
committed
Add migration FQCN to migration failed exception.
1 parent 1c2b3f2 commit 0c23c92

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/Exception/MigrationFailedException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@
1515

1616
class MigrationFailedException extends \Exception
1717
{
18+
public function __construct(public readonly string $fqcn, string $message, int $code = 0, ?\Throwable $previous = null)
19+
{
20+
$message = \sprintf(
21+
'Migration failed (%s) - %s.',
22+
$this->fqcn,
23+
\rtrim($message, '.')
24+
);
25+
26+
parent::__construct($message, $code, $previous);
27+
}
1828
}

src/Migration/AbstractPdoMigration.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
abstract readonly class AbstractPdoMigration extends AbstractMigration
1919
{
2020
/**
21-
* @var \ArrayIterator<int, array{"0": "string", "1": array<string, mixed>}>
21+
* @var \ArrayIterator<int, array{"0": "string", "1": array<string, mixed>, "2": string}>
2222
*/
2323
private \ArrayIterator $entries;
2424

@@ -57,7 +57,7 @@ abstract protected function doDown(): void;
5757
*/
5858
final protected function addSql(string $sql, array $parameters = []): void
5959
{
60-
$this->entries->offsetSet(\count($this->entries), [$sql, $parameters]);
60+
$this->entries->offsetSet(\count($this->entries), [$sql, $parameters, \get_class($this)]);
6161
}
6262

6363
/**
@@ -75,13 +75,13 @@ private function executeEntry(array $entry): void
7575
$stmt->execute($entry[1]);
7676
} catch (\Throwable $error) {
7777
$message = \sprintf(
78-
'Migration failed - "%s", parameters: %s with message: %s.',
78+
'"%s", parameters: %s with message: %s.',
7979
$entry[0],
8080
\json_encode($entry[1], \JSON_THROW_ON_ERROR),
8181
\rtrim($error->getMessage(), '.')
8282
);
8383

84-
throw new MigrationFailedException($message, 0, $error);
84+
throw new MigrationFailedException($entry[2], $message, 0, $error);
8585
}
8686
}
8787
}

tests/Functional/Migrator/MigratorMySqlTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function shouldSuccessWrapPdoException(): void
133133
$migrator = $this->createMigrator(__DIR__.'/../../Migrations/DataSet03', 'Database');
134134

135135
$this->expectException(MigrationFailedException::class);
136-
$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:');
136+
$this->expectExceptionMessage('Migration failed (FiveLab\Component\Migrator\Tests\Migrations\DataSet03\Version01) - "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);
139139
}

0 commit comments

Comments
 (0)