Skip to content

Commit f264a20

Browse files
committed
feat: allow skipping entity creation and dropping
1 parent c40ce2b commit f264a20

File tree

4 files changed

+93
-32
lines changed

4 files changed

+93
-32
lines changed

src/Entities/Entity.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,29 @@ public function connectionName(): ?string
2222
return null;
2323
}
2424

25-
/** Hook before creating the entity. */
26-
public function creating(Connection $connection): void
25+
/**
26+
* Hook before creating the entity.
27+
*
28+
* @return bool true to create the entity, false to skip.
29+
*/
30+
public function creating(Connection $connection): bool
2731
{
32+
return true;
2833
}
2934

3035
/** Hook after creating the entity. */
3136
public function created(Connection $connection): void
3237
{
3338
}
3439

35-
/** Hook before dropping the entity. */
36-
public function dropping(Connection $connection): void
40+
/**
41+
* Hook before dropping the entity.
42+
*
43+
* @return bool true to drop the entity, false to skip.
44+
*/
45+
public function dropping(Connection $connection): bool
3746
{
47+
return true;
3848
}
3949

4050
/** Hook after dropping the entity. */

src/EntityManager.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ public function create(Entity|string $entity): void
5353
}
5454

5555
$connection = $this->connection($entity);
56-
$grammar = $this->grammar($connection);
5756

58-
$entity->creating($connection);
57+
if (! $entity->creating($connection)) {
58+
return;
59+
}
60+
61+
$grammar = $this->grammar($connection);
62+
5963
$connection->statement($grammar->compileCreate($entity));
6064
$entity->created($connection);
6165
}
@@ -68,9 +72,13 @@ public function drop(Entity|string $entity): void
6872
}
6973

7074
$connection = $this->connection($entity);
71-
$grammar = $this->grammar($connection);
7275

73-
$entity->dropping($connection);
76+
if (! $entity->dropping($connection)) {
77+
return;
78+
}
79+
80+
$grammar = $this->grammar($connection);
81+
7482
$connection->statement($grammar->compileDrop($entity));
7583
$entity->dropped($connection);
7684
}

tests/Feature/EntityManagerTest.php

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,57 @@
4848
})->throws(InvalidArgumentException::class, 'Entity [unknown] not found.');
4949
});
5050

51-
it('creates an entity', function (string|Entity $entity) {
52-
test()->connection
53-
->shouldReceive('getDriverName')->once()->andReturn('sqlite')
54-
->shouldReceive('statement')
55-
->once()
56-
->withArgs(fn ($sql) => str_contains($sql, 'CREATE VIEW'));
57-
58-
test()->manager->create($entity);
59-
})->with([
60-
'name' => 'users_view',
61-
'entity' => new UserView(),
62-
]);
63-
64-
it('drops an entity', function (string|Entity $entity) {
65-
test()->connection
66-
->shouldReceive('getDriverName')->once()->andReturn('pgsql')
67-
->shouldReceive('statement')
68-
->once()
69-
->withArgs(fn ($sql) => str_contains($sql, 'DROP VIEW'));
51+
describe('create', function () {
52+
it('creates an entity', function (string|Entity $entity) {
53+
test()->connection
54+
->shouldReceive('getDriverName')->once()->andReturn('sqlite')
55+
->shouldReceive('statement')
56+
->once()
57+
->withArgs(fn ($sql) => str_contains($sql, 'CREATE VIEW'));
58+
59+
test()->manager->create($entity);
60+
})->with([
61+
'name' => 'users_view',
62+
'entity' => new UserView(),
63+
]);
64+
65+
it('can skip creation', function () {
66+
$entity = new UserView();
67+
68+
test()->connection
69+
->shouldNotReceive('getDriverName')
70+
->shouldNotReceive('statement');
71+
72+
$entity->shouldCreate = false;
73+
test()->manager->create($entity);
74+
});
75+
});
7076

71-
test()->manager->drop($entity);
72-
})->with([
73-
'name' => 'users_view',
74-
'entity' => new UserView(),
75-
]);
77+
describe('drop', function () {
78+
it('drops an entity', function (string|Entity $entity) {
79+
test()->connection
80+
->shouldReceive('getDriverName')->once()->andReturn('pgsql')
81+
->shouldReceive('statement')
82+
->once()
83+
->withArgs(fn ($sql) => str_contains($sql, 'DROP VIEW'));
84+
85+
test()->manager->drop($entity);
86+
})->with([
87+
'name' => 'users_view',
88+
'entity' => new UserView(),
89+
]);
90+
91+
it('can skip dropping', function () {
92+
$entity = new UserView();
93+
94+
test()->connection
95+
->shouldNotReceive('getDriverName')
96+
->shouldNotReceive('statement');
97+
98+
$entity->shouldDrop = false;
99+
test()->manager->drop($entity);
100+
});
101+
});
76102

77103
it('creates entities by type and connection', function () {
78104
test()->connection

workbench/database/entities/views/UserView.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
namespace Workbench\Database\Entities\views;
66

77
use CalebDW\SqlEntities\Entities\View;
8+
use Illuminate\Database\Connection;
89
use Override;
910

1011
class UserView extends View
1112
{
13+
public bool $shouldCreate = true;
14+
15+
public bool $shouldDrop = true;
16+
1217
#[Override]
1318
public function name(): string
1419
{
@@ -20,4 +25,16 @@ public function definition(): string
2025
{
2126
return 'SELECT id, name FROM users';
2227
}
28+
29+
#[Override]
30+
public function creating(Connection $connection): bool
31+
{
32+
return $this->shouldCreate;
33+
}
34+
35+
#[Override]
36+
public function dropping(Connection $connection): bool
37+
{
38+
return $this->shouldDrop;
39+
}
2340
}

0 commit comments

Comments
 (0)