kitloong / laravel-migrations-generator

Laravel Migrations Generator: Automatically generate your migrations from an existing database schema.
MIT License
2.44k stars 270 forks source link

Anonymous migration class and table exists check #141

Closed ccarlosm closed 1 year ago

ccarlosm commented 1 year ago

Hi!,

For some reason running dusk tests always told me that class XXX already existed and I discovered the previous person working in the project had used the feature for creating anonymous migration classes (which solves that problem):

return new class extends Migration {
....
};

I think it would be great if the new feature could be added at migrations creation with a flag for example.

Another interesting feature would be to add a flag for checking existing tables before creation since it is another problem I am encountering.

if (!Schema::hasTable('tblCategory'))
{
     Schema::create('tblCategory', function($table){
            //...
    }
}

Thanks!

kitloong commented 1 year ago

Hi @ccarlosm , thanks for the suggestion.

return new class extends Migration {
....
};

I will take a look into this. Mainly I have to consider fallback compatibility.

if (!Schema::hasTable('tblCategory'))
{
     Schema::create('tblCategory', function($table){
            //...
    }
}

For this, could you please elaborate more on how a hasTable check could help? Because, in normal circumstances, any migrated tables are logged in the migrations, migrated migrations will not execute twice. Mostly, I wish to know in which use case your migration gets executed twice.

ccarlosm commented 1 year ago

Yes, of course. I should I have explained more before.

If you migrate all tables with php artisan migrate:generate there might be some tables that are already migrated. In my case, Laravel sanctum has a migration in vendor (personal_access_tokens) that creates that conflict. I could have removed that table from the migration with php artisan migrate:generate --ignore="table3,table4,table5"`` but I did not know it until I ran them and found the conflict.

Thanks!

kitloong commented 1 year ago

Relates to #128