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
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use EventSauce\IdEncoding\BinaryUuidIdEncoder;
use EventSauce\EventSourcing\Serialization\ConstructingMessageSerializer;
use EventSauce\MessageRepository\TableSchema\DefaultTableSchema;
use EventSauce\MessageRepository\TestTooling\BinaryUuidTestTrait;

class DefaultDoctrineMessageRepositoryTest extends DoctrineMessageRepositoryTestCase
{
use BinaryUuidTestTrait;

protected string $tableName = 'domain_messages_uuid';

protected function messageRepository(): DoctrineMessageRepository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use EventSauce\EventSourcing\Serialization\ConstructingMessageSerializer;
use EventSauce\MessageRepository\TableSchema\DefaultTableSchema;
use EventSauce\MessageRepository\TestTooling\BinaryUuidTestTrait;
use EventSauce\UuidEncoding\BinaryUuidEncoder;

class DefaultDoctrineUuidV4MessageRepositoryTest extends DoctrineMessageRepositoryTestCase
{
use BinaryUuidTestTrait;

protected string $tableName = 'domain_messages_uuid';

protected function messageRepository(): DoctrineUuidV4MessageRepository
Expand Down
26 changes: 21 additions & 5 deletions src/DoctrineMessageRepository/DoctrineMessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EventSauce\MessageRepository\DoctrineMessageRepository;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\QueryBuilder;
use EventSauce\IdEncoding\BinaryUuidIdEncoder;
use EventSauce\IdEncoding\IdEncoder;
Expand Down Expand Up @@ -36,6 +37,8 @@ class DoctrineMessageRepository implements MessageRepository
private TableSchema $tableSchema;
private IdEncoder $aggregateRootIdEncoder;
private IdEncoder $eventIdEncoder;
private bool $binaryAggregateRootId;
private bool $binaryEventId;

public function __construct(
private Connection $connection,
Expand All @@ -50,6 +53,9 @@ public function __construct(
$this->tableSchema = $tableSchema ?? new DefaultTableSchema();
$this->aggregateRootIdEncoder = $aggregateRootIdEncoder ?? new BinaryUuidIdEncoder();
$this->eventIdEncoder = $eventIdEncoder ?? $this->aggregateRootIdEncoder;

$this->binaryAggregateRootId = $this->aggregateRootIdEncoder instanceof BinaryUuidIdEncoder;
$this->binaryEventId = $this->eventIdEncoder instanceof BinaryUuidIdEncoder;
}

public function persist(Message ...$messages): void
Expand All @@ -73,9 +79,11 @@ public function persist(Message ...$messages): void
$payload = $this->serializer->serializeMessage($message);
$payload['headers'][Header::EVENT_ID] ??= Uuid::uuid4()->toString();

$eventIdIndex = $this->indexParameter('event_id', $index);
$aggregateRootIdIndex = $this->indexParameter('aggregate_root_id', $index);
$messageParameters = [
$this->indexParameter('event_id', $index) => $this->eventIdEncoder->encodeId($payload['headers'][Header::EVENT_ID]),
$this->indexParameter('aggregate_root_id', $index) => $this->aggregateRootIdEncoder->encodeId($message->aggregateRootId()),
$eventIdIndex => $this->eventIdEncoder->encodeId($payload['headers'][Header::EVENT_ID]),
$aggregateRootIdIndex => $this->aggregateRootIdEncoder->encodeId($message->aggregateRootId()),
$this->indexParameter('version', $index) => $payload['headers'][Header::AGGREGATE_ROOT_VERSION] ?? 0,
$this->indexParameter('payload', $index) => json_encode($payload, $this->jsonEncodeOptions),
];
Expand All @@ -98,8 +106,16 @@ public function persist(Message ...$messages): void
implode("),\n(", $insertValues),
);

$types = [];
if ($this->binaryEventId) {
$types[$eventIdIndex] = ParameterType::BINARY;
}
if ($this->binaryAggregateRootId) {
$types[$aggregateRootIdIndex] = ParameterType::BINARY;
}

try {
$this->connection->executeStatement($insertQuery, $insertParameters);
$this->connection->executeStatement($insertQuery, $insertParameters, $types);
} catch (Throwable $exception) {
throw UnableToPersistMessages::dueTo('', $exception);
}
Expand All @@ -119,7 +135,7 @@ public function retrieveAll(AggregateRootId $id): Generator
{
$builder = $this->createQueryBuilder();
$builder->where(sprintf('%s = :aggregate_root_id', $this->tableSchema->aggregateRootIdColumn()));
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id));
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id), $this->binaryAggregateRootId ? ParameterType::BINARY : ParameterType::STRING);

try {
return $this->yieldMessagesFromPayloads($builder->executeQuery()->iterateColumn());
Expand All @@ -136,7 +152,7 @@ public function retrieveAllAfterVersion(AggregateRootId $id, int $aggregateRootV
$builder = $this->createQueryBuilder();
$builder->where(sprintf('%s = :aggregate_root_id', $this->tableSchema->aggregateRootIdColumn()));
$builder->andWhere(sprintf('%s > :version', $this->tableSchema->versionColumn()));
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id));
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id), $this->binaryAggregateRootId ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('version', $aggregateRootVersion);

try {
Expand Down
21 changes: 16 additions & 5 deletions src/DoctrineMessageRepository/DoctrineUuidV4MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EventSauce\MessageRepository\DoctrineMessageRepository;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\QueryBuilder;
use EventSauce\EventSourcing\AggregateRootId;
use EventSauce\EventSourcing\Header;
Expand Down Expand Up @@ -74,9 +75,11 @@ public function persist(Message ...$messages): void
$payload = $this->serializer->serializeMessage($message);
$payload['headers'][Header::EVENT_ID] ??= Uuid::uuid4()->toString();

$eventIdIndex = $this->indexParameter('event_id', $index);
$aggregateRootIdIndex = $this->indexParameter('aggregate_root_id', $index);
$messageParameters = [
$this->indexParameter('event_id', $index) => $this->uuidEncoder->encodeString($payload['headers'][Header::EVENT_ID]),
$this->indexParameter('aggregate_root_id', $index) => $this->uuidEncoder->encodeString($payload['headers'][Header::AGGREGATE_ROOT_ID]),
$eventIdIndex => $this->uuidEncoder->encodeString($payload['headers'][Header::EVENT_ID]),
$aggregateRootIdIndex => $this->uuidEncoder->encodeString($payload['headers'][Header::AGGREGATE_ROOT_ID]),
$this->indexParameter('version', $index) => $payload['headers'][Header::AGGREGATE_ROOT_VERSION] ?? 0,
$this->indexParameter('payload', $index) => json_encode($payload, $this->jsonEncodeOptions),
];
Expand All @@ -99,8 +102,16 @@ public function persist(Message ...$messages): void
implode("),\n(", $insertValues),
);

$types = [];
if ($this->uuidEncoder instanceof BinaryUuidEncoder) {
$types = [
$eventIdIndex => ParameterType::BINARY,
$aggregateRootIdIndex => ParameterType::BINARY,
];
}

try {
$this->connection->executeStatement($insertQuery, $insertParameters);
$this->connection->executeStatement($insertQuery, $insertParameters, $types);
} catch (Throwable $exception) {
throw UnableToPersistMessages::dueTo('', $exception);
}
Expand All @@ -120,7 +131,7 @@ public function retrieveAll(AggregateRootId $id): Generator
{
$builder = $this->createQueryBuilder();
$builder->where(sprintf('%s = :aggregate_root_id', $this->tableSchema->aggregateRootIdColumn()));
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()));
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()), $this->uuidEncoder instanceof BinaryUuidEncoder ? ParameterType::BINARY : ParameterType::STRING);

try {
return $this->yieldMessagesFromPayloads($builder->executeQuery()->iterateColumn());
Expand All @@ -137,7 +148,7 @@ public function retrieveAllAfterVersion(AggregateRootId $id, int $aggregateRootV
$builder = $this->createQueryBuilder();
$builder->where(sprintf('%s = :aggregate_root_id', $this->tableSchema->aggregateRootIdColumn()));
$builder->andWhere(sprintf('%s > :version', $this->tableSchema->versionColumn()));
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()));
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()), $this->uuidEncoder instanceof BinaryUuidEncoder ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('version', $aggregateRootVersion);

try {
Expand Down
36 changes: 36 additions & 0 deletions src/MessageRepositoryTestTooling/BinaryUuidTestTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace EventSauce\MessageRepository\TestTooling;

trait BinaryUuidTestTrait
{
/**
* @test
*/
public function it_uses_correct_parameter_types_for_binary_fields(): void
{
if (version_compare(PHP_VERSION, '8.4', '<')) {
self::markTestSkipped('PHP version needs to be >=8.4 to have PDO use binary hints');
}

$repository = $this->messageRepository();
$message = $this->createMessage('payload');

$repository->persist($message);
$this->assertWarnings('persist()');

$repository->retrieveAll($message->aggregateRootId());
$this->assertWarnings('retrieveAll()');

$repository->retrieveAllAfterVersion($message->aggregateRootId(), 1);
$this->assertWarnings('retrieveAllAfterVersion()');
}

private function assertWarnings(string $scenario): void
{
$warnings = $this->connection->executeQuery('SHOW WARNINGS')->fetchAllAssociative();
self::assertSame([], $warnings, 'Binary uuids were not properly passed during ' . $scenario);
}
}