Skip to content

Commit

Permalink
Sync: respect null properties on merge
Browse files Browse the repository at this point in the history
fixes #2623
  • Loading branch information
Thomas-Gelf committed Oct 7, 2022
1 parent cf924a2 commit 20c3114
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions library/Director/Import/Sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Sync
/** @var IcingaObject[] Objects to work with */
protected $objects;

/** @var array<mixed, array<int, string>> key => [property, property]*/
protected $setNull = [];

/** @var bool Whether we already prepared your sync */
protected $isPrepared = false;

Expand Down Expand Up @@ -513,7 +516,7 @@ protected function prepareNewObjects()
}

$object = $objects[$key];
$this->prepareNewObject($row, $object, $sourceId);
$this->prepareNewObject($row, $object, $key, $sourceId);
}
}

Expand All @@ -527,7 +530,7 @@ protected function prepareNewObjects()
* @throws \Icinga\Exception\NotFoundError
* @throws \Icinga\Module\Director\Exception\DuplicateKeyException
*/
protected function prepareNewObject($row, DbObject $object, $sourceId)
protected function prepareNewObject($row, DbObject $object, $objectKey, $sourceId)
{
foreach ($this->syncProperties as $propertyKey => $p) {
if ($p->get('source_id') !== $sourceId) {
Expand All @@ -539,7 +542,6 @@ protected function prepareNewObject($row, DbObject $object, $sourceId)
}

$prop = $p->get('destination_field');

$val = SyncUtils::fillVariables($p->get('source_expression'), $row);

if ($object instanceof IcingaObject) {
Expand All @@ -561,15 +563,23 @@ protected function prepareNewObject($row, DbObject $object, $sourceId)
$this->wantArray($val)
);
} else {
$object->vars()->$varName = $val;
if ($val === null) {
$this->setNull[$objectKey][] = $prop;
} else {
$object->vars()->$varName = $val;
}
}
} else {
if ($val !== null) {
if ($val === null) {
$this->setNull[$objectKey][] = $prop;
} else {
$object->set($prop, $val);
}
}
} else {
if ($val !== null) {
if ($val === null) {
$this->setNull[$objectKey][] = $prop;
} else {
$object->set($prop, $val);
}
}
Expand Down Expand Up @@ -776,6 +786,12 @@ protected function refreshObject($key, $object)
}
}
}

if (isset($this->setNull[$key])) {
foreach ($this->setNull[$key] as $property) {
$this->objects[$key]->set($property, null);
}
}
}

/**
Expand Down

0 comments on commit 20c3114

Please sign in to comment.