Skip to content

Commit

Permalink
compatibility with doctrine/dbal 3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
othillo committed Jun 24, 2021
1 parent 9f18ebc commit 7cc0b51
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 38 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
composer.lock
.php-cs-fixer.cache
.phpunit.result.cache
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,3 @@ parameters:
count: 1
path: src/DBALEventStore.php

-
message: "#^Method Broadway\\\\EventStore\\\\Dbal\\\\DBALEventStore\\:\\:prepareVisitEventsStatement\\(\\) should return Doctrine\\\\DBAL\\\\Driver\\\\Statement but returns Doctrine\\\\DBAL\\\\ForwardCompatibility\\\\DriverResultStatement\\|Doctrine\\\\DBAL\\\\ForwardCompatibility\\\\DriverStatement\\.$#"
count: 1
path: src/DBALEventStore.php

33 changes: 13 additions & 20 deletions src/DBALEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
use Broadway\Serializer\Serializer;
use Broadway\UuidGenerator\Converter\BinaryUuidConverterInterface;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Version;
use Doctrine\DBAL\Statement;

/**
* Event store using a relational database as storage.
Expand Down Expand Up @@ -88,13 +88,9 @@ public function __construct(
$this->payloadSerializer = $payloadSerializer;
$this->metadataSerializer = $metadataSerializer;
$this->tableName = $tableName;
$this->useBinary = (bool) $useBinary;
$this->useBinary = $useBinary;
$this->binaryUuidConverter = $binaryUuidConverter;

if ($this->useBinary && Version::compare('2.5.0') >= 0) {
throw new \InvalidArgumentException('The Binary storage is only available with Doctrine DBAL >= 2.5.0');
}

if ($this->useBinary && null === $binaryUuidConverter) {
throw new \LogicException('binary UUID converter is required when using binary');
}
Expand All @@ -108,10 +104,10 @@ public function load($id): DomainEventStream
$statement = $this->prepareLoadStatement();
$statement->bindValue(1, $this->convertIdentifierToStorageValue($id));
$statement->bindValue(2, 0);
$statement->execute();
$result = $statement->executeQuery();

$events = [];
while ($row = $statement->fetch()) {
while ($row = $result->fetchAssociative()) {
$events[] = $this->deserializeEvent($row);
}

Expand All @@ -130,10 +126,10 @@ public function loadFromPlayhead($id, int $playhead): DomainEventStream
$statement = $this->prepareLoadStatement();
$statement->bindValue(1, $this->convertIdentifierToStorageValue($id));
$statement->bindValue(2, $playhead);
$statement->execute();
$result = $statement->executeQuery();

$events = [];
while ($row = $statement->fetch()) {
while ($row = $result->fetchAssociative()) {
$events[] = $this->deserializeEvent($row);
}

Expand Down Expand Up @@ -165,7 +161,7 @@ public function append($id, DomainEventStream $eventStream): void
$this->connection->rollBack();

throw new DuplicatePlayheadException($eventStream, $exception);
} catch (DBALException $exception) {
} catch (Exception $exception) {
$this->connection->rollBack();

throw DBALEventStoreException::create($exception);
Expand Down Expand Up @@ -293,27 +289,24 @@ private function convertStorageValueToIdentifier($id)

public function visitEvents(Criteria $criteria, EventVisitor $eventVisitor): void
{
$statement = $this->prepareVisitEventsStatement($criteria);
$statement->execute();
$result = $this->prepareVisitEventsStatement($criteria);

while ($row = $statement->fetch()) {
while ($row = $result->fetchAssociative()) {
$domainMessage = $this->deserializeEvent($row);

$eventVisitor->doWithEvent($domainMessage);
}
}

private function prepareVisitEventsStatement(Criteria $criteria): Statement
private function prepareVisitEventsStatement(Criteria $criteria): Result
{
list($where, $bindValues, $bindValueTypes) = $this->prepareVisitEventsStatementWhereAndBindValues($criteria);
$query = 'SELECT uuid, playhead, metadata, payload, recorded_on
FROM '.$this->tableName.'
'.$where.'
ORDER BY id ASC';

$statement = $this->connection->executeQuery($query, $bindValues, $bindValueTypes);

return $statement;
return $this->connection->executeQuery($query, $bindValues, $bindValueTypes);
}

private function prepareVisitEventsStatementWhereAndBindValues(Criteria $criteria): array
Expand Down
4 changes: 2 additions & 2 deletions src/DBALEventStoreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
namespace Broadway\EventStore\Dbal;

use Broadway\EventStore\EventStoreException;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;

/**
* Wraps exceptions thrown by the DBAL event store.
*/
class DBALEventStoreException extends EventStoreException
{
public static function create(DBALException $exception): DBALEventStoreException
public static function create(Exception $exception): DBALEventStoreException
{
return new DBALEventStoreException('', 0, $exception);
}
Expand Down
8 changes: 2 additions & 6 deletions test/BinaryDBALEventStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Version;
use Doctrine\DBAL\Types\Types;

/**
* @requires extension pdo_sqlite
Expand All @@ -32,10 +32,6 @@ class BinaryDBALEventStoreTest extends DBALEventStoreTest

protected function setUp(): void
{
if (Version::compare('2.5.0') >= 0) {
$this->markTestSkipped('Binary type is only available for Doctrine >= v2.5');
}

$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
$schemaManager = $connection->getSchemaManager();
$schema = $schemaManager->createSchema();
Expand All @@ -61,7 +57,7 @@ public function table_should_contain_binary_uuid_column()
$uuidColumn = $this->table->getColumn('uuid');

$this->assertEquals(16, $uuidColumn->getLength());
$this->assertEquals(Type::getType(Type::BINARY), $uuidColumn->getType());
$this->assertEquals(Type::getType(Types::BINARY), $uuidColumn->getType());
$this->assertTrue($uuidColumn->getFixed());
}

Expand Down
5 changes: 0 additions & 5 deletions test/Management/BinaryDBALEventStoreManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use Broadway\Serializer\SimpleInterfaceSerializer;
use Broadway\UuidGenerator\Converter\BinaryUuidConverter;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Version;

/**
* @requires extension pdo_sqlite
Expand All @@ -29,10 +28,6 @@ class BinaryDBALEventStoreManagementTest extends DBALEventStoreManagementTest

public function createEventStore()
{
if (Version::compare('2.5.0') >= 0) {
$this->markTestSkipped('Binary type is only available for Doctrine >= v2.5');
}

$connection = DriverManager::getConnection(['driver' => 'pdo_sqlite', 'memory' => true]);
$schemaManager = $connection->getSchemaManager();
$schema = $schemaManager->createSchema();
Expand Down

0 comments on commit 7cc0b51

Please sign in to comment.