Skip to content

Conversation

@nunomaduro
Copy link
Member

@nunomaduro nunomaduro commented Dec 5, 2023

This minimal pull request proposes the addition of a ServiceProvider::publishesMigrations() method, which can be used in favor of ServiceProvider::publishes() for migrations. It will ensure that any published migration has an "up-to-date" date in its name.

To get started, packages may opt in to this new method simply by replacing publishes with publishesMigrations:

        $this->publishes([
            __DIR__.'/../config/pennant.php' => config_path('pennant.php'),
        ], 'pennant-config');

-       $this->publishes([
+       $this->publishesMigrations([
            __DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
        ], 'pennant-migrations');

If a package wants to support multiple Laravel versions, you may use the method_exists function like so:

$migrations = [
    __DIR__.'/../database/migrations' => $this->app->databasePath('migrations'),
];

method_exists($this, 'publishesMigrations')
    ? $this->publishesMigrations($migrations, 'pennant-migrations')
    ? $this->publishes($migrations, 'pennant-migrations')

The result will be that, instead of publishing a migration with an old date, Laravel will use a new date instead:

-2015_11_01_000001_create_features_table.php
+2023_12_05_132339_create_features_table.php

Note, when a package contains multiple migrations, for example like Jetstream with create_user and create_team_user, the package developer needs to add a number after the date. Here is an example, before and after:

-2015_11_01_000001_create_features_table.php
+2023_12_05_132339_create_features_table.php

At the moment, and intentionally, because I wanted to ensure we discuss it before coding it, this pull request does not detect if the migration already exists. So, if you run the command php artisan vendor:publish --provider="Laravel\Pennant\PennantServiceProvider" twice, you will end up with two similar migrations in your database/migrations directory:

+2023_12_05_132339_create_features_table.php
+2023_12_05_132400_create_features_table.php // duplicated here...

@driesvints driesvints changed the title [master] Adds ServiceProvider::publishesMigrations() method [11.x] Adds ServiceProvider::publishesMigrations() method Dec 5, 2023
@nunomaduro nunomaduro marked this pull request as ready for review December 5, 2023 17:47
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.

4 participants