Illuminate\Database\Events\MigrationEnded. In Laravel 13.x, the time spent in the up or down method of a single successful migration is passed as duration.
What the event contains
MigrationEnded extends MigrationEvent and exposes the migration and the result of the run.
In a normal migration run,
duration is a float rounded to two decimal places. It can be null, for example when you create the event yourself, so check the value before your monitoring code uses it.
Logging the duration
Listen for the event in your application’s service provider and pass the data to the log as structured context.name is available, also logging migration::class makes it easier to identify the migration when it runs through a different path, such as an anonymous class. duration_ms is already in milliseconds, so you can send it to logs and metrics without converting it to another unit.
Event order
Laravel dispatchesMigrationEnded only when up or down succeeds.
A failed migration doesn’t dispatch MigrationEnded. Put the code that records the cause of a failure and its exception in a separate MigrationFailed listener, as described in Handling migration failures with the MigrationFailed event.
What is measured
The framework starts timing withmicrotime(true) right before it calls the migration method and stops right after up or down returns. The measurement therefore includes:
- The PHP code inside the migration method
- The database queries that method runs
- Time spent waiting on the database while those queries run
MigrationStarted listeners, MigrationEnded listeners, or the processing time of the whole migration batch. When schema transactions are used, the timing happens inside the transaction callback, so treat it separately from surrounding work such as starting and committing the transaction.
This value doesn’t represent the time of the whole php artisan migrate command or of a batch containing several migrations. Use it to compare where time is spent in individual up / down methods.
Alerting on migrations over a threshold
Logging a warning only for migrations that exceed a certain time lets you detect slow changes without adding noise to your normal logs.MigrationFailed.
Checking the duration in tests
WithEvent::fake and Event::assertDispatched, you can check the shape of duration without running the real event listeners.
down durations as well, run migrate:rollback and check for events with method === 'down' in the same way. Durations depend on the environment and the state of the database, so test the type and that the value is non-negative rather than a specific number of milliseconds.
Summary
durationonMigrationEndedis the time, in milliseconds, thatup/downof a single successful migration took.- Send durations to logs and metrics to detect slow migrations.
- Event listeners and the whole batch aren’t included, so keep this separate from measuring the performance of the whole command.
- Split monitoring responsibilities:
MigrationFailedfor failures,MigrationEndedfor slowness in successful runs.