Skip to content

Commit

Permalink
Merge pull request #4890 from mhsdesign/task/workspaceAwareCommands-e…
Browse files Browse the repository at this point in the history
…vents-migration

TASK: Workspace aware commands events migration
  • Loading branch information
ahaeslich authored Mar 15, 2024
2 parents fe5c14b + 8cd2f1d commit 2a14f73
Show file tree
Hide file tree
Showing 19 changed files with 165 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Feature: Constraint checks on SetNodeReferences
| sourceNodeAggregateId | "source-nodandaise" |
| referenceName | "referenceProperty" |
| references | [{"target":"anthony-destinode"}] |
Then the last command should have thrown an exception of type "ContentStreamDoesNotExistYet" with code 1521386692
Then the last command should have thrown an exception of type "ContentStreamDoesNotExistYet" with code 1710407870

# checks for sourceNodeAggregateId
Scenario: Try to reference nodes in a non-existent node aggregate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,10 @@ protected function requireContentStream(
WorkspaceName $workspaceName,
ContentRepository $contentRepository
): ContentStreamId {
$contentStreamId = ContentStreamIdOverride::$contentStreamIdToUse
?: $contentRepository->getWorkspaceFinder()->findOneByName($workspaceName)?->currentContentStreamId;
if (!$contentStreamId || !$contentRepository->getContentStreamFinder()->hasContentStream($contentStreamId)) {
$contentStreamId = ContentStreamIdOverride::resolveContentStreamIdForWorkspace($contentRepository, $workspaceName);
if (!$contentRepository->getContentStreamFinder()->hasContentStream($contentStreamId)) {
throw new ContentStreamDoesNotExistYet(
'Content stream "' . $contentStreamId?->value . '" does not exist yet.',
'Content stream "' . $contentStreamId->value . '" does not exist yet.',
1521386692
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,54 @@

namespace Neos\ContentRepository\Core\Feature\Common;

use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\Feature\WorkspaceCommandHandler;
use Neos\ContentRepository\Core\SharedModel\Exception\ContentStreamDoesNotExistYet;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\ContentRepository\Core\SharedModel\Workspace\WorkspaceName;

/**
* @internal (slightly hacky) implementation details for the workspace command handler
*/
class ContentStreamIdOverride
{
/**
* A content stream id that to use instead of the workspace one's {@see ConstraintChecks::requireContentStream()}
* A content stream id that is used instead of the workspace one's {@see ConstraintChecks::requireContentStream()}
*/
public static ?ContentStreamId $contentStreamIdToUse = null;
private static ?ContentStreamId $contentStreamIdToUse = null;

/**
* Makes the given content stream id available to be used in the given function {@see WorkspaceCommandHandler::handleRebaseWorkspace()}
* @internal
*/
public static function useContentStreamId(?ContentStreamId $contentStreamIdToUse): void
public static function applyContentStreamIdToClosure(ContentStreamId $contentStreamIdToUse, \Closure $fn): void
{
if (self::$contentStreamIdToUse !== null) {
throw new \Exception('Recursive content stream override is not supported', 1710426945);
}
self::$contentStreamIdToUse = $contentStreamIdToUse;
try {
$fn();
} finally {
self::$contentStreamIdToUse = null;
}
}

/**
* @internal
*/
public static function resolveContentStreamIdForWorkspace(ContentRepository $contentRepository, WorkspaceName $workspaceName): ContentStreamId
{
$contentStreamId = self::$contentStreamIdToUse
?: $contentRepository->getWorkspaceFinder()->findOneByName($workspaceName)?->currentContentStreamId;

if (!$contentStreamId) {
throw new ContentStreamDoesNotExistYet(
'Content stream for workspace "' . $workspaceName->value . '" does not exist yet.',
1710407870
);
}

return $contentStreamId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public function jsonSerialize(): array
public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return (
$this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
$this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
&& $this->originDimensionSpacePoint->equals($nodeIdToPublish->dimensionSpacePoint)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function jsonSerialize(): array
public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return (
$this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->coveredDimensionSpacePoint === $nodeIdToPublish->dimensionSpacePoint
$this->coveredDimensionSpacePoint === $nodeIdToPublish->dimensionSpacePoint
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function jsonSerialize(): array
public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return (
$this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->coveredDimensionSpacePoint === $nodeIdToPublish->dimensionSpacePoint
$this->coveredDimensionSpacePoint === $nodeIdToPublish->dimensionSpacePoint
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
);
return (
!is_null($targetNodeAggregateId)
&& $this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->targetDimensionSpacePoint->equals($nodeIdToPublish->dimensionSpacePoint)
&& $targetNodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public function jsonSerialize(): array
public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return (
$this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->originDimensionSpacePoint->equals($nodeIdToPublish->dimensionSpacePoint)
$this->originDimensionSpacePoint->equals($nodeIdToPublish->dimensionSpacePoint)
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,7 @@ public function jsonSerialize(): array

public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return $this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
return $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
&& $this->dimensionSpacePoint === $nodeIdToPublish->dimensionSpacePoint;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ public function jsonSerialize(): array

public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return (
$this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->sourceOriginDimensionSpacePoint->equals($nodeIdToPublish->dimensionSpacePoint)
return ($this->sourceOriginDimensionSpacePoint->equals($nodeIdToPublish->dimensionSpacePoint)
&& $this->sourceNodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public function jsonSerialize(): array
public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return (
$this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
$this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
&& $this->coveredDimensionSpacePoint === $nodeIdToPublish->dimensionSpacePoint
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ public function jsonSerialize(): array

public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return (
$this->workspaceName->equals($nodeIdToPublish->workspaceName)
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
);
return $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId);
}

public function createCopyForWorkspace(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ public static function fromArray(array $array): self

public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return $this->workspaceName === $nodeIdToPublish->workspaceName
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId);
return $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ public function jsonSerialize(): array

public function matchesNodeId(NodeIdToPublishOrDiscard $nodeIdToPublish): bool
{
return $this->workspaceName->equals($nodeIdToPublish->workspaceName)
&& $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
return $this->nodeAggregateId->equals($nodeIdToPublish->nodeAggregateId)
&& $this->targetOrigin->equals($nodeIdToPublish->dimensionSpacePoint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private function handleRebaseWorkspace(
// - extract the commands from the to-be-rebased content stream; and applies them on the new content stream
$originalCommands = $this->extractCommandsFromContentStreamMetadata($workspaceContentStreamName);
$rebaseStatistics = new WorkspaceRebaseStatistics();
$this->withContentStreamIdToUse(
ContentStreamIdOverride::applyContentStreamIdToClosure(
$command->rebasedContentStreamId,
function () use ($originalCommands, $contentRepository, $rebaseStatistics, $workspaceContentStreamName, $baseWorkspace): void {
foreach ($originalCommands as $i => $originalCommand) {
Expand Down Expand Up @@ -531,7 +531,7 @@ private function handlePublishIndividualNodesFromWorkspace(

try {
// 4) using the new content stream, apply the matching commands
$this->withContentStreamIdToUse(
ContentStreamIdOverride::applyContentStreamIdToClosure(
$command->contentStreamIdForMatchingPart,
function () use ($matchingCommands, $contentRepository, $baseWorkspace, $command): void {
foreach ($matchingCommands as $matchingCommand) {
Expand Down Expand Up @@ -565,7 +565,7 @@ function () use ($matchingCommands, $contentRepository, $baseWorkspace, $command
)->block();

// 7) apply REMAINING commands to the workspace's new content stream
$this->withContentStreamIdToUse(
ContentStreamIdOverride::applyContentStreamIdToClosure(
$command->contentStreamIdForRemainingPart,
function () use ($contentRepository, $remainingCommands) {
foreach ($remainingCommands as $remainingCommand) {
Expand Down Expand Up @@ -668,7 +668,7 @@ private function handleDiscardIndividualNodesFromWorkspace(

// 4) using the new content stream, apply the commands to keep
try {
$this->withContentStreamIdToUse(
ContentStreamIdOverride::applyContentStreamIdToClosure(
$command->newContentStreamId,
function () use ($commandsToKeep, $contentRepository, $baseWorkspace, $command): void {
foreach ($commandsToKeep as $matchingCommand) {
Expand Down Expand Up @@ -990,19 +990,4 @@ private function hasEventsInContentStreamExceptForking(

return false;
}

private function withContentStreamIdToUse(ContentStreamId $contentStreamIdToUse, callable $function): void
{
if (ContentStreamIdOverride::$contentStreamIdToUse !== null) {
throw new \Exception('Recursive content stream override is not supported');
}
ContentStreamIdOverride::useContentStreamId($contentStreamIdToUse);
try {
$function();
} catch (\Exception $exception) {
ContentStreamIdOverride::useContentStreamId(null);
throw $exception;
}
ContentStreamIdOverride::useContentStreamId(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
final readonly class NodeIdToPublishOrDiscard implements \JsonSerializable
{
public function __construct(
/** @todo do we really need this? should be unique per command, not per node in the command */
public WorkspaceName $workspaceName,
public NodeAggregateId $nodeAggregateId,
public DimensionSpacePoint $dimensionSpacePoint,
) {
Expand All @@ -43,7 +41,6 @@ public function __construct(
public static function fromArray(array $array): self
{
return new self(
WorkspaceName::fromString($array['workspaceName']),
NodeAggregateId::fromString($array['nodeAggregateId']),
DimensionSpacePoint::fromArray($array['dimensionSpacePoint']),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
*
* Needed for #4322: https://github.com/neos/neos-development-collection/pull/4322
*
* Included in February 2023 - before final Neos 9.0 release
* Included in February 2024 - before final Neos 9.0 release
*
* @param string $contentRepository Identifier of the Content Repository to migrate
*/
Expand All @@ -32,4 +32,20 @@ public function migratePropertiesToUnsetCommand(string $contentRepository = 'def
$eventMigrationService = $this->contentRepositoryRegistry->buildService($contentRepositoryId, $this->eventMigrationServiceFactory);
$eventMigrationService->migratePropertiesToUnset($this->outputLine(...));
}

/**
* Adds a dummy workspace name to the events meta-data, so it can be rebased
*
* Needed for #4708: https://github.com/neos/neos-development-collection/pull/4708
*
* Included in March 2024 - before final Neos 9.0 release
*
* @param string $contentRepository Identifier of the Content Repository to migrate
*/
public function migrateMetaDataToWorkspaceNameCommand(string $contentRepository = 'default'): void
{
$contentRepositoryId = ContentRepositoryId::fromString($contentRepository);
$eventMigrationService = $this->contentRepositoryRegistry->buildService($contentRepositoryId, $this->eventMigrationServiceFactory);
$eventMigrationService->migrateMetaDataToWorkspaceName($this->outputLine(...));
}
}
Loading

0 comments on commit 2a14f73

Please sign in to comment.