Skip to content

Commit

Permalink
TASK: Rename sourceContentStreamVersion column to be more precise
Browse files Browse the repository at this point in the history
... and refactor status calculation logic. By leveraging `isSatisfiedBy` i found out the `>` comparison was not quite correct. We should use `===`.
  • Loading branch information
mhsdesign committed Oct 17, 2024
1 parent b2c53f8 commit 26e2de7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspaces;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceStatus;
use Neos\EventStore\Model\Event\Version;
use Neos\EventStore\Model\EventStream\ExpectedVersion;
use Neos\EventStore\Model\EventStream\MaybeVersion;

/**
* @internal only used inside the
Expand All @@ -55,11 +57,11 @@ public function findWorkspaceByName(WorkspaceName $workspaceName): ?Workspace
{
$workspaceByNameStatement = <<<SQL
SELECT
ws.name, ws.baseWorkspaceName, ws.currentContentStreamId, cs.sourceVersion as lastSourceVersion, scs.version as sourceVersion
ws.name, ws.baseWorkspaceName, ws.currentContentStreamId, cs.sourceContentStreamVersion as expectedSourceVersion, scs.version as sourceVersion
FROM
{$this->tableNames->workspace()} ws
JOIN {$this->tableNames->contentStream()} cs ON cs.id = ws.currentcontentstreamid
LEFT JOIN {$this->tableNames->contentStream()} scs ON cs.sourceContentStreamId = scs.id
LEFT JOIN {$this->tableNames->contentStream()} scs ON scs.id = cs.sourceContentStreamId
WHERE
ws.name = :workspaceName
LIMIT 1
Expand All @@ -81,11 +83,11 @@ public function findWorkspaces(): Workspaces
{
$workspacesStatement = <<<SQL
SELECT
ws.name, ws.baseWorkspaceName, ws.currentContentStreamId, cs.sourceVersion as lastSourceVersion, scs.version as sourceVersion
ws.name, ws.baseWorkspaceName, ws.currentContentStreamId, cs.sourceContentStreamVersion as expectedSourceVersion, scs.version as sourceVersion
FROM
{$this->tableNames->workspace()} ws
JOIN {$this->tableNames->contentStream()} cs ON cs.id = ws.currentcontentstreamid
LEFT JOIN {$this->tableNames->contentStream()} scs ON cs.sourceContentStreamId = scs.id
LEFT JOIN {$this->tableNames->contentStream()} scs ON scs.id = cs.sourceContentStreamId
SQL;
try {
$rows = $this->dbal->fetchAllAssociative($workspacesStatement);
Expand Down Expand Up @@ -140,16 +142,21 @@ public function findContentStreams(): ContentStreams
*/
private static function workspaceFromDatabaseRow(array $row): Workspace
{
if ($row['sourceVersion'] === null) {
$status = WorkspaceStatus::UP_TO_DATE;
} elseif ($row['sourceVersion'] > $row['lastSourceVersion']) {
$status = WorkspaceStatus::OUTDATED;
} else {
$status = WorkspaceStatus::UP_TO_DATE;
$baseWorkspaceName = $row['baseWorkspaceName'] !== null ? WorkspaceName::fromString($row['baseWorkspaceName']) : null;

$status = WorkspaceStatus::UP_TO_DATE;
if ($baseWorkspaceName !== null) {
// root workspaces can never be out of date and dont have a source content stream
$sourceVersion = MaybeVersion::fromVersionOrNull(Version::fromInteger($row['sourceVersion']));
$expectedSourceVersion = ExpectedVersion::fromVersion(Version::fromInteger($row['expectedSourceVersion']));

if (!$expectedSourceVersion->isSatisfiedBy($sourceVersion)) {
$status = WorkspaceStatus::OUTDATED;
}
}
return new Workspace(
WorkspaceName::fromString($row['name']),
isset($row['baseWorkspaceName']) ? WorkspaceName::fromString($row['baseWorkspaceName']) : null,
$baseWorkspaceName,
ContentStreamId::fromString($row['currentContentStreamId']),
$status,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ private function createContentStreamTable(): Table
DbalSchemaFactory::columnForContentStreamId('id')->setNotnull(true),
(new Column('version', Type::getType(Types::INTEGER)))->setNotnull(true),
DbalSchemaFactory::columnForContentStreamId('sourceContentStreamId')->setNotnull(false),
(new Column('sourceContentStreamVersion', Type::getType(Types::INTEGER)))->setNotnull(false),
// Should become a DB ENUM (unclear how to configure with DBAL) or int (latter needs adaption to code)
(new Column('status', Type::getType(Types::BINARY)))->setLength(20)->setNotnull(true),
(new Column('removed', Type::getType(Types::BOOLEAN)))->setDefault(false)->setNotnull(false),
(new Column('sourceVersion', Type::getType(Types::INTEGER)))->setNotnull(false),
]);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ private function createContentStream(ContentStreamId $contentStreamId, ContentSt
{
$this->dbal->insert($this->tableNames->contentStream(), [
'id' => $contentStreamId->value,
'sourceContentStreamId' => $sourceContentStreamId?->value,
'version' => 0,
'sourceVersion' => $sourceVersion?->value,
'sourceContentStreamId' => $sourceContentStreamId?->value,
'sourceContentStreamVersion' => $sourceVersion?->value,
'status' => $status->value,
]);
}
Expand Down

0 comments on commit 26e2de7

Please sign in to comment.