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

[1.x] Adds booting support #4

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,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 @@ -161,6 +159,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);
}
}
}
Loading