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 @@ -18,7 +18,6 @@ protected function messageRepository(): DoctrineMessageRepository
serializer: new ConstructingMessageSerializer(),
tableSchema: new DefaultTableSchema(),
aggregateRootIdEncoder: new BinaryUuidIdEncoder(),
eventIdEncoder: new BinaryUuidIdEncoder(),
);
}
}
25 changes: 5 additions & 20 deletions src/DoctrineMessageRepository/DoctrineMessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -74,11 +73,9 @@ 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 = [
$eventIdIndex => $this->eventIdEncoder->encodeId($payload['headers'][Header::EVENT_ID]),
$aggregateRootIdIndex => $this->aggregateRootIdEncoder->encodeId($message->aggregateRootId()),
$this->indexParameter('event_id', $index) => $this->eventIdEncoder->encodeId($payload['headers'][Header::EVENT_ID]),
$this->indexParameter('aggregate_root_id', $index) => $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 @@ -101,16 +98,8 @@ public function persist(Message ...$messages): void
implode("),\n(", $insertValues),
);

$types = [];
if ($this->eventIdEncoder instanceof BinaryUuidIdEncoder) {
$types[$eventIdIndex] = ParameterType::BINARY;
}
if ($this->aggregateRootIdEncoder instanceof BinaryUuidIdEncoder) {
$types[$aggregateRootIdIndex] = ParameterType::BINARY;
}

try {
$this->connection->executeStatement($insertQuery, $insertParameters, $types);
$this->connection->executeStatement($insertQuery, $insertParameters);
} catch (Throwable $exception) {
throw UnableToPersistMessages::dueTo('', $exception);
}
Expand All @@ -128,11 +117,9 @@ private function formatNamedParameters(array $parameters): array

public function retrieveAll(AggregateRootId $id): Generator
{
$aggregateRootIdType = $this->aggregateRootIdEncoder instanceof BinaryUuidIdEncoder ? ParameterType::BINARY : ParameterType::STRING;

$builder = $this->createQueryBuilder();
$builder->where(sprintf('%s = :aggregate_root_id', $this->tableSchema->aggregateRootIdColumn()));
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id), $aggregateRootIdType);
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id));

try {
return $this->yieldMessagesFromPayloads($builder->executeQuery()->iterateColumn());
Expand All @@ -146,12 +133,10 @@ public function retrieveAll(AggregateRootId $id): Generator
*/
public function retrieveAllAfterVersion(AggregateRootId $id, int $aggregateRootVersion): Generator
{
$aggregateRootIdType = $this->aggregateRootIdEncoder instanceof BinaryUuidIdEncoder ? ParameterType::BINARY : ParameterType::STRING;

$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), $aggregateRootIdType);
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id));
$builder->setParameter('version', $aggregateRootVersion);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,6 @@ protected function setUp(): void
$this->truncateTable();
}

protected function tearDown(): void
{
parent::tearDown();
if (str_contains($this->formatDsn(), 'pgsql')) {
// can only check this for mysql
return;
}

$warning = $this->connection->executeQuery('SHOW WARNINGS')->fetchNumeric();
if ($warning !== false && count($warning) > 0) {
if (str_contains($warning[2], 'Base table or view not found') || str_contains($warning[2], "doesn't exist")) {
// shortcut for tests
return;
}
self::fail(sprintf(
'Warnings issued durings tests, these can potentially result in data loss: [%d] %s',
$warning[1],
$warning[2],
));
}
}

protected function formatDsn(): string
{
$host = getenv('EVENTSAUCE_TESTING_MYSQL_HOST') ?: '127.0.0.1';
Expand Down
19 changes: 5 additions & 14 deletions src/DoctrineMessageRepository/DoctrineUuidV4MessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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 @@ -75,11 +74,9 @@ 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 = [
$eventIdIndex => $this->uuidEncoder->encodeString($payload['headers'][Header::EVENT_ID]),
$aggregateRootIdIndex => $this->uuidEncoder->encodeString($payload['headers'][Header::AGGREGATE_ROOT_ID]),
$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]),
$this->indexParameter('version', $index) => $payload['headers'][Header::AGGREGATE_ROOT_VERSION] ?? 0,
$this->indexParameter('payload', $index) => json_encode($payload, $this->jsonEncodeOptions),
];
Expand All @@ -102,14 +99,8 @@ public function persist(Message ...$messages): void
implode("),\n(", $insertValues),
);

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

try {
$this->connection->executeStatement($insertQuery, $insertParameters, $types);
$this->connection->executeStatement($insertQuery, $insertParameters);
} catch (Throwable $exception) {
throw UnableToPersistMessages::dueTo('', $exception);
}
Expand All @@ -129,7 +120,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()), $this->uuidEncoder instanceof BinaryUuidEncoder ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()));

try {
return $this->yieldMessagesFromPayloads($builder->executeQuery()->iterateColumn());
Expand All @@ -146,7 +137,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()), $this->uuidEncoder instanceof BinaryUuidEncoder ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()));
$builder->setParameter('version', $aggregateRootVersion);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use EventSauce\EventSourcing\Serialization\ConstructingMessageSerializer;
use EventSauce\MessageRepository\DoctrineV2MessageRepository\DoctrineMessageRepositoryTestCase;
use EventSauce\MessageRepository\TableSchema\DefaultTableSchema;
use EventSauce\UuidEncoding\BinaryUuidEncoder;

/**
* @group doctrine2
Expand Down
21 changes: 5 additions & 16 deletions src/DoctrineV2MessageRepository/DoctrineMessageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\ForwardCompatibility\Result;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\QueryBuilder;
use EventSauce\EventSourcing\AggregateRootId;
use EventSauce\EventSourcing\Header;
Expand Down Expand Up @@ -76,11 +75,9 @@ 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 = [
$eventIdIndex => $this->eventIdEncoder->encodeId($payload['headers'][Header::EVENT_ID]),
$aggregateRootIdIndex => $this->aggregateRootIdEncoder->encodeId($message->aggregateRootId()),
$this->indexParameter('event_id', $index) => $this->eventIdEncoder->encodeId($payload['headers'][Header::EVENT_ID]),
$this->indexParameter('aggregate_root_id', $index) => $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 @@ -103,16 +100,8 @@ public function persist(Message ...$messages): void
implode("),\n(", $insertValues),
);

$types = [];
if ($this->eventIdEncoder instanceof BinaryUuidIdEncoder) {
$types[$eventIdIndex] = ParameterType::BINARY;
}
if ($this->aggregateRootIdEncoder instanceof BinaryUuidIdEncoder) {
$types[$aggregateRootIdIndex] = ParameterType::BINARY;
}

try {
$this->connection->executeStatement($insertQuery, $insertParameters, $types);
$this->connection->executeStatement($insertQuery, $insertParameters);
} catch (Throwable $exception) {
throw UnableToPersistMessages::dueTo('', $exception);
}
Expand All @@ -132,7 +121,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), $this->aggregateRootIdEncoder instanceof BinaryUuidIdEncoder ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id));

try {
/** @var ResultStatement $resultStatement */
Expand All @@ -152,7 +141,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), $this->aggregateRootIdEncoder instanceof BinaryUuidIdEncoder ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('aggregate_root_id', $this->aggregateRootIdEncoder->encodeId($id));
$builder->setParameter('version', $aggregateRootVersion);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,6 @@ protected function setUp(): void
$this->connection->executeQuery('TRUNCATE TABLE ' . $this->tableName);
}

protected function tearDown(): void
{
parent::tearDown();
if (str_contains($this->formatDsn(), 'pgsql')) {
// can only check this for mysql
return;
}

$warning = $this->connection->executeQuery('SHOW WARNINGS')->fetchNumeric();
if ($warning !== false && count($warning) > 0) {
if (str_contains($warning[2], 'Base table or view not found') || str_contains($warning[2], "doesn't exist")) {
// shortcut for tests
return;
}
self::fail(sprintf(
'Warnings issued durings tests, these can potentially result in data loss: [%d] %s',
$warning[1],
$warning[2],
));
}
}

protected function aggregateRootId(): AggregateRootId
{
return DummyAggregateRootId::generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\ForwardCompatibility\Result;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Query\QueryBuilder;
use EventSauce\EventSourcing\AggregateRootId;
use EventSauce\EventSourcing\Header;
Expand Down Expand Up @@ -79,11 +78,9 @@ 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 = [
$eventIdIndex => $this->uuidEncoder->encodeString($payload['headers'][Header::EVENT_ID]),
$aggregateRootIdIndex => $this->uuidEncoder->encodeString($payload['headers'][Header::AGGREGATE_ROOT_ID]),
$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]),
$this->indexParameter('version', $index) => $payload['headers'][Header::AGGREGATE_ROOT_VERSION] ?? 0,
$this->indexParameter('payload', $index) => json_encode($payload, $this->jsonEncodeOptions),
];
Expand All @@ -106,14 +103,8 @@ public function persist(Message ...$messages): void
implode("),\n(", $insertValues),
);

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

try {
$this->connection->executeStatement($insertQuery, $insertParameters, $types);
$this->connection->executeStatement($insertQuery, $insertParameters);
} catch (Throwable $exception) {
throw UnableToPersistMessages::dueTo('', $exception);
}
Expand All @@ -133,7 +124,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()), $this->uuidEncoder instanceof BinaryUuidEncoder ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()));

try {
/** @var ResultStatement $resultStatement */
Expand All @@ -153,7 +144,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()), $this->uuidEncoder instanceof BinaryUuidEncoder ? ParameterType::BINARY : ParameterType::STRING);
$builder->setParameter('aggregate_root_id', $this->uuidEncoder->encodeString($id->toString()));
$builder->setParameter('version', $aggregateRootVersion);

try {
Expand Down