Skip to content

Commit 2a0d817

Browse files
author
Kurt Friars
committed
Merge branch 'main' into 11.x
2 parents 78089c1 + e815d0f commit 2a0d817

10 files changed

+200
-14
lines changed

README.md

+39-1
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ Track and respond to database schema changes in your Laravel application through
2020
- [Usage](#usage)
2121
- [Available Events](#available-events)
2222
- [Event Properties](#event-properties)
23+
- [Repository](#repository)
2324
- [Contributing](#contributing)
2425
- [Credits](#credits)
2526
- [License](#license)
2627
- [Security Vulnerabilities](#security-vulnerabilities)
27-
- [About Plank](#about-plank)
28+
- [About Plank](#check-us-out)
2829

2930
## Installation
3031

@@ -114,11 +115,13 @@ The package provides four main events:
114115
### Event Properties
115116

116117
Each event includes basic connection information:
118+
117119
- `connection` - The name of the database connection
118120
- `databaseName` - The name of the database
119121
- `driverName` - The database driver being used
120122

121123
#### TableCreated Event
124+
122125
```php
123126
public readonly string $table;
124127
public readonly Collection $columns; // Added columns
@@ -127,6 +130,7 @@ public readonly Collection $foreignKeys; // Added foreign keys
127130
```
128131

129132
#### TableChanged Event
133+
130134
```php
131135
public readonly string $table;
132136
public readonly Collection $addedColumns;
@@ -141,16 +145,50 @@ public readonly Collection $droppedForeignKeys;
141145
```
142146

143147
#### TableDropped Event
148+
144149
```php
145150
public readonly string $table;
146151
```
147152

148153
#### TableRenamed Event
154+
149155
```php
150156
public readonly string $from;
151157
public readonly string $to;
152158
```
153159

160+
### Repository
161+
162+
The event repository collects the schema events that occur during the migrations which can be retrieved after the `MigrationsEnded` event is fired by the Migrator.
163+
164+
If your application wants to handle dispatching and flushing the events, you can set the `schema-events.listeners.finished` listener to `null` and listen to the `MigrationsEnded` event in your application.
165+
166+
The schema event repository can be controlled using the `SchemaEvents` facade.
167+
168+
#### get()
169+
170+
Retrieve all schema events that were fired during the course of the migrations.
171+
172+
#### created()
173+
174+
Retrieve all `TableCreated` events that were fired during the course of the migrations.
175+
176+
#### changed()
177+
178+
Retrieve all `TableChanged` events that were fired during the course of the migrations.
179+
180+
#### renamed()
181+
182+
Retrieve all `TableRenamed` events that were fired during the course of the migrations.
183+
184+
#### dropped()
185+
186+
Retrieve all `TableDropped` events that were fired during the course of the migrations.
187+
188+
#### flush()
189+
190+
Clear all schema events stored in the schema event repository.
191+
154192
## Contributing
155193

156194
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

config/schema-events.php

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
|--------------------------------------------------------------------------
1111
|
1212
| `ran`:
13-
| This listens to `MigrationStarted` events and bubbles up some more granular
14-
| events based on the changes taking place to the schema.
15-
|
16-
| If the `finished` listener is not configured, these events will bubble up
17-
| immediately.
13+
| This listens to `MigrationStarted` event which is dispatched before each
14+
| migration file is run. This listener is responsible for collecting the
15+
| schema events that will be occurring during the run.
1816
|
1917
| `finished`:
20-
| This listens for the completion of migrations being run and batches the more
21-
| granular events parsed during `ran` into one larger event to be processed.
18+
| This listens for the completion of migrations being run and emits all
19+
| of the collected schema events. If this is
20+
|
2221
*/
2322
'listeners' => [
2423
'ran' => MigrationRan::class,

src/Facades/SchemaEvents.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
use Plank\LaravelSchemaEvents\Repository\EventRepository;
1212

1313
/**
14-
* @method static void store(Collection<TableCreated|TableChanged|TableDropped|TableRenamed> $events)
1514
* @method static Collection<TableCreated|TableChanged|TableDropped|TableRenamed> get()
15+
* @method static Collection<TableCreated> created()
16+
* @method static Collection<TableChanged> changed()
17+
* @method static Collection<TableDropped> dropped()
18+
* @method static Collection<TableRenamed> renamed()
19+
* @method static void flush()
20+
* @method static void store(Collection<TableCreated|TableChanged|TableDropped|TableRenamed> $events)
1621
*/
1722
class SchemaEvents extends Facade
1823
{

src/Listeners/MigrationsFinished.php

+2
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ public function handle(MigrationsEnded $event)
1212
{
1313
SchemaEvents::get()
1414
->each(fn ($event) => Event::dispatch($event));
15+
16+
SchemaEvents::flush();
1517
}
1618
}

src/Repository/EventRepository.php

+48-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
namespace Plank\LaravelSchemaEvents\Repository;
44

55
use Illuminate\Support\Collection;
6+
use Plank\LaravelSchemaEvents\Events\TableChanged;
7+
use Plank\LaravelSchemaEvents\Events\TableCreated;
8+
use Plank\LaravelSchemaEvents\Events\TableDropped;
9+
use Plank\LaravelSchemaEvents\Events\TableRenamed;
610

711
class EventRepository
812
{
@@ -13,17 +17,56 @@ public function __construct()
1317
$this->events = new Collection;
1418
}
1519

16-
public function get()
20+
/**
21+
* @return Collection<TableCreated|TableChanged|TableDropped|TableRenamed>
22+
*/
23+
public function get(): Collection
1724
{
18-
$events = $this->events;
25+
return $this->events;
26+
}
1927

20-
$this->events = new Collection;
28+
/**
29+
* @return Collection<TableCreated>
30+
*/
31+
public function created(): Collection
32+
{
33+
return $this->events->filter(fn ($event) => $event instanceof TableCreated)->values();
34+
}
2135

22-
return $events;
36+
/**
37+
* @return Collection<TableChanged>
38+
*/
39+
public function changed(): Collection
40+
{
41+
return $this->events->filter(fn ($event) => $event instanceof TableChanged)->values();
42+
}
43+
44+
/**
45+
* @return Collection<TableDropped>
46+
*/
47+
public function dropped(): Collection
48+
{
49+
return $this->events->filter(fn ($event) => $event instanceof TableDropped)->values();
2350
}
2451

25-
public function store(Collection $events)
52+
/**
53+
* @return Collection<TableRenamed>
54+
*/
55+
public function renamed(): Collection
56+
{
57+
return $this->events->filter(fn ($event) => $event instanceof TableRenamed)->values();
58+
}
59+
60+
/**
61+
* @param Collection<TableCreated|TableChanged|TableDropped|TableRenamed> $events
62+
*/
63+
public function store(Collection $events): void
2664
{
2765
$this->events = $this->events->concat($events);
2866
}
67+
68+
public function flush(): void
69+
{
70+
$this->events = new Collection;
71+
}
2972
}

tests/Feature/EventRepositoryTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use Illuminate\Database\Events\MigrationsEnded;
4+
use Illuminate\Support\Facades\Event;
5+
use Plank\LaravelSchemaEvents\Facades\SchemaEvents;
6+
7+
use function Pest\Laravel\artisan;
8+
9+
it('events are collected and queried correctly', function () {
10+
Event::forget(MigrationsEnded::class);
11+
12+
artisan('migrate', [
13+
'--path' => migrationPath('repository'),
14+
'--realpath' => true,
15+
])->run();
16+
17+
expect(SchemaEvents::get())->toHaveCount(4);
18+
expect(SchemaEvents::created())->toHaveCount(1);
19+
expect(SchemaEvents::changed())->toHaveCount(1);
20+
expect(SchemaEvents::renamed())->toHaveCount(1);
21+
expect(SchemaEvents::dropped())->toHaveCount(1);
22+
23+
SchemaEvents::flush();
24+
expect(SchemaEvents::get())->toBeEmpty();
25+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::create('comments', function (Blueprint $table) {
12+
$table->id();
13+
$table->unsignedBigInteger('post_id');
14+
$table->string('body');
15+
$table->timestamps();
16+
17+
$table->foreign('post_id')
18+
->references('id')
19+
->on('posts')
20+
->nullOnDelete();
21+
});
22+
}
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table('posts', function (Blueprint $table) {
12+
$table->string('subtitle')->after('title');
13+
$table->string('slug')->after('subtitle');
14+
$table->unique('slug');
15+
$table->longText('body')->change();
16+
$table->foreignId('publisher_id')->constrained();
17+
$table->string('published_at');
18+
$table->index(['published_at']);
19+
20+
$table->renameColumn('description', 'blurb');
21+
$table->dropColumn('teaser');
22+
$table->dropForeign(['author_id']);
23+
$table->dropIndex(['tag']);
24+
$table->renameIndex('posts_category_index', 'category_index');
25+
});
26+
}
27+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\Schema;
5+
6+
return new class extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::rename('posts', 'articles');
11+
}
12+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Support\Facades\Schema;
5+
6+
return new class extends Migration
7+
{
8+
public function up()
9+
{
10+
Schema::drop('articles');
11+
}
12+
};

0 commit comments

Comments
 (0)