Skip to content

Commit

Permalink
add automatic renaming of applied vars during basket imports
Browse files Browse the repository at this point in the history
  • Loading branch information
NavidSassan committed Apr 19, 2023
1 parent 4383139 commit 7c95a72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
16 changes: 16 additions & 0 deletions library/Director/Objects/DirectorDatafield.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 7c95a72

Please sign in to comment.