Skip to content

Commit

Permalink
TASK: Provide transparent conflict when attempting to publish legacy …
Browse files Browse the repository at this point in the history
…copy nodes
  • Loading branch information
mhsdesign committed Jan 29, 2025
1 parent 422805c commit 6a15b6d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Neos\ContentRepository\Core\Feature\NodeCreation\Command\CreateNodeAggregateWithNodeAndSerializedProperties;
use Neos\ContentRepository\Core\Feature\NodeDisabling\Command\DisableNodeAggregate;
use Neos\ContentRepository\Core\Feature\NodeDisabling\Command\EnableNodeAggregate;
use Neos\ContentRepository\Core\Feature\NodeDuplication\Command\CopyNodesRecursively;
use Neos\ContentRepository\Core\Feature\NodeModification\Command\SetSerializedNodeProperties;
use Neos\ContentRepository\Core\Feature\NodeMove\Command\MoveNodeAggregate;
use Neos\ContentRepository\Core\Feature\NodeReferencing\Command\SetSerializedNodeReferences;
Expand All @@ -24,7 +23,7 @@
use Neos\ContentRepository\Core\Feature\SubtreeTagging\Command\TagSubtree;
use Neos\ContentRepository\Core\Feature\SubtreeTagging\Command\UntagSubtree;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateIds;
use Neos\EventStore\Model\EventStream\EventStreamInterface;
use Neos\EventStore\Model\EventEnvelope;

/**
* @internal
Expand All @@ -43,11 +42,14 @@ public function __construct(
$this->items = $items;
}

public static function extractFromEventStream(EventStreamInterface $eventStream): self
/**
* @param iterable<EventEnvelope> $eventStream
*/
public static function extractFromEventStream(iterable $eventStream): self
{
$commands = [];
foreach ($eventStream as $eventEnvelope) {
if ($eventEnvelope->event->metadata && isset($eventEnvelope->event->metadata?->value['commandClass'])) {
if (isset($eventEnvelope->event->metadata?->value['commandClass'])) {
$commands[] = RebaseableCommand::extractFromEventEnvelope($eventEnvelope);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
use Neos\ContentRepository\Core\Feature\WorkspacePublication\Event\WorkspaceWasPublished;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Command\RebaseWorkspace;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\ConflictingEvent;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\ConflictingEvents;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Dto\RebaseErrorHandlingStrategy;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Event\WorkspaceWasRebased;
use Neos\ContentRepository\Core\Feature\WorkspaceRebase\Exception\PartialWorkspaceRebaseFailed;
Expand All @@ -67,6 +68,7 @@
use Neos\EventStore\Exception\ConcurrencyException;
use Neos\EventStore\Model\Event\SequenceNumber;
use Neos\EventStore\Model\Event\Version;
use Neos\EventStore\Model\EventEnvelope;
use Neos\EventStore\Model\EventStream\EventStreamInterface;
use Neos\EventStore\Model\EventStream\ExpectedVersion;

Expand Down Expand Up @@ -199,10 +201,10 @@ private function handlePublishWorkspace(
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($baseWorkspace, $commandHandlingDependencies);

$rebaseableCommands = RebaseableCommands::extractFromEventStream(
$this->eventStore->load(
$this->triggerConflictForLegacyEvents($this->eventStore->load(
ContentStreamEventStreamName::fromContentStreamId($workspace->currentContentStreamId)
->getEventStreamName()
)
), dropLegacyEvents: false)
);

yield $this->closeContentStream(
Expand Down Expand Up @@ -369,10 +371,10 @@ private function handleRebaseWorkspace(
}

$rebaseableCommands = RebaseableCommands::extractFromEventStream(
$this->eventStore->load(
$this->triggerConflictForLegacyEvents($this->eventStore->load(
ContentStreamEventStreamName::fromContentStreamId($workspace->currentContentStreamId)
->getEventStreamName()
)
), dropLegacyEvents: $command->rebaseErrorHandlingStrategy === RebaseErrorHandlingStrategy::STRATEGY_FORCE)
);

yield $this->closeContentStream(
Expand Down Expand Up @@ -432,6 +434,35 @@ static function ($handle) use ($rebaseableCommands): void {
yield $this->removeContentStreamWithoutConstraintChecks($workspace->currentContentStreamId);
}

/**
* This layer can be removed if there are no legacy events expected during rebasing
* @return iterable<EventEnvelope>
*/
private function triggerConflictForLegacyEvents(EventStreamInterface $eventStream, bool $dropLegacyEvents): iterable
{
foreach ($eventStream as $eventEnvelope) {
if (($eventEnvelope->event->metadata?->value['commandClass'] ?? '') === 'Neos\\ContentRepository\\Core\\Feature\\NodeDuplication\\Command\\CopyNodesRecursively') {
// The original draft of the CopyNodesRecursively command was removed. In case events that are created via that command are attempted to be
// normally rebased, published, or discarded we manually cause a conflict to ensure that the event is removed.
if ($dropLegacyEvents === true) {
// a forced rebase drops the legacy events
continue;
}
$originalEvent = $this->eventNormalizer->denormalize($eventEnvelope->event);
throw WorkspaceRebaseFailed::duringRebase(
new ConflictingEvents(
new ConflictingEvent(
$originalEvent,
throw new \RuntimeException('The legacy command CopyNodesRecursively was removed. Its not possible to rebase this event and it must be dropped.', 1738139237),
$eventEnvelope->sequenceNumber
),
)
);
}
yield $eventEnvelope;
}
}

/**
* This method is like a combined Rebase and Publish!
*
Expand All @@ -452,10 +483,10 @@ private function handlePublishIndividualNodesFromWorkspace(
$baseWorkspaceContentStreamVersion = $this->requireOpenContentStreamAndVersion($baseWorkspace, $commandHandlingDependencies);

$rebaseableCommands = RebaseableCommands::extractFromEventStream(
$this->eventStore->load(
$this->triggerConflictForLegacyEvents($this->eventStore->load(
ContentStreamEventStreamName::fromContentStreamId($workspace->currentContentStreamId)
->getEventStreamName()
)
), dropLegacyEvents: false)
);

[$matchingCommands, $remainingCommands] = $rebaseableCommands->separateMatchingAndRemainingCommands($command->nodesToPublish);
Expand Down

0 comments on commit 6a15b6d

Please sign in to comment.