Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
],
"require": {
"php": "~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0",
"doctrine/dbal": "^4.0.0",
"doctrine/dbal": "^4.4.0",
"doctrine/migrations": "^3.3.2",
"patchlevel/hydrator": "^1.8.0",
"patchlevel/worker": "^1.4.0",
Expand Down
22 changes: 11 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 6 additions & 24 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,12 @@ parameters:
count: 1
path: tests/Integration/Store/StreamDoctrineDbalStoreTest.php

-
message: '#^Parameter \#1 \$table of class Patchlevel\\EventSourcing\\Subscription\\Cleanup\\Dbal\\DropTableTask constructor expects non\-empty\-string, string given\.$#'
identifier: argument.type
count: 1
path: tests/Integration/Subscription/Subscriber/ProfileProjectionWithCleanup.php

-
message: '#^Call to static method PHPUnit\\Framework\\Assert\:\:assertArrayHasKey\(\) with 0 and array\{Patchlevel\\EventSourcing\\Subscription\\Subscription\} will always evaluate to true\.$#'
identifier: staticMethod.alreadyNarrowedType
Expand All @@ -336,18 +342,6 @@ parameters:
count: 1
path: tests/Unit/Aggregate/AggregateRootTest.php

-
message: '#^Cannot access offset 0 on iterable\<int, Patchlevel\\EventSourcing\\CommandBus\\HandlerDescriptor\>\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 2
path: tests/Unit/CommandBus/AggregateHandlerProviderTest.php

-
message: '#^Cannot call method callable\(\) on mixed\.$#'
identifier: method.nonObject
count: 2
path: tests/Unit/CommandBus/AggregateHandlerProviderTest.php

-
message: '#^Parameter \#2 \$aggregateClass of class Patchlevel\\EventSourcing\\CommandBus\\Handler\\CreateAggregateHandler constructor expects class\-string\<Patchlevel\\EventSourcing\\Aggregate\\AggregateRoot\>, string given\.$#'
identifier: argument.type
Expand All @@ -366,18 +360,6 @@ parameters:
count: 5
path: tests/Unit/CommandBus/InstantRetryCommandBusTest.php

-
message: '#^Cannot access offset 0 on iterable\<int, Patchlevel\\EventSourcing\\CommandBus\\HandlerDescriptor\>\.$#'
identifier: offsetAccess.nonOffsetAccessible
count: 2
path: tests/Unit/CommandBus/ServiceHandlerProviderTest.php

-
message: '#^Cannot call method callable\(\) on mixed\.$#'
identifier: method.nonObject
count: 2
path: tests/Unit/CommandBus/ServiceHandlerProviderTest.php

-
message: '#^Parameter \#1 \$data of static method Patchlevel\\EventSourcing\\Tests\\Unit\\Fixture\\Message\:\:fromArray\(\) expects array\{id\: string, text\: string, createdAt\: string\}, array\<mixed, mixed\> given\.$#'
identifier: argument.type
Expand Down
20 changes: 18 additions & 2 deletions src/Subscription/Cleanup/Dbal/DbalCleanupTaskHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskHandler;
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskNotSupported;

use function strtolower;

final class DbalCleanupTaskHandler implements CleanupTaskHandler
{
public function __construct(
Expand All @@ -19,13 +21,27 @@
public function __invoke(object $task): void
{
if ($task instanceof DropTableTask) {
$this->connection($task->connectionName)->createSchemaManager()->dropTable($task->table);
$schemaManager = $this->connection($task->connectionName)->createSchemaManager();
if ($schemaManager->tablesExist([$task->table])) {
$schemaManager->dropTable($task->table);
}

return;
}

if ($task instanceof DropIndexTask) {
$this->connection($task->connectionName)->createSchemaManager()->dropIndex($task->index, $task->table);
$schemaManager = $this->connection($task->connectionName)->createSchemaManager();

if (!$schemaManager->tablesExist([$task->table])) {
return;
}

foreach ($schemaManager->introspectTableIndexesByUnquotedName($task->table) as $index) {
if (strtolower($index->getObjectName()->toString()) === strtolower($task->index)) {

Check warning on line 40 in src/Subscription/Cleanup/Dbal/DbalCleanupTaskHandler.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "UnwrapStrToLower": @@ @@ } foreach ($schemaManager->introspectTableIndexesByUnquotedName($task->table) as $index) { - if (strtolower($index->getObjectName()->toString()) === strtolower($task->index)) { + if (strtolower($index->getObjectName()->toString()) === $task->index) { $schemaManager->dropIndex($task->index, $task->table); break; }

Check warning on line 40 in src/Subscription/Cleanup/Dbal/DbalCleanupTaskHandler.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "UnwrapStrToLower": @@ @@ } foreach ($schemaManager->introspectTableIndexesByUnquotedName($task->table) as $index) { - if (strtolower($index->getObjectName()->toString()) === strtolower($task->index)) { + if ($index->getObjectName()->toString() === strtolower($task->index)) { $schemaManager->dropIndex($task->index, $task->table); break; }
$schemaManager->dropIndex($task->index, $task->table);
break;

Check warning on line 42 in src/Subscription/Cleanup/Dbal/DbalCleanupTaskHandler.php

View workflow job for this annotation

GitHub Actions / Mutation tests on diff (locked, 8.5, ubuntu-latest)

Escaped Mutant for Mutator "Break_": @@ @@ foreach ($schemaManager->introspectTableIndexesByUnquotedName($task->table) as $index) { if (strtolower($index->getObjectName()->toString()) === strtolower($task->index)) { $schemaManager->dropIndex($task->index, $task->table); - break; + continue; } }
}
}

return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Subscription/Cleanup/Dbal/DropIndexTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

final class DropIndexTask
{
/**
* @param non-empty-string $index
* @param non-empty-string $table
* @param non-empty-string|null $connectionName
*/
public function __construct(
public readonly string $index,
public readonly string $table,
Expand Down
4 changes: 4 additions & 0 deletions src/Subscription/Cleanup/Dbal/DropTableTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

final class DropTableTask
{
/**
* @param non-empty-string $table
* @param non-empty-string|null $connectionName
*/
public function __construct(
public readonly string $table,
public readonly string|null $connectionName = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Index;
use Doctrine\Persistence\ConnectionRegistry;
use Patchlevel\EventSourcing\Subscription\Cleanup\CleanupTaskNotSupported;
use Patchlevel\EventSourcing\Subscription\Cleanup\Dbal\ConnectionNameNotSupported;
Expand Down Expand Up @@ -53,6 +54,7 @@ public function testHandleNoSupportedTask(): void
public function testHandleDropTable(): void
{
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaManager->expects($this->once())->method('tablesExist')->with(['test'])->willReturn(true);
$schemaManager->expects($this->once())->method('dropTable')->with('test');

$connection = $this->createMock(Connection::class);
Expand All @@ -66,9 +68,32 @@ public function testHandleDropTable(): void
$handler(new DropTableTask('test'));
}

public function testHandleDropTableIfNotExists(): void
{
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaManager->expects($this->once())->method('tablesExist')->with(['test'])->willReturn(false);
$schemaManager->expects($this->never())->method('dropTable');

$connection = $this->createMock(Connection::class);
$connection
->expects($this->once())
->method('createSchemaManager')
->willReturn($schemaManager);

$handler = new DbalCleanupTaskHandler($connection);

$handler(new DropTableTask('test'));
}

public function testHandleDropIndex(): void
{
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaManager->expects($this->once())->method('tablesExist')->with(['bar'])->willReturn(true);
$schemaManager
->expects($this->once())
->method('introspectTableIndexesByUnquotedName')
->with('bar')
->willReturn([new Index('foo', ['id'])]);
$schemaManager->expects($this->once())->method('dropIndex')->with('foo', 'bar');

$connection = $this->createMock(Connection::class);
Expand All @@ -82,9 +107,50 @@ public function testHandleDropIndex(): void
$handler(new DropIndexTask('foo', 'bar'));
}

public function testHandleDropIndexIfTableNotExists(): void
{
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaManager->expects($this->once())->method('tablesExist')->with(['bar'])->willReturn(false);
$schemaManager->expects($this->never())->method('introspectTableIndexesByUnquotedName');
$schemaManager->expects($this->never())->method('dropIndex');

$connection = $this->createMock(Connection::class);
$connection
->expects($this->once())
->method('createSchemaManager')
->willReturn($schemaManager);

$handler = new DbalCleanupTaskHandler($connection);

$handler(new DropIndexTask('foo', 'bar'));
}

public function testHandleDropIndexIfIndexNotExists(): void
{
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaManager->expects($this->once())->method('tablesExist')->with(['bar'])->willReturn(true);
$schemaManager
->expects($this->once())
->method('introspectTableIndexesByUnquotedName')
->with('bar')
->willReturn([new Index('baz', ['id'])]);
$schemaManager->expects($this->never())->method('dropIndex');

$connection = $this->createMock(Connection::class);
$connection
->expects($this->once())
->method('createSchemaManager')
->willReturn($schemaManager);

$handler = new DbalCleanupTaskHandler($connection);

$handler(new DropIndexTask('foo', 'bar'));
}

public function testHandleWithConnectionRegistry(): void
{
$schemaManager = $this->createMock(AbstractSchemaManager::class);
$schemaManager->expects($this->once())->method('tablesExist')->with(['test'])->willReturn(true);
$schemaManager->expects($this->once())->method('dropTable')->with('test');

$connection = $this->createMock(Connection::class);
Expand Down
Loading