Load migration classes only when they are executed - #1114
Open
nicosp wants to merge 1 commit into
Open
Conversation
Checking migration status (status command, PendingMigrationsMiddleware, the test suite Migrator) previously required every migration class to be loaded and instantiated. Migration versions and names are now derived from the file names, and a migration class is only loaded when the migration is about to be executed, rolled back or have its breakpoint changed. Adds Manager::getMigrationVersions() and Manager::getMigration(). getMigrations() still loads all migrations for backwards compatibility. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EeBHdY3PAMKrNXQnogCUsQ
Member
|
Did you measure the diff? The downside of this is getting no real feedback on invalid/broken ones until you actually execute I assume. |
Contributor
Author
The motivation was to speed up our test bootstrap. Running a single test spent around 45s in the migrations. With this it dropped to around 4s. We have around 900 migrations :) Our application is not the common case for sure. |
nicosp
marked this pull request as ready for review
September 14, 2026 11:04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checking whether migrations are up to date currently requires loading and instantiating every migration class. This happens in
migrations status,PendingMigrationsMiddlewareand the test suiteMigrator, and it gets slower as projects accumulate migrations.This change reads migration versions and names from the file names and loads a migration class only when it is about to run, be rolled back or have its breakpoint changed.
Changes
Manager::getMigrationVersions()returns the sorted versions without loading any classes. It still checks for duplicate versions and names, since that only needs the file names.Manager::getMigration(int $version)loads a single migration and caches it.printStatus(),migrateToDateTime(),getVersionsToMark()andcleanupMissingMigrations()work from versions only.migrate()androllback()load only the migrations they execute.markBreakpoint()loads only the migration being marked.PendingMigrationsMiddlewareusesgetMigrationVersions().getMigrations()is kept for backwards compatibility and still loads all migrations. Migrations passed tosetMigrations()still take priority.Behaviour notes
printStatus()takes the migration name from the file name. For class-based migrations this is the same asgetName(). For anonymous-class migrations it shows the name from the file name instead of PHP's generatedclass@anonymous…name.AbstractMigrationbase class or missing its class, no longer breaks status checks or the middleware. The error is raised when that migration is executed.Tests
Added tests using a fixture that throws when loaded. They check that
getMigrationVersions(),printStatus()and an up-to-datemigrate()do not load migration classes. The full suite passes locally on SQLite, MySQL and Postgres, and phpstan and phpcs are clean.🤖 Generated with Claude Code
https://claude.ai/code/session_01EeBHdY3PAMKrNXQnogCUsQ