Skip to content

Commit e485847

Browse files
authored
improve error message if a migration contains the old base migration class (#1059)
1 parent 4e108a3 commit e485847

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

src/Migration/Manager.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,8 @@ function ($phpFile) {
975975

976976
$io->verbose("Loading class <info>$class</info> from <info>$filePath</info>.");
977977

978-
// load the migration file
978+
$this->checkMigrationClass($filePath);
979+
979980
$orig_display_errors_setting = ini_get('display_errors');
980981
ini_set('display_errors', 'On');
981982

@@ -1025,6 +1026,32 @@ function ($phpFile) {
10251026
return (array)$this->migrations;
10261027
}
10271028

1029+
/**
1030+
* Prevent fatal errors when loading legacy migration files that still reference old classes
1031+
*
1032+
* @param string $filePath Migration file path
1033+
* @return void
1034+
*/
1035+
protected function checkMigrationClass(string $filePath): void
1036+
{
1037+
$contents = file_get_contents($filePath);
1038+
if ($contents === false) {
1039+
return;
1040+
}
1041+
1042+
$usesLegacyAbstractMigration =
1043+
str_contains($contents, 'use Migrations\AbstractMigration;') ||
1044+
str_contains($contents, 'extends AbstractMigration') ||
1045+
str_contains($contents, 'extends \Migrations\AbstractMigration');
1046+
1047+
if ($usesLegacyAbstractMigration) {
1048+
throw new RuntimeException(sprintf(
1049+
'Migration file `%s` uses the legacy `Migrations\\AbstractMigration` class, which is not available in this version. Update the migration to extend `Migrations\\BaseMigration`.',
1050+
$filePath,
1051+
));
1052+
}
1053+
}
1054+
10281055
/**
10291056
* Returns a list of migration files found in the provided migration paths.
10301057
*

tests/TestCase/Migration/ManagerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,18 @@ public function testGetMigrationsWithInvalidMigrationClassName()
639639
$manager->getMigrations();
640640
}
641641

642+
public function testGetMigrationsWithLegacyAbstractMigrationClass(): void
643+
{
644+
$config = new Config(['paths' => ['migrations' => ROOT . '/config/LegacyAbstractMigration']]);
645+
$manager = new Manager($config, $this->io);
646+
647+
$this->expectException(RuntimeException::class);
648+
$this->expectExceptionMessageMatches('/uses the legacy `Migrations\\\\AbstractMigration` class/');
649+
$this->expectExceptionMessageMatches('/20260327000000_LegacyAbstractMigration\\.php/');
650+
651+
$manager->getMigrations();
652+
}
653+
642654
public function testGetMigrationsWithAnonymousClass()
643655
{
644656
$config = new Config(['paths' => ['migrations' => ROOT . '/config/AnonymousMigrations']]);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use Migrations\AbstractMigration;
5+
6+
class LegacyAbstractMigration extends AbstractMigration
7+
{
8+
public function change(): void
9+
{
10+
}
11+
}

0 commit comments

Comments
 (0)