Skip to content

Commit

Permalink
TASK: Make atomic delete instead of deleting old references one by one
Browse files Browse the repository at this point in the history
  • Loading branch information
mhsdesign committed Oct 31, 2024
1 parent 4eee438 commit bc2427c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Neos\ContentGraph\DoctrineDbalAdapter;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception as DBALException;
use Neos\ContentGraph\DoctrineDbalAdapter\Domain\Projection\Feature\ContentStream;
Expand Down Expand Up @@ -71,6 +72,7 @@
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateClassification;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\ContentRepository\Core\SharedModel\Node\NodeName;
use Neos\ContentRepository\Core\SharedModel\Node\ReferenceName;
use Neos\ContentRepository\Core\SharedModel\Workspace\ContentStreamId;
use Neos\EventStore\Model\Event\SequenceNumber;
use Neos\EventStore\Model\EventEnvelope;
Expand Down Expand Up @@ -597,16 +599,26 @@ function (NodeRecord $node) use ($eventEnvelope) {
$event->contentStreamId
);


// remove old
foreach ($event->references->getReferenceNames() as $referenceName) {
try {
$this->dbal->delete($this->tableNames->referenceRelation(), [
$deleteOldReferencesSql = <<<SQL
DELETE FROM {$this->tableNames->referenceRelation()}
WHERE nodeanchorpoint = :nodeanchorpoint
AND name in (:names)
SQL;
try {
$this->dbal->executeStatement(
$deleteOldReferencesSql,
[
'nodeanchorpoint' => $nodeAnchorPoint?->value,
'name' => $referenceName->value
]);
} catch (DbalException $e) {
throw new \RuntimeException(sprintf('Failed to remove reference relation: %s', $e->getMessage()), 1716486309, $e);
}
'names' => array_map(fn (ReferenceName $name) => $name->value, $event->references->getReferenceNames())
],
[
'names' => ArrayParameterType::STRING
]
);
} catch (DbalException $e) {
throw new \RuntimeException(sprintf('Failed to remove reference relation: %s', $e->getMessage()), 1716486309, $e);
}

// set new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function (NodeRecord $node) {

$position = 0;
foreach ($event->references as $referencesForProperty) {
// TODO can't we turn this into two atomic queries?
$this->getDatabaseConnection()->delete($this->tableNamePrefix . '_referencerelation', [
'sourcenodeanchor' => $anchorPoint->value,
'name' => $referencesForProperty->referenceName->value
Expand Down

0 comments on commit bc2427c

Please sign in to comment.