Skip to content

Commit

Permalink
add transactional store interface
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidBadura committed Feb 19, 2024
1 parent d8cc942 commit e96cd7a
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 19 deletions.
9 changes: 7 additions & 2 deletions src/Projection/Projection/Store/DoctrineStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Closure;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
Expand All @@ -33,7 +34,7 @@
* retry: int,
* }
*/
final class DoctrineStore implements ProjectionStore, SchemaConfigurator
final class DoctrineStore implements ProjectionStore, TransactionalStore, SchemaConfigurator
{
public function __construct(
private readonly Connection $connection,
Expand Down Expand Up @@ -162,7 +163,11 @@ public function transactional(Closure $closure): void
try {
$closure();
} finally {
$this->connection->commit();
try {
$this->connection->commit();
} catch (DriverException $e) {
throw new TransactionCommitNotPossible($e);
}
}
}

Expand Down
6 changes: 0 additions & 6 deletions src/Projection/Projection/Store/InMemoryStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Patchlevel\EventSourcing\Projection\Projection\Store;

use Closure;
use Patchlevel\EventSourcing\Projection\Projection\Projection;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionAlreadyExists;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria;
Expand Down Expand Up @@ -96,9 +95,4 @@ public function remove(Projection $projection): void
{
unset($this->projections[$projection->id()]);
}

public function transactional(Closure $closure): void
{
$closure();
}
}
3 changes: 0 additions & 3 deletions src/Projection/Projection/Store/ProjectionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Patchlevel\EventSourcing\Projection\Projection\Store;

use Closure;
use Patchlevel\EventSourcing\Projection\Projection\Projection;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionAlreadyExists;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria;
Expand All @@ -26,6 +25,4 @@ public function update(Projection $projection): void;

/** @throws ProjectionNotFound */
public function remove(Projection $projection): void;

public function transactional(Closure $closure): void;
}
20 changes: 20 additions & 0 deletions src/Projection/Projection/Store/TransactionCommitNotPossible.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projection\Store;

use RuntimeException;
use Throwable;

final class TransactionCommitNotPossible extends RuntimeException
{
public function __construct(Throwable $previous)
{
parent::__construct(
'Committing a transaction is not possible. Maybe your platform does not support transactional DDL.',
0,
$previous,
);
}
}
12 changes: 12 additions & 0 deletions src/Projection/Projection/Store/TransactionalStore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Projection\Projection\Store;

use Closure;

interface TransactionalStore
{
public function transactional(Closure $closure): void;
}
10 changes: 8 additions & 2 deletions src/Projection/Projectionist/DefaultProjectionist.php
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ function (array $projections) use ($throwByError): void {
private function discoverNewProjections(): void
{
$this->findForUpdate(
null,
new ProjectionCriteria(),
function (array $projections): void {
foreach ($this->projectors as $projector) {
$projectorId = $this->projectorId($projector);
Expand Down Expand Up @@ -821,8 +821,14 @@ private function lowestProjectionPosition(array $projections): int
}

/** @param Closure(list<Projection>):void $closure */
private function findForUpdate(ProjectionCriteria|null $criteria, Closure $closure): void
private function findForUpdate(ProjectionCriteria $criteria, Closure $closure): void
{
if (!$this->projectionStore instanceof Store) {
$closure($this->projectionStore->find($criteria));

return;
}

$this->projectionStore->transactional(function () use ($closure, $criteria): void {
$projections = $this->projectionStore->find($criteria);

Expand Down
6 changes: 0 additions & 6 deletions tests/Unit/Projection/DummyStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Patchlevel\EventSourcing\Tests\Unit\Projection;

use Closure;
use Patchlevel\EventSourcing\Projection\Projection\Projection;
use Patchlevel\EventSourcing\Projection\Projection\ProjectionCriteria;
use Patchlevel\EventSourcing\Projection\Projection\Store\InMemoryStore;
Expand Down Expand Up @@ -57,9 +56,4 @@ public function remove(Projection $projection): void
$this->parentStore->remove($projection);
$this->removedProjections[] = clone $projection;
}

public function transactional(Closure $closure): void
{
$this->parentStore->transactional($closure);
}
}

0 comments on commit e96cd7a

Please sign in to comment.