Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

fix(db): change type of notification_number column dev-21.10.x #11863

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions www/class/centreonDB.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,47 @@ public function isColumnExist(string $table = null, string $column = null): int
return -1;
}
}

/**
* Write SQL errors messages and queries
*
* @param string $query the query string to write to log
* @param string $message the message to write to log
*/
private function logSqlError(string $query, string $message): void
{
$this->log->insertLog(2, $message . " QUERY : " . $query);
}

/**
* This method returns a column type from a given table and column.
*
* @param string $tableName
* @param string $columnName
* @return string
*/
public function getColumnType(string $tableName, string $columnName): string
{
$query = 'SELECT COLUMN_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = :dbName
AND TABLE_NAME = :tableName
AND COLUMN_NAME = :columnName';

$stmt = $this->prepare($query);

try {
$stmt->bindValue(':dbName', $this->dsn['database'], \PDO::PARAM_STR);
$stmt->bindValue(':tableName', $tableName, \PDO::PARAM_STR);
$stmt->bindValue(':columnName', $columnName, \PDO::PARAM_STR);
$stmt->execute();
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
if (! empty($result)) {
return $result['COLUMN_TYPE'];
}
throw new \PDOException("Unable to get column type");
} catch (\PDOException $e) {
$this->logSqlError($query, $e->getMessage());
}
}
}
4 changes: 2 additions & 2 deletions www/install/installBroker.sql
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ CREATE TABLE `hosts` (
`notes` varchar(512) DEFAULT NULL,
`notes_url` varchar(2048) DEFAULT NULL,
`notification_interval` double DEFAULT NULL,
`notification_number` smallint(6) DEFAULT NULL,
`notification_number` bigint(20) unsigned DEFAULT NULL,
`notification_period` varchar(75) DEFAULT NULL,
`notify` tinyint(1) DEFAULT NULL,
`notify_on_down` tinyint(1) DEFAULT NULL,
Expand Down Expand Up @@ -591,7 +591,7 @@ CREATE TABLE `services` (
`notes` varchar(512) DEFAULT NULL,
`notes_url` varchar(2048) DEFAULT NULL,
`notification_interval` double DEFAULT NULL,
`notification_number` smallint(6) DEFAULT NULL,
`notification_number` bigint(20) unsigned DEFAULT NULL,
`notification_period` varchar(75) DEFAULT NULL,
`notify` tinyint(1) DEFAULT NULL,
`notify_on_critical` tinyint(1) DEFAULT NULL,
Expand Down
49 changes: 49 additions & 0 deletions www/install/php/Update-21.10.11.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* Copyright 2005 - 2022 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/

require_once __DIR__ . '/../../class/centreonLog.class.php';
$centreonLog = new CentreonLog();

//error specific content
$versionOfTheUpgrade = 'UPGRADE - 21.10.11: ';
$errorMessage = '';

try {
$errorMessage = "Impossible to update 'hosts' table";
if (! str_contains(strtolower($pearDBO->getColumnType('hosts', 'notification_number')), 'bigint')) {
$pearDBO->query("ALTER TABLE `hosts` MODIFY `notification_number` BIGINT(20) UNSIGNED DEFAULT NULL");
}

$errorMessage = "Impossible to update 'services' table";
if (! str_contains(strtolower($pearDBO->getColumnType('services', 'notification_number')), 'bigint')) {
$pearDBO->query("ALTER TABLE `services` MODIFY `notification_number` BIGINT(20) UNSIGNED DEFAULT NULL");
}
} catch (\Exception $e) {
$centreonLog->insertLog(
4,
$versionOfTheUpgrade . $errorMessage .
" - Code : " . (int)$e->getCode() .
" - Error : " . $e->getMessage() .
" - Trace : " . $e->getTraceAsString()
);

throw new \Exception($versionOfTheUpgrade . $errorMessage, (int) $e->getCode(), $e);
}