diff --git a/README.md b/README.md index 54587a0..0464f0f 100644 --- a/README.md +++ b/README.md @@ -165,10 +165,8 @@ use Illuminate\Database\Schema\Blueprint; use Laragear\MetaModel\CustomizableMigration; use MyVendor\MyPackage\Models\Car; -abstract class CarsMigration extends CustomizableMigration +class CarsMigration extends CustomizableMigration { - protected static $model = Car::class; - protected function create(Blueprint $table) { $table->id(); @@ -213,6 +211,43 @@ use Illuminate\Database\Schema\Blueprint; return Car::migration(); ``` +### Booting + +You can run custom logic when the migration is instanced using the `boot()` method. + +```php +namespace MyVendor\MyPackage\Migrations; + +use Illuminate\Database\Schema\Blueprint; +use Laragear\MetaModel\CustomizableMigration; +use MyVendor\MyPackage\Models\Car; + +class CarsMigration extends CustomizableMigration +{ + protected function boot() : void + { + if (app()->isUnitTesting()) { + Car::$useConnection = env('DB_CONNECTION'); + } + } + + protected function create(Blueprint $table) + { + $table->id(); + + $table->string('manufacturer'); + $table->string('model'); + $table->tinyInteger('year'); + + $table->timestamps(); + } +} +``` + +> [!CAUTION] +> +> The `boot()` method runs every time the migration is instanced. Ensure the method effects are idempotent when required. + ### Adding Custom Columns You may want to let the end-developer to add additional columns to the migration. For that, just call `addColumns()` anywhere inside the `create()` method, ensuring you pass the `Blueprint` instance. A great place to call this is just before the `timestamps()` or after the primary key. diff --git a/src/CustomizableMigration.php b/src/CustomizableMigration.php index 078371b..84bc9d0 100644 --- a/src/CustomizableMigration.php +++ b/src/CustomizableMigration.php @@ -44,6 +44,18 @@ public function __construct( ) { $this->table = (new $model)->getTable(); + + $this->boot(); + } + + /** + * Run additional logic when the migration is instanced. + * + * @return void + */ + protected function boot(): void + { + // } /** diff --git a/tests/CustomizableMigrationTest.php b/tests/CustomizableMigrationTest.php index 4b10281..8528a30 100644 --- a/tests/CustomizableMigrationTest.php +++ b/tests/CustomizableMigrationTest.php @@ -57,8 +57,6 @@ public function creates_columns(): void return true; }); - - TestModel::migration()->up(); } @@ -251,4 +249,15 @@ public function calls_before_down(): void TestModel::migration()->beforeDown(fn($table) => $table->afterDownCall())->down(); } + + #[Test] + public function calls_boot_method(): void + { + $this->expectNotToPerformAssertions(); + + $this->container->instance('db.schema', $schema = m::mock(SchemaBuilder::class)); + $schema->expects('create')->with('test', m::type(Closure::class))->once(); + + new Fixtures\TestMigrationWithBoot(TestModel::class); + } } diff --git a/tests/Fixtures/TestMigrationWithBoot.php b/tests/Fixtures/TestMigrationWithBoot.php new file mode 100644 index 0000000..1a86835 --- /dev/null +++ b/tests/Fixtures/TestMigrationWithBoot.php @@ -0,0 +1,26 @@ + true); + } + + public function create(Blueprint $table): void + { + $table->createCall(); + + if (static::$callMethod) { + $this->addColumns($table); + } + } +}