Skip to content

Commit

Permalink
Merge pull request #4 from Laragear/feat/boot
Browse files Browse the repository at this point in the history
[1.x] Adds booting support
  • Loading branch information
DarkGhostHunter authored Mar 15, 2024
2 parents 66b0c17 + 9270f8d commit d8f7aa0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions src/CustomizableMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
//
}

/**
Expand Down
13 changes: 11 additions & 2 deletions tests/CustomizableMigrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ public function creates_columns(): void
return true;
});



TestModel::migration()->up();
}

Expand Down Expand 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);
}
}
26 changes: 26 additions & 0 deletions tests/Fixtures/TestMigrationWithBoot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Tests\Fixtures;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Laragear\MetaModel\CustomizableMigration;

class TestMigrationWithBoot extends CustomizableMigration
{
public static bool $callMethod = false;

protected function boot(): void
{
Schema::create('test', fn () => true);
}

public function create(Blueprint $table): void
{
$table->createCall();

if (static::$callMethod) {
$this->addColumns($table);
}
}
}

0 comments on commit d8f7aa0

Please sign in to comment.