diff --git a/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php b/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php index b047e17e4..668e9f2ba 100644 --- a/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php +++ b/library/Director/DirectorObject/Automation/BasketSnapshotFieldResolver.php @@ -2,6 +2,7 @@ namespace Icinga\Module\Director\DirectorObject\Automation; +use Icinga\Module\Director\CustomVariable\CustomVariables; use Icinga\Module\Director\Db; use Icinga\Module\Director\Objects\DirectorDatafield; use Icinga\Module\Director\Objects\IcingaObject; @@ -61,6 +62,10 @@ public function storeNewFields() if ($field->hasBeenModified()) { $field->store(); $this->idMap[$id] = $field->get('id'); + // at this point the datafields from the basket are importet and stored in the database, so now we can rename the related custom variables, if the varname of the datafield changed during the import (see Datafield::import()) + if ($field->shouldBeRenamed()) { + CustomVariables::renameAll($field->getPreImportName(), $field->get('varname'), $this->targetDb); + } } } } diff --git a/library/Director/Objects/DirectorDatafield.php b/library/Director/Objects/DirectorDatafield.php index 12797cb39..8d603fe1f 100644 --- a/library/Director/Objects/DirectorDatafield.php +++ b/library/Director/Objects/DirectorDatafield.php @@ -21,6 +21,8 @@ class DirectorDatafield extends DbObjectWithSettings protected $uuidColumn = 'uuid'; protected $settingsTable = 'director_datafield_setting'; protected $settingsRemoteId = 'datafield_id'; + protected $shouldBeRenamed = false; + protected $preImportName = ''; protected $defaultProperties = [ 'id' => null, @@ -41,6 +43,15 @@ class DirectorDatafield extends DbObjectWithSettings private $category; private $object; + + public function shouldBeRenamed() { + return $this->shouldBeRenamed; + } + + public function getPreImportName() { + return $this->preImportName; + } + public static function fromDbRow($row, Db $connection) { $obj = static::create((array) $row, $connection); @@ -148,6 +159,11 @@ public static function import(stdClass $plain, Db $db): DirectorDatafield if ($candidate = DirectorDatafield::loadWithUniqueId($uuid, $db)) { self::fixOptionalDatalistReference($plain, $db); assert($candidate instanceof DirectorDatafield); + // setting the new shouldBeRenamed flag leads to all associated custom variables to be renamed later on (see BasketSnapshotFieldResolver->storeNewFields()) + if ($candidate->varname != $plain->varname) { + $candidate->shouldBeRenamed = true; + $candidate->preImportName = $candidate->varname; + } $candidate->setProperties((array) $plain); return $candidate; }