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

BUGFIX: do not crash when node already disabled/enabled #4284

Merged
merged 3 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Neos\ContentRepository\Core\ContentRepository;
use Neos\ContentRepository\Core\Projection\ProjectionInterface;
use Neos\ContentRepository\Core\Projection\ProjectionStateInterface;
use Neos\EventStore\Model\Event\Version;
use Neos\EventStore\Model\EventStore\CommitResult;
use Neos\EventStore\Model\Event\SequenceNumber;

Expand All @@ -25,6 +26,20 @@ public function __construct(
) {
}

/**
* an empty command result which should not result in projection updates
* @return self
*/
public static function empty(): self {
return new self(
PendingProjections::empty(),
new CommitResult(
Version::first(),
SequenceNumber::none()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a bit dangerous/misleading if consumers don't check for the pending projections properly and use the CommitResult to determine the progress of the ES.
Probably not a real issue, but it might be safer to use a union type EmptyCommandResult (or similar) instead?

)
);
}

/**
* Wait until all projections are up to date; i.e. have processed the events.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public function __construct(
) {
}

public static function empty(
): self {
bwaidelich marked this conversation as resolved.
Show resolved Hide resolved
return new self(Projections::create(), []);
}

public static function fromProjectionsAndEventsAndSequenceNumber(
Projections $allProjections,
Events $events,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Neos\EventStore\Model\Event\EventId;
use Neos\EventStore\Model\Event\EventMetadata;
use Neos\EventStore\Model\Events;
use Neos\EventStore\Model\EventStore\CommitResult;

/**
* Internal service to persist {@see EventInterface} with the proper normalization, and triggering the
Expand All @@ -39,6 +40,9 @@ public function __construct(
*/
public function publishEvents(EventsToPublish $eventsToPublish): CommandResult
{
if ($eventsToPublish->events->isEmpty()) {
return CommandResult::empty();
}
// the following logic could also be done in an AppEventStore::commit method (being called
// directly from the individual Command Handlers).
$normalizedEvents = Events::fromArray(
Expand Down
5 changes: 5 additions & 0 deletions Neos.ContentRepository.Core/Classes/EventStore/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function map(\Closure $callback): array
{
return array_map($callback, $this->events);
}

public function isEmpty(): bool
{
return empty($this->events);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,13 @@ public function __construct(
public readonly ExpectedVersion $expectedVersion,
) {
}

public static function empty(): self
{
return new EventsToPublish(
StreamName::fromString("empty"),
Events::fromArray([]),
ExpectedVersion::ANY()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -609,39 +609,6 @@ protected function requireNodeAggregateToNotOccupyDimensionSpacePoint(
}
}

/**
* @throws NodeAggregateCurrentlyDoesNotDisableDimensionSpacePoint
*/
protected function requireNodeAggregateToDisableDimensionSpacePoint(
NodeAggregate $nodeAggregate,
DimensionSpacePoint $dimensionSpacePoint
): void {
if (!$nodeAggregate->disablesDimensionSpacePoint($dimensionSpacePoint)) {
throw new NodeAggregateCurrentlyDoesNotDisableDimensionSpacePoint(
'Node aggregate "' . $nodeAggregate->nodeAggregateId->value
. '" currently does not disable dimension space point '
. json_encode($dimensionSpacePoint) . '.',
1557735431
);
}
}

/**
* @throws NodeAggregateCurrentlyDisablesDimensionSpacePoint
*/
protected function requireNodeAggregateToNotDisableDimensionSpacePoint(
NodeAggregate $nodeAggregate,
DimensionSpacePoint $dimensionSpacePoint
): void {
if ($nodeAggregate->disablesDimensionSpacePoint($dimensionSpacePoint)) {
throw new NodeAggregateCurrentlyDisablesDimensionSpacePoint(
'Node aggregate "' . $nodeAggregate->nodeAggregateId->value
. '" currently disables dimension space point ' . json_encode($dimensionSpacePoint) . '.',
1555179563
);
}
}

protected function validateReferenceProperties(
ReferenceName $referenceName,
PropertyValuesToWrite $referenceProperties,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ private function handleDisableNodeAggregate(
$nodeAggregate,
$command->coveredDimensionSpacePoint
);
$this->requireNodeAggregateToNotDisableDimensionSpacePoint(
$nodeAggregate,
$command->coveredDimensionSpacePoint
);

if ($nodeAggregate->disablesDimensionSpacePoint($command->coveredDimensionSpacePoint)) {
bwaidelich marked this conversation as resolved.
Show resolved Hide resolved
// already disabled, so we can return a no-operation.
return EventsToPublish::empty();
}

$affectedDimensionSpacePoints = $command->nodeVariantSelectionStrategy
->resolveAffectedDimensionSpacePoints(
Expand Down Expand Up @@ -113,10 +114,11 @@ public function handleEnableNodeAggregate(
$nodeAggregate,
$command->coveredDimensionSpacePoint
);
$this->requireNodeAggregateToDisableDimensionSpacePoint(
$nodeAggregate,
$command->coveredDimensionSpacePoint
);

if (!$nodeAggregate->disablesDimensionSpacePoint($command->coveredDimensionSpacePoint)) {
// already enabled, so we can return a no-operation.
return EventsToPublish::empty();
}

$affectedDimensionSpacePoints = $command->nodeVariantSelectionStrategy
->resolveAffectedDimensionSpacePoints(
Expand Down