Skip to content

Migrations are not published in timestamp order #52074

@vorban

Description

@vorban

Laravel Version

11.x

PHP Version

8.2

Database Driver & Version

MySQL ^8.0

Description

We're developing a package that publishes several migrations. These follow the usual naming format Y-m-d_His_blablabla.php.

The timestamps allow for natural ordering of files; this is useful because some migrations depend on others, through foreign keys for example. So we have:

0001-01-01_000000_create_first_table.php
0001-01-01_000001_create_second_table.php
0001-01-01_000002_create_third_table_with_relationships.php

As documented here, our service provider does:

$this->publishesMigrations([
    __DIR__.'/../database/migrations' => database_path('migrations'),
], 'our-package');

Expected behaviour

When running artisan vendor:publish --tag=our-package, I expected to get:

2024-07-09_203029_create_first_table.php
2024-07-09_203030_create_second_table.php
2024-07-09_203031_create_third_table_with_relationships.php

Actual behaviour

When running artisan vendor:publish --tag=our-package, the resulting migrations are completely out of order:

2024-07-09_203029_create_third_table_with_relationships.php
2024-07-09_203030_create_first_table.php
2024-07-09_203031_create_second_table.php

The timestamps are correctly updated via this line from the VendorPublishCommand:

$path = $this->ensureMigrationNameIsUpToDate($from, $path);

To me the problem is that the iterator used in this line from VendorPublishCommand yields files in seemingly random order:

foreach ($manager->listContents('from://', true) as $file) {
    // ...
}

Ideas to fix the issue

A potential fix would be to sort the results, perhaps:

foreach (collect($manager->listContents('from://', true)->toArray())->sort() as $file) {
    // ...
}

PR

if this is working as intended, either I failed to find the relevant documentation, or it doesn't exist yet and detailing how to ensure migration order by manually publishing migrations should be added to it.

I'm willing to submit a PR with the proposed fix, or submit a PR to the docs repo with additional info on the matter.

I'd like some feedback on this before moving forward.

Steps To Reproduce

I've made a GitHub repository to showcase this issue: https://github.com/vorban/migration-disorder-bug-report.

The readme details how to install and reproduce the issue.

I've also included a screenshot of an example result I got.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions