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

Migration order by date as attribute #13

Open
shaedrich opened this issue Jan 10, 2024 · 0 comments
Open

Migration order by date as attribute #13

shaedrich opened this issue Jan 10, 2024 · 0 comments

Comments

@shaedrich
Copy link

shaedrich commented Jan 10, 2024

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']);
        }
    }
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

No branches or pull requests

1 participant