From 01501ba1c703dfff8e81ae9123b8d84086432e7a Mon Sep 17 00:00:00 2001 From: Rias Date: Wed, 5 Aug 2026 14:04:48 +0200 Subject: [PATCH 1/2] Fix Yii-style migration resolution --- yii2-adapter/src/Database/Migrator.php | 36 ++++++++++++++++ yii2-adapter/src/Yii2ServiceProvider.php | 10 +++++ .../Legacy/Database/MigratorTest.php | 43 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 yii2-adapter/src/Database/Migrator.php create mode 100644 yii2-adapter/tests-laravel/Legacy/Database/MigratorTest.php diff --git a/yii2-adapter/src/Database/Migrator.php b/yii2-adapter/src/Database/Migrator.php new file mode 100644 index 00000000000..5bc1fec0aa6 --- /dev/null +++ b/yii2-adapter/src/Database/Migrator.php @@ -0,0 +1,36 @@ +getMigrationName($path); + $realPath = realpath($path); + + foreach (array_reverse(get_declared_classes()) as $class) { + if ($class !== $migrationName && !str_ends_with($class, "\\$migrationName")) { + continue; + } + + if ($realPath !== new ReflectionClass($class)->getFileName()) { + continue; + } + + return is_a($class, Migration::class, true) + ? app()->make($class) + : new MigrationWrapper($class); + } + + return parent::resolvePath($path); + } +} diff --git a/yii2-adapter/src/Yii2ServiceProvider.php b/yii2-adapter/src/Yii2ServiceProvider.php index e2309a1df73..586b33e7b79 100644 --- a/yii2-adapter/src/Yii2ServiceProvider.php +++ b/yii2-adapter/src/Yii2ServiceProvider.php @@ -13,6 +13,8 @@ use CraftCms\Cms\Cms; use CraftCms\Cms\Cp\Settings; use CraftCms\Cms\Database\LaravelMigrations; +use CraftCms\Cms\Database\MigrationRepository; +use CraftCms\Cms\Database\Migrator as CoreMigrator; use CraftCms\Cms\Database\Table; use CraftCms\Cms\Field\Events\FieldCachesInvalidated; use CraftCms\Cms\Gql\Gql; @@ -42,6 +44,7 @@ use CraftCms\Yii2Adapter\Console\MigrateSessionsTableCommand; use CraftCms\Yii2Adapter\Console\RepairCategoryGroupStructureCommand; use CraftCms\Yii2Adapter\Cp\LegacySettings; +use CraftCms\Yii2Adapter\Database\Migrator; use CraftCms\Yii2Adapter\Filesystem\FilesystemCompatibility; use CraftCms\Yii2Adapter\Gql\LegacyGql; use CraftCms\Yii2Adapter\Gql\LegacyGqlArguments; @@ -62,6 +65,7 @@ use CraftCms\Yii2Adapter\Utility\LegacyUtilityTypes; use Illuminate\Contracts\Debug\ExceptionHandler; use Illuminate\Contracts\Http\Kernel as HttpKernel; +use Illuminate\Database\Migrations\MigrationRepositoryInterface; use Illuminate\Foundation\Exceptions\Handler; use Illuminate\Routing\Router; use Illuminate\Support\Facades\Artisan; @@ -84,6 +88,12 @@ class Yii2ServiceProvider extends ServiceProvider #[Override] public function register(): void { + $this->app->bind(CoreMigrator::class, Migrator::class); + $this->app + ->when(Migrator::class) + ->needs(MigrationRepositoryInterface::class) + ->give(fn() => $this->app->make(MigrationRepository::class, ['table' => Table::MIGRATIONS])); + new ClassAliases()->register(); new MultiEnvironmentConfigCompatibility()->register($this->app); diff --git a/yii2-adapter/tests-laravel/Legacy/Database/MigratorTest.php b/yii2-adapter/tests-laravel/Legacy/Database/MigratorTest.php new file mode 100644 index 00000000000..0cb29d7e9df --- /dev/null +++ b/yii2-adapter/tests-laravel/Legacy/Database/MigratorTest.php @@ -0,0 +1,43 @@ +makePartial(); + $filesystem->shouldReceive('getRequire')->andReturn(new stdClass()); + + $migrator = app(CoreMigrator::class); + new ReflectionProperty(IlluminateMigrator::class, 'files')->setValue($migrator, $filesystem); + + $migrator->requireFiles([$path]); + + $resolvePath = new ReflectionMethod(Migrator::class, 'resolvePath'); + $migration = $resolvePath->invoke($migrator, $path); + + $filesystem->shouldNotHaveReceived('getRequire'); + + expect($migrator)->toBeInstanceOf(Migrator::class); + expect($migrator->getRepository())->toBeInstanceOf(MigrationRepository::class); + expect($migration)->toBeInstanceOf(MigrationWrapper::class); +}); From bb0c1f617a7d474f5057f111f76f401765859100 Mon Sep 17 00:00:00 2001 From: Rias Date: Wed, 5 Aug 2026 14:32:30 +0200 Subject: [PATCH 2/2] Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99e3f807e1d..aafcab25922 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Craft CMS 6 +## Unreleased + +- Fixed a bug where Yii-style migrations could be required twice. ([#19376](https://github.com/craftcms/cms/pull/19376)) + ## 6.0.0-alpha.15 - 2026-08-04 - Added support for Markdown-based custom Dashboard widgets in the application's `resources/widgets/` directory. ([#19319](https://github.com/craftcms/cms/pull/19319))