Skip to content

Commit d65b7df

Browse files
committed
feat: create all entities if no pending migrations
1 parent 2be8ca6 commit d65b7df

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ class AppServiceProvider extends ServiceProvider
342342
}
343343
```
344344

345+
The listener will also create all entities if there's no pending migrations,
346+
ensuring any new entities are created automatically.
347+
345348
## 🤝 Contributing
346349

347350
Thank you for considering contributing! You can read the contribution guide [here](CONTRIBUTING.md).

src/Listeners/SyncSqlEntities.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CalebDW\SqlEntities\SqlEntityManager;
88
use Illuminate\Database\Events\MigrationsEnded;
99
use Illuminate\Database\Events\MigrationsStarted;
10+
use Illuminate\Database\Events\NoPendingMigrations;
1011

1112
class SyncSqlEntities
1213
{
@@ -41,15 +42,27 @@ public function handleEnded(MigrationsEnded $event): void
4142
$this->manager->createAll();
4243
}
4344

45+
public function handleNoPending(NoPendingMigrations $event): void
46+
{
47+
if ($event->method !== 'up') {
48+
return;
49+
}
50+
51+
// We still need to create the entities if there are no pending
52+
// migrations because new entities may have been added to the code.
53+
$this->manager->createAll();
54+
}
55+
4456
/**
4557
* @codeCoverageIgnore
4658
* @return array<string, string>
4759
*/
4860
public function subscribe(): array
4961
{
5062
return [
51-
MigrationsStarted::class => 'handleStarted',
52-
MigrationsEnded::class => 'handleEnded',
63+
MigrationsStarted::class => 'handleStarted',
64+
MigrationsEnded::class => 'handleEnded',
65+
NoPendingMigrations::class => 'handleNoPending',
5366
];
5467
}
5568
}

tests/Unit/Listeners/SyncEntitiesSubscriberTest.php

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CalebDW\SqlEntities\SqlEntityManager;
77
use Illuminate\Database\Events\MigrationsEnded;
88
use Illuminate\Database\Events\MigrationsStarted;
9+
use Illuminate\Database\Events\NoPendingMigrations;
910

1011
beforeEach(function () {
1112
test()->manager = Mockery::mock(SqlEntityManager::class);
@@ -63,3 +64,21 @@
6364
);
6465
});
6566
});
67+
68+
describe('no pending', function () {
69+
it('does nothing if the method is not "up"', function () {
70+
test()->manager->shouldNotReceive('createAll');
71+
test()->listener->handleNoPending(
72+
new NoPendingMigrations(method: 'down'),
73+
);
74+
});
75+
it('creates all entities', function () {
76+
test()->manager
77+
->shouldReceive('createAll')
78+
->once();
79+
80+
test()->listener->handleNoPending(
81+
new NoPendingMigrations(method: 'up'),
82+
);
83+
});
84+
});

0 commit comments

Comments
 (0)