Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide the ability to change the table name of the SingleTableStore #31

Merged
merged 3 commits into from
Jan 29, 2021
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
21 changes: 11 additions & 10 deletions src/Store/SingleTableStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

final class SingleTableStore extends DoctrineStore
{
private const TABLE_NAME = 'eventstore';

/** @var array<class-string<AggregateRoot>, string> */
private array $aggregates;
private string $tableName;

/**
* @param array<class-string<AggregateRoot>, string> $aggregates
*/
public function __construct(Connection $connection, array $aggregates)
public function __construct(Connection $connection, array $aggregates, string $tableName)
{
parent::__construct($connection);

$this->aggregates = $aggregates;
$this->tableName = $tableName;
}

/**
Expand All @@ -42,7 +42,7 @@ public function load(string $aggregate, string $id, int $fromPlayhead = -1): arr

$sql = $this->connection->createQueryBuilder()
->select('*')
->from(self::TABLE_NAME)
->from($this->tableName)
->where('aggregate = :aggregate AND aggregateId = :id AND playhead > :playhead')
->getSQL();

Expand Down Expand Up @@ -75,7 +75,7 @@ public function loadAll(): Generator
{
$sql = $this->connection->createQueryBuilder()
->select('*')
->from(self::TABLE_NAME)
->from($this->tableName)
->getSQL();

$result = $this->connection->executeQuery($sql, []);
Expand All @@ -98,7 +98,7 @@ public function has(string $aggregate, string $id): bool

$sql = $this->connection->createQueryBuilder()
->select('COUNT(*)')
->from(self::TABLE_NAME)
->from($this->tableName)
->where('aggregate = :aggregate AND aggregateId = :id')
->setMaxResults(1)
->getSQL();
Expand All @@ -118,7 +118,7 @@ public function count(): int
{
$sql = $this->connection->createQueryBuilder()
->select('COUNT(*)')
->from(self::TABLE_NAME)
->from($this->tableName)
->getSQL();

return (int)$this->connection->fetchOne($sql);
Expand All @@ -131,9 +131,10 @@ public function count(): int
public function saveBatch(string $aggregate, string $id, array $events): void
{
$shortName = $this->shortName($aggregate);
$tableName = $this->tableName;

$this->connection->transactional(
static function (Connection $connection) use ($shortName, $id, $events): void {
static function (Connection $connection) use ($shortName, $id, $events, $tableName): void {
foreach ($events as $event) {
if ($event->aggregateId() !== $id) {
throw new StoreException('id missmatch');
Expand All @@ -143,7 +144,7 @@ static function (Connection $connection) use ($shortName, $id, $events): void {
$data['aggregate'] = $shortName;

$connection->insert(
self::TABLE_NAME,
$tableName,
$data,
[
'recordedOn' => Types::DATETIMETZ_IMMUTABLE,
Expand All @@ -164,7 +165,7 @@ public function schema(): Schema

private function addTableToSchema(Schema $schema): void
{
$table = $schema->createTable(self::TABLE_NAME);
$table = $schema->createTable($this->tableName);

$table->addColumn('id', Types::BIGINT)
->setAutoincrement(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public function testSuccessful(): void

$store = new SingleTableStore(
$this->connection,
[Profile::class => 'profile']
[Profile::class => 'profile'],
'eventstore'
);

$repository = new Repository($store, $eventStream, Profile::class);
Expand Down Expand Up @@ -100,7 +101,8 @@ public function testWithSymfonySuccessful(): void

$store = new SingleTableStore(
$this->connection,
[Profile::class => 'profile']
[Profile::class => 'profile'],
'eventstore'
);

$repository = new Repository($store, $eventStream, Profile::class);
Expand Down Expand Up @@ -172,7 +174,8 @@ public function testSnapshot(): void

$store = new SingleTableStore(
$this->connection,
[Profile::class => 'profile']
[Profile::class => 'profile'],
'eventstore'
);

$snapshotStore = new InMemorySnapshotStore();
Expand Down