N°8981: ModuleInstallationRepository dedicated to module installation queries

N°8981: ModuleInstallationRepository dedicated to module installation queries

fix renaming
This commit is contained in:
odain
2026-01-07 19:57:06 +01:00
parent e55bbf728b
commit 9768ffb19d
8 changed files with 62 additions and 62 deletions

View File

@@ -139,49 +139,6 @@ EOF;
return $aValues;
}
/**
* Return previous module installation. offset is applied on parent_id.
* @param $iOffset
* @return array
*/
public static function GetPreviousModuleInstallationsByOffset(int $iOffset = 0): array
{
$oFilter = DBObjectSearch::FromOQL('SELECT ModuleInstallation AS mi WHERE mi.parent_id=0 AND mi.name!="datamodel"');
$oSet = new DBObjectSet($oFilter, ['installed' => false]); // Most recent first
$oSet->SetLimit($iOffset + 1);
$iParentId = 0;
/** @var \DBObject $oModuleInstallation */
while ($oModuleInstallation = $oSet->Fetch()) {
if ($iOffset == 0) {
$iParentId = $oModuleInstallation->Get('id');
break;
}
$iOffset--;
}
if ($iParentId === 0) {
IssueLog::Error("no ITOP_APPLICATION ModuleInstallation found", null, ['offset' => $iOffset]);
throw new \Exception("no ITOP_APPLICATION ModuleInstallation found");
}
$oFilter = DBObjectSearch::FromOQL("SELECT ModuleInstallation AS mi WHERE mi.id=$iParentId OR mi.parent_id=$iParentId");
$oSet = new DBObjectSet($oFilter); // Most recent first
$aRawValues = $oSet->ToArrayOfValues();
$aValues = [];
foreach ($aRawValues as $aRawValue) {
$aValue = [];
foreach ($aRawValue as $sAliasAttCode => $sValue) {
// remove 'mi.' from AttCode
$sAttCode = substr($sAliasAttCode, 3);
$aValue[$sAttCode] = $sValue;
}
$aValues[] = $aValue;
}
return $aValues;
}
public static function GetDBTablesInfo()
{
self::AnalyzeTables();

View File

@@ -186,7 +186,7 @@ function collect_configuration()
// iTop modules
$oConfig = MetaModel::GetConfig();
$aInstalledModules = ModuleInstallationService::GetInstance()->ReadFromDB($oConfig);
$aInstalledModules = ModuleInstallationRepository::GetInstance()->ReadFromDB($oConfig);
foreach ($aInstalledModules as $aDBInfo) {
$aConfiguration['itop_modules'][$aDBInfo['name']] = $aDBInfo['version'];

View File

@@ -1,6 +1,6 @@
<?php
require_once __DIR__.'/ModuleInstallationService.php';
require_once __DIR__.'/ModuleInstallationRepository.php';
class AnalyzeInstallation
{
@@ -97,7 +97,7 @@ class AnalyzeInstallation
$aRes[$sModuleName] = $aModuleInfo;
}
$aCurrentlyInstalledModules = ModuleInstallationService::GetInstance()->ReadComputeInstalledModules($oConfig);
$aCurrentlyInstalledModules = ModuleInstallationRepository::GetInstance()->ReadComputeInstalledModules($oConfig);
// Adjust the list of proposed modules
foreach ($aCurrentlyInstalledModules as $sModuleName => $aModuleDB) {

View File

@@ -1,23 +1,23 @@
<?php
class ModuleInstallationService
class ModuleInstallationRepository
{
private static ModuleInstallationService $oInstance;
private static ModuleInstallationRepository $oInstance;
protected function __construct()
{
}
final public static function GetInstance(): ModuleInstallationService
final public static function GetInstance(): ModuleInstallationRepository
{
if (!isset(self::$oInstance)) {
self::$oInstance = new ModuleInstallationService();
self::$oInstance = new ModuleInstallationRepository();
}
return self::$oInstance;
}
final public static function SetInstance(?ModuleInstallationService $oInstance): void
final public static function SetInstance(?ModuleInstallationRepository $oInstance): void
{
static::$oInstance = $oInstance;
}
@@ -181,4 +181,47 @@ SQL;
return $aInstallByModule;
}
/**
* Return previous module installation. offset is applied on parent_id.
* @param $iOffset: by default (offset=0) returns current installation
* @return array
*/
public static function GetPreviousModuleInstallationsByOffset(int $iOffset = 0): array
{
$oFilter = DBObjectSearch::FromOQL('SELECT ModuleInstallation AS mi WHERE mi.parent_id=0 AND mi.name!="datamodel"');
$oSet = new DBObjectSet($oFilter, ['installed' => false]); // Most recent first
$oSet->SetLimit($iOffset + 1);
$iParentId = 0;
/** @var \DBObject $oModuleInstallation */
while ($oModuleInstallation = $oSet->Fetch()) {
if ($iOffset == 0) {
$iParentId = $oModuleInstallation->Get('id');
break;
}
$iOffset--;
}
if ($iParentId === 0) {
IssueLog::Error("no ITOP_APPLICATION ModuleInstallation found", null, ['offset' => $iOffset]);
throw new \Exception("no ITOP_APPLICATION ModuleInstallation found");
}
$oFilter = DBObjectSearch::FromOQL("SELECT ModuleInstallation AS mi WHERE mi.id=$iParentId OR mi.parent_id=$iParentId");
$oSet = new DBObjectSet($oFilter); // Most recent first
$aRawValues = $oSet->ToArrayOfValues();
$aValues = [];
foreach ($aRawValues as $aRawValue) {
$aValue = [];
foreach ($aRawValue as $sAliasAttCode => $sValue) {
// remove 'mi.' from AttCode
$sAttCode = substr($sAliasAttCode, 3);
$aValue[$sAttCode] = $sValue;
}
$aValues[] = $aValue;
}
return $aValues;
}
}

View File

@@ -630,7 +630,7 @@ class RunTimeEnvironment
public function GetApplicationVersion(Config $oConfig)
{
try {
$aSelectInstall = ModuleInstallationService::GetInstance()->ReadFromDB($oConfig);
$aSelectInstall = ModuleInstallationRepository::GetInstance()->ReadFromDB($oConfig);
} catch (MySQLException $e) {
// No database or erroneous information
$this->log_error('Can not connect to the database: host: '.$oConfig->Get('db_host').', user:'.$oConfig->Get('db_user').', pwd:'.$oConfig->Get('db_pwd').', db name:'.$oConfig->Get('db_name'));

View File

@@ -46,7 +46,7 @@ class HubControllerTest extends ItopDataTestCase
$this->testLaunchCompile();
HubController::GetInstance()->LaunchDeploy();
$this->CheckReport('{"code":0,"message":"Compilation successful.","fields":[]}');
$this->CompareCurrentAndPreviousModuleInstallations();
$this->AssertPreviousAndCurrentInstallationAreEquivalent();
}
private function CheckReport($sExpected)

View File

@@ -35,6 +35,7 @@ use lnkContactToTicket;
use lnkFunctionalCIToTicket;
use MetaModel;
use MissingQueryArgument;
use ModuleInstallationRepository;
use MySQLException;
use MySQLHasGoneAwayException;
use Person;
@@ -1558,15 +1559,14 @@ abstract class ItopDataTestCase extends ItopTestCase
@unlink($this->sConfigTmpBackupFile);
}
public function CompareCurrentAndPreviousModuleInstallations()
public function AssertPreviousAndCurrentInstallationAreEquivalent()
{
$this->RequireOnceItopFile('env-production/combodo-db-tools/src/Service/DBToolsUtils.php');
$aPreviousInstallations = DBToolsUtils::GetPreviousModuleInstallationsByOffset(1);
$aInstallations = DBToolsUtils::GetPreviousModuleInstallationsByOffset();
$this->assertEquals($this->KeepModuleInstallationComparableFields($aPreviousInstallations), $this->KeepModuleInstallationComparableFields($aInstallations));
$aPreviousInstallations = ModuleInstallationRepository::GetInstance()->GetPreviousModuleInstallationsByOffset(1);
$aInstallations = ModuleInstallationRepository::GetInstance()->GetPreviousModuleInstallationsByOffset();
$this->assertEquals($this->GetCanonicalComparableModuleInstallationArray($aPreviousInstallations), $this->GetCanonicalComparableModuleInstallationArray($aInstallations));
}
public function KeepModuleInstallationComparableFields($aInstallations): array
protected function GetCanonicalComparableModuleInstallationArray($aInstallations): array
{
$aRes = [];
$aIgnoredFields = ['id', 'parent_id', 'installed', 'comment'];

View File

@@ -4,7 +4,7 @@ namespace Combodo\iTop\Test\UnitTest\Setup;
use AnalyzeInstallation;
use Combodo\iTop\Test\UnitTest\ItopTestCase;
use ModuleInstallationService;
use ModuleInstallationRepository;
class AnalyzeInstallationTest extends ItopTestCase
{
@@ -12,7 +12,7 @@ class AnalyzeInstallationTest extends ItopTestCase
{
parent::setUp();
$this->RequireOnceItopFile('setup/AnalyzeInstallation.php');
$this->RequireOnceItopFile('setup/ModuleInstallationService.php');
$this->RequireOnceItopFile('setup/ModuleInstallationRepository.php');
$this->RequireOnceItopFile('setup/modulediscovery.class.inc.php');
$this->RequireOnceItopFile('setup/runtimeenv.class.inc.php');
}
@@ -151,7 +151,7 @@ class AnalyzeInstallationTest extends ItopTestCase
$this->SetNonPublicProperty(AnalyzeInstallation::GetInstance(), "aAvailableModules", $aAvailableModules);
//$aModules = json_decode(file_get_contents(__DIR__.'/ressources/priv_modules2.json'), true);
$this->SetNonPublicProperty(ModuleInstallationService::GetInstance(), "aSelectInstall", $aInstalledModules);
$this->SetNonPublicProperty(ModuleInstallationRepository::GetInstance(), "aSelectInstall", $aInstalledModules);
$oConfig = $this->createMock(\Config::class);