Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add method to MigrationsStarted/MigrationEnded events #40334

Merged
merged 3 commits into from
Jan 11, 2022

Conversation

kohenkatz
Copy link
Contributor

The MigrationStarted and MigrationEnded events that are fired for individual migrations have a method property that can have the values up or down to indicate whether this is a migration or a rollback.

However, the MigrationsStarted and MigrationsEnded (plural) which run at the beginning and end of a set of migrations do not have this property.

This PR adds the method property to those events as well.

The intended use-case of this change is to allow pre- and post-migration code to run better. For example, consider this code:

// Check if the proper PostgreSQL extensions are loaded.
// Throws an exception if an extension is missing or the PostgreSQL version is too old.
Event::listen(function (MigrationsStarted $event) {
    Artisan::call('project:ensure-extensions', ['--ansi' => true]);
    echo Artisan::output();
});

// Ensure initial required data is present in the database after migrations are complete.
// See https://twitter.com/taylorotwell/status/1387766514674192384 (thread)
// for the inspiration behind this code.
Event::listen(function (MigrationsEnded $event) {
    Artisan::call('project:ensure-db', ['--ansi' => true]);
    echo Artisan::output();
});

There are two problems with the above code when running migrate:rollback:

  1. (minor) It is unnecessary to run project:ensure-extensions because the database tables already exist so the requirements were clearly met.
  2. (major) project:ensure-db will crash because it is trying to write to database tables that no longer exist.

With this PR, the code above can be modified like this:

// Check if the proper PostgreSQL extensions are loaded.
// Throws an exception if an extension is missing or the PostgreSQL version is too old.
Event::listen(function (MigrationsStarted $event) {
    if ($event->method === 'up') {
        Artisan::call('project:ensure-extensions', ['--ansi' => true]);
        echo Artisan::output();
    }
});

// Ensure initial required data is present in the database after migrations are complete.
// See https://twitter.com/taylorotwell/status/1387766514674192384 (thread)
// for the inspiration behind this code.
Event::listen(function (MigrationsEnded $event) {
    if ($event->method === 'up') {
        Artisan::call('project:ensure-db', ['--ansi' => true]);
        echo Artisan::output();
    }
});

@taylorotwell taylorotwell merged commit d320012 into laravel:8.x Jan 11, 2022
@kohenkatz kohenkatz deleted the include-migrations-method branch January 11, 2022 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants