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

FEATURE: DBAL 3.x compatibility #17

Merged
merged 1 commit into from
Jan 19, 2024
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"require": {
"php": "^8.1",
"neos/eventstore": "^1",
"doctrine/dbal": "^2",
"doctrine/dbal": "^3",
"webmozart/assert": "^1.10",
"psr/clock": "^1"
},
Expand Down
22 changes: 12 additions & 10 deletions src/DoctrineEventStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Doctrine\DBAL\Exception\DeadlockException;
use Doctrine\DBAL\Exception\LockWaitTimeoutException;
use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
Expand Down Expand Up @@ -164,29 +165,30 @@ public function setup(): void
*/
private function determineRequiredSqlStatements(): array
{
$schemaManager = $this->connection->getSchemaManager();
$schemaManager = $this->connection->createSchemaManager();
assert($schemaManager !== null);
$platform = $this->connection->getDatabasePlatform();
assert($platform !== null);
if (!$schemaManager->tablesExist($this->eventTableName)) {
return $platform->getCreateTableSQL($this->createEventStoreSchema($schemaManager)->getTable($this->eventTableName));
}
$tableSchema = $schemaManager->listTableDetails($this->eventTableName);
$tableSchema = $schemaManager->introspectTable($this->eventTableName);
$fromSchema = new Schema([$tableSchema], [], $schemaManager->createSchemaConfig());
$schemaDiff = (new Comparator())->compare($fromSchema, $this->createEventStoreSchema($schemaManager));
return $schemaDiff->toSaveSql($platform);
$schemaDiff = (new Comparator())->compareSchemas($fromSchema, $this->createEventStoreSchema($schemaManager));
return $platform->getAlterSchemaSQL($schemaDiff);
}

// ----------------------------------

/**
* Creates the Doctrine schema to be compared with the current db schema for migration
*
* @param AbstractSchemaManager<AbstractPlatform> $schemaManager
* @return Schema
*/
private function createEventStoreSchema(AbstractSchemaManager $schemaManager): Schema
{
$isSQLite = $schemaManager->getDatabasePlatform() instanceof SqlitePlatform;
$isSQLite = $this->connection->getDatabasePlatform() instanceof SqlitePlatform;
$table = new Table($this->eventTableName, [
// The monotonic sequence number
(new Column('sequencenumber', Type::getType($isSQLite ? Types::INTEGER : Types::BIGINT)))
Expand All @@ -196,7 +198,7 @@ private function createEventStoreSchema(AbstractSchemaManager $schemaManager): S
// The stream name, usually in the format "<BoundedContext>:<StreamName>"
(new Column('stream', Type::getType(Types::STRING)))
->setLength(StreamName::MAX_LENGTH)
->setCustomSchemaOptions($isSQLite ? [] : ['charset' => 'ascii']),
->setPlatformOptions($isSQLite ? [] : ['charset' => 'ascii']),

// Version of the event in the respective stream
(new Column('version', Type::getType($isSQLite ? Types::INTEGER : Types::BIGINT)))
Expand All @@ -205,7 +207,7 @@ private function createEventStoreSchema(AbstractSchemaManager $schemaManager): S
// The event type, often in the format "<BoundedContext>:<EventType>"
(new Column('type', Type::getType(Types::STRING)))
->setLength(EventType::MAX_LENGTH)
->setCustomSchemaOptions($isSQLite ? [] : ['charset' => 'ascii']),
->setPlatformOptions($isSQLite ? [] : ['charset' => 'ascii']),

// The event payload, usually stored as JSON
(new Column('payload', Type::getType(Types::TEXT))),
Expand All @@ -218,19 +220,19 @@ private function createEventStoreSchema(AbstractSchemaManager $schemaManager): S
(new Column('id', Type::getType(Types::STRING)))
->setFixed(true)
->setLength(EventId::MAX_LENGTH)
->setCustomSchemaOptions($isSQLite ? [] : ['charset' => 'ascii']),
->setPlatformOptions($isSQLite ? [] : ['charset' => 'ascii']),

// An optional causation id, usually a UUID
(new Column('causationid', Type::getType(Types::STRING)))
->setNotnull(false)
->setLength(CausationId::MAX_LENGTH)
->setCustomSchemaOptions($isSQLite ? [] : ['charset' => 'ascii']),
->setPlatformOptions($isSQLite ? [] : ['charset' => 'ascii']),

// An optional correlation id, usually a UUID
(new Column('correlationid', Type::getType(Types::STRING)))
->setNotnull(false)
->setLength(CorrelationId::MAX_LENGTH)
->setCustomSchemaOptions($isSQLite ? [] : ['charset' => 'ascii']),
->setPlatformOptions($isSQLite ? [] : ['charset' => 'ascii']),

// Timestamp of the event publishing
(new Column('recordedat', Type::getType(Types::DATETIME_IMMUTABLE))),
Expand Down
3 changes: 1 addition & 2 deletions src/DoctrineEventStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public function getIterator(): \Traversable

$this->reconnectDatabaseConnection();

/** @var Result<array<string, string>> $result */
$result = $queryBuilder->execute();
$result = $queryBuilder->executeQuery();
/** @var array<string, string> $row */
foreach ($result->fetchAllAssociative() as $row) {
$recordedAt = \DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $row['recordedat']);
Expand Down