bastinald / laravel-automatic-migrations

Automatic Laravel model migrations.
42 stars 11 forks source link

Migration order by date as attribute #13

Open shaedrich opened 8 months ago

shaedrich commented 8 months ago

It would be nice and more aligned with how the [Migration Order](../tree/master/readme.md#Migration Order) natively works in Laravel, to have an Attribute automatically created, containing the date of the migration creation:

[!NOTE]
Each migration filename contains a timestamp that allows Laravel to determine the order of the migrations

use Bastinald\LaravelAutomaticMigrations\Attributes\MigrationScaffoldedAt;

class MyModel extends Model
{
    #[MigrationScaffoldedAt('2024_01_10_123456')]
    public function migration(Blueprint $table)
    {
        $table->id();
        $table->string('name');
        $table->timestamp('created_at')->nullable();
        $table->timestamp('updated_at')->nullable();
    }
}

This would then be implemented something like this:

<?php

namespace Bastinald\LaravelAutomaticMigrations\Commands;

[…]
use Bastinald\LaravelAutomaticMigrations\Attributes\MigrationScaffoldedAt;
use ReflectionClass;

class MigrateAutoCommand extends Command
{
    […]

    private function handleAutomaticMigrations()
    {
        […]

        foreach ((new Finder)->in($path) as $model) {
            […]

            if (method_exists($model, 'migration')) {
                $object = app($model);
                $reflection = new ReflectionClass($model);
                $attribute  = $reflection->getAttributes(MigrationScaffoldedAt::class)[0];
                $scaffoldingDate = $attribute->getArguments()[0];

                $models->push([
                    'object' => $object,
                    'order' => $object->migrationOrder ?? $scaffoldingDate ?? 0
                ]);
            }
        }

        foreach ($models->sortBy('order') as $model) {
            $this->migrate($model['object']);
        }
    }