“Combined” migrations – even after many CRUD changes
Founder of QuickAdminPanel
One of the new features we are presenting for Agency plan customers this week is ability to have database migrations “clean”. Up until now, we were creating a new migration file for every change in CRUD – like field added/deleted/updated. Now you have another option.
Here’s an example how migrations look for a CRUD that was changed a few times:
As you can see, one migration file for each operation.
Migration 1. Creating a table
class Create1498418011ProjectsTable extends Migration { public function up() { if(! Schema::hasTable('projects')) { Schema::create('projects', function (Blueprint $table) { $table->increments('id'); $table->string('name')->nullable(); $table->timestamps(); $table->softDeletes(); $table->index(['deleted_at']); }); }
Migration 2. Adding a field
class Update1498474322ProjectsTable extends Migration { public function up() { Schema::table('projects', function (Blueprint $table) { $table->integer('author_id')->unsigned()->nullable(); $table->foreign('author_id', '48197_5950e75241d23')->references('id')->on('users')->onDelete('cascade'); });
Migration 3. Removing a field
class Update1498474367ProjectsTable extends Migration { public function up() { Schema::table('projects', function (Blueprint $table) { if(Schema::hasColumn('projects', 'author_id')) { $table->dropForeign('48197_5950e75241d23'); $table->dropIndex('48197_5950e75241d23'); $table->dropColumn('author_id'); } });
In other words, if you add, modify and then finally delete the same column – you will have separate migrations for it. It may become quite messy.
Now, Agency plan customers will have a new separate folder – after downloading the panel, you have not only regular database/migrations, but also a new database/migrations/combined, which will contain only the migrations needed from scratch – to achieve the final result.
database/migrations/combined/2017_06_25_221331_create_combined_1498418011_projects_table.php
class CreateCombined1498418011ProjectsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { if(! Schema::hasTable('projects')) { Schema::create('projects', function (Blueprint $table) { $table->increments('id'); $table->string('name')->nullable(); $table->timestamps(); $table->softDeletes(); $table->index(['deleted_at']); }); } }
So no mention of that field being added or removed.
From here you have a few options and flexibility how to run your migrations:
- You can continue with “old style” migrations and don’t pay attention to new structure
- You can use only “combined” migrations by copying them to the main folder
In other words, you have a separate folder of migrations which you may want to use.
We hope that this change will help you to keep your projects a little cleaner.