From dd744395c77732e53b81077f6fbf14786dc4e4cc Mon Sep 17 00:00:00 2001 From: Sukhwinder Dhillon Date: Mon, 13 Jan 2025 11:29:53 +0100 Subject: [PATCH] Keep compatibility with Icinga DB mysql v6 & pgsql v4 --- application/controllers/ServiceController.php | 2 +- library/Icingadb/Common/Backend.php | 21 +++++++++++++++++++ library/Icingadb/Model/Host.php | 4 ++-- library/Icingadb/Model/HostState.php | 2 +- library/Icingadb/Model/Service.php | 6 +++--- library/Icingadb/Model/ServiceState.php | 2 +- library/Icingadb/Model/State.php | 2 +- .../Icingadb/Widget/Detail/ObjectDetail.php | 2 +- 8 files changed, 31 insertions(+), 10 deletions(-) diff --git a/application/controllers/ServiceController.php b/application/controllers/ServiceController.php index 25f56b72..921c1df5 100644 --- a/application/controllers/ServiceController.php +++ b/application/controllers/ServiceController.php @@ -54,7 +54,7 @@ class ServiceController extends Controller Filter::equal('host.name', $hostName) )); - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $query->withColumns(['has_problematic_parent']); } diff --git a/library/Icingadb/Common/Backend.php b/library/Icingadb/Common/Backend.php index 42aca17b..908fea0d 100644 --- a/library/Icingadb/Common/Backend.php +++ b/library/Icingadb/Common/Backend.php @@ -29,6 +29,9 @@ final class Backend /** @var ?IcingaRedis */ private static $redis; + /** @var ?bool Whether the current Icinga DB version supports dependencies */ + private static $supportsDependencies; + /** * Set the connection to the Icinga DB * @@ -165,4 +168,22 @@ final class Backend return self::$redis; } + + /** + * Whether the current Icinga DB version supports dependencies + * + * @return bool + */ + public static function supportsDependencies(): bool + { + if (self::$supportsDependencies === null) { + if (self::getDb()->getAdapter() instanceof Pgsql) { + self::$supportsDependencies = self::getDbSchemaVersion() >= 5; + } else { + self::$supportsDependencies = self::getDbSchemaVersion() >= 7; + } + } + + return self::$supportsDependencies; + } } diff --git a/library/Icingadb/Model/Host.php b/library/Icingadb/Model/Host.php index de17d903..58d703cb 100644 --- a/library/Icingadb/Model/Host.php +++ b/library/Icingadb/Model/Host.php @@ -116,7 +116,7 @@ class Host extends Model 'command_endpoint_id' ]; - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $columns[] = 'affected_children'; } @@ -166,7 +166,7 @@ class Host extends Model 'command_endpoint_id' => t('Endpoint Id') ]; - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $columns['affected_children'] = t('Affected Children'); } diff --git a/library/Icingadb/Model/HostState.php b/library/Icingadb/Model/HostState.php index fffe4817..724765ab 100644 --- a/library/Icingadb/Model/HostState.php +++ b/library/Icingadb/Model/HostState.php @@ -57,7 +57,7 @@ class HostState extends State 'next_update' => t('Host Next Update') ]; - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $columns['affects_children'] = t('Host Affects Children'); } diff --git a/library/Icingadb/Model/Service.php b/library/Icingadb/Model/Service.php index 8836ebee..103b72a3 100644 --- a/library/Icingadb/Model/Service.php +++ b/library/Icingadb/Model/Service.php @@ -109,7 +109,7 @@ class Service extends Model 'command_endpoint_id' ]; - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $columns[] = 'affected_children'; } @@ -156,7 +156,7 @@ class Service extends Model 'command_endpoint_id' => t('Endpoint Id'), ]; - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $columns['affected_children'] = t('Affected Children'); } @@ -207,7 +207,7 @@ class Service extends Model 'command_endpoint_id' ])); - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $behaviors->add(new HasProblematicParent()); } } diff --git a/library/Icingadb/Model/ServiceState.php b/library/Icingadb/Model/ServiceState.php index 195fe281..c7f899b7 100644 --- a/library/Icingadb/Model/ServiceState.php +++ b/library/Icingadb/Model/ServiceState.php @@ -59,7 +59,7 @@ class ServiceState extends State 'next_update' => t('Service Next Update') ]; - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $columns['affects_children'] = t('Service Affects Children'); } diff --git a/library/Icingadb/Model/State.php b/library/Icingadb/Model/State.php index acd11d81..b4ec2765 100644 --- a/library/Icingadb/Model/State.php +++ b/library/Icingadb/Model/State.php @@ -103,7 +103,7 @@ abstract class State extends Model 'next_update' ]; - if (Backend::getDbSchemaVersion() >= 6) { + if (Backend::supportsDependencies()) { $columns[] = 'affects_children'; } diff --git a/library/Icingadb/Widget/Detail/ObjectDetail.php b/library/Icingadb/Widget/Detail/ObjectDetail.php index c13d7851..5b657425 100644 --- a/library/Icingadb/Widget/Detail/ObjectDetail.php +++ b/library/Icingadb/Widget/Detail/ObjectDetail.php @@ -619,7 +619,7 @@ class ObjectDetail extends BaseHtmlElement */ protected function createRootProblems(): ?array { - if (Backend::getDbSchemaVersion() < 6) { + if (! Backend::supportsDependencies()) { if ($this->object->state->is_reachable) { return null; }