From eb7bc9984ffa8948cb67b5ce51a0e7dd346f5747 Mon Sep 17 00:00:00 2001 From: Kevin Duret Date: Thu, 7 Oct 2021 11:17:28 +0200 Subject: [PATCH] fix(upgrade): avoid transaction when autocommit is done (#10256) Refs: MON-10926 --- www/install/php/Update-20.10.8.php | 2 -- www/install/php/Update-21.04.0-beta.1.php | 2 -- www/install/php/Update-21.04.1.php | 10 ++------ www/install/php/Update-21.04.2.php | 10 ++------ www/install/php/Update-21.04.5.php | 11 --------- www/install/php/Update-21.04.6.php | 10 ++++---- www/install/php/Update-21.04.7.php | 2 -- www/install/php/Update-21.10.0-beta.1.php | 28 +++++++++++------------ 8 files changed, 23 insertions(+), 52 deletions(-) diff --git a/www/install/php/Update-20.10.8.php b/www/install/php/Update-20.10.8.php index fdf71ccc994..f1349826747 100644 --- a/www/install/php/Update-20.10.8.php +++ b/www/install/php/Update-20.10.8.php @@ -25,8 +25,6 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 20.10.8 : '; -$pearDB = new CentreonDB('centreon', 3, false); - /** * Query with transaction */ diff --git a/www/install/php/Update-21.04.0-beta.1.php b/www/install/php/Update-21.04.0-beta.1.php index 22884d2536a..0efa1a1f4be 100644 --- a/www/install/php/Update-21.04.0-beta.1.php +++ b/www/install/php/Update-21.04.0-beta.1.php @@ -25,8 +25,6 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 21.04.0-beta.1: '; -$pearDB = new CentreonDB('centreon', 3, false); - $criteriasConcordanceArray = [ 'states' => [ 'acknowledged' => 'Acknowledged', diff --git a/www/install/php/Update-21.04.1.php b/www/install/php/Update-21.04.1.php index a68a5330687..0975e2f38d4 100644 --- a/www/install/php/Update-21.04.1.php +++ b/www/install/php/Update-21.04.1.php @@ -25,8 +25,6 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 21.04.1: '; -$pearDB = new CentreonDB('centreon', 3, false); - /** * Query with transaction */ @@ -73,13 +71,9 @@ $statement->execute(); } - if ($pearDB->inTransaction()) { - $pearDB->commit(); - } + $pearDB->commit(); } catch (\Exception $e) { - if ($pearDB->inTransaction()) { - $pearDB->rollBack(); - } + $pearDB->rollBack(); $centreonLog->insertLog( 4, $versionOfTheUpgrade . $errorMessage . diff --git a/www/install/php/Update-21.04.2.php b/www/install/php/Update-21.04.2.php index cbb1330d9bd..81f6f5628ec 100644 --- a/www/install/php/Update-21.04.2.php +++ b/www/install/php/Update-21.04.2.php @@ -25,8 +25,6 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 21.04.2: '; -$pearDB = new CentreonDB('centreon', 3, false); - /** * Query with transaction */ @@ -57,13 +55,9 @@ $statement->execute(); } - if ($pearDB->inTransaction()) { - $pearDB->commit(); - } + $pearDB->commit(); } catch (\Exception $e) { - if ($pearDB->inTransaction()) { - $pearDB->rollBack(); - } + $pearDB->rollBack(); $centreonLog->insertLog( 4, $versionOfTheUpgrade . $errorMessage . diff --git a/www/install/php/Update-21.04.5.php b/www/install/php/Update-21.04.5.php index 2dccd0cbe41..2955fd05f80 100644 --- a/www/install/php/Update-21.04.5.php +++ b/www/install/php/Update-21.04.5.php @@ -25,28 +25,17 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 21.04.5:'; -$pearDB = new CentreonDB('centreon', 3, false); - /** * Query with transaction */ try { - $pearDB->beginTransaction(); - $errorMessage = 'Impossible to alter the table contact'; if (!$pearDB->isColumnExist('contact', 'contact_platform_data_sending')) { $pearDB->query( "ALTER TABLE `contact` ADD COLUMN `contact_platform_data_sending` ENUM('0', '1', '2')" ); } - - if ($pearDB->inTransaction()) { - $pearDB->commit(); - } } catch (\Exception $e) { - if ($pearDB->inTransaction()) { - $pearDB->rollBack(); - } $centreonLog->insertLog( 4, $versionOfTheUpgrade . $errorMessage . diff --git a/www/install/php/Update-21.04.6.php b/www/install/php/Update-21.04.6.php index be7a3fa9dd6..2a7858b94a1 100644 --- a/www/install/php/Update-21.04.6.php +++ b/www/install/php/Update-21.04.6.php @@ -25,8 +25,6 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 21.04.6:'; -$pearDB = new CentreonDB('centreon', 3, false); - /** * Query with transaction */ @@ -40,6 +38,8 @@ $errorMessage = 'Impossible to purge the table ws_token'; $pearDB->query("DELETE FROM `ws_token`"); + $pearDB->commit(); + $constraintStatement = $pearDB->query( "SELECT COUNT(*) as count FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME='session_ibfk_1'" ); @@ -55,10 +55,10 @@ if ($pearDB->isColumnExist('contact', 'contact_platform_data_sending')) { $pearDB->query("ALTER TABLE `contact` DROP COLUMN `contact_platform_data_sending`"); } - - $pearDB->commit(); } catch (\Exception $e) { - $pearDB->rollBack(); + if ($pearDB->inTransaction()) { + $pearDB->rollBack(); + } $centreonLog->insertLog( 4, $versionOfTheUpgrade . $errorMessage . diff --git a/www/install/php/Update-21.04.7.php b/www/install/php/Update-21.04.7.php index b0a8d12a760..1e20d3f2e85 100644 --- a/www/install/php/Update-21.04.7.php +++ b/www/install/php/Update-21.04.7.php @@ -25,8 +25,6 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 21.04.7: '; -$pearDB = new CentreonDB('centreon', 3, false); - /** * Query with transaction */ diff --git a/www/install/php/Update-21.10.0-beta.1.php b/www/install/php/Update-21.10.0-beta.1.php index 232a9672ed8..1aa6b11c443 100644 --- a/www/install/php/Update-21.10.0-beta.1.php +++ b/www/install/php/Update-21.10.0-beta.1.php @@ -25,8 +25,6 @@ //error specific content $versionOfTheUpgrade = 'UPGRADE - 21.10.0-beta.1: '; -$pearDB = new CentreonDB('centreon', 3, false); - /** * Query with transaction */ @@ -37,17 +35,6 @@ $errorMessage = 'Impossible to purge the table session'; $pearDB->query("DELETE FROM `session`"); - $constraintStatement = $pearDB->query( - "SELECT COUNT(*) as count FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME='session_ibfk_1'" - ); - if (($constraint = $constraintStatement->fetch()) && (int) $constraint['count'] === 0) { - $errorMessage = 'Impossible to add Delete Cascade constraint on the table session'; - $pearDB->query( - "ALTER TABLE `session` ADD CONSTRAINT `session_ibfk_1` FOREIGN KEY (`user_id`) " . - "REFERENCES `contact` (`contact_id`) ON DELETE CASCADE" - ); - } - // Add TLS hostname in config brocker for input/outputs IPV4 $statement = $pearDB->query("SELECT cb_field_id from cb_field WHERE fieldname = 'tls_hostname'"); if ($statement->fetchColumn() === false) { @@ -73,8 +60,21 @@ } $pearDB->commit(); + + $constraintStatement = $pearDB->query( + "SELECT COUNT(*) as count FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_NAME='session_ibfk_1'" + ); + if (($constraint = $constraintStatement->fetch()) && (int) $constraint['count'] === 0) { + $errorMessage = 'Impossible to add Delete Cascade constraint on the table session'; + $pearDB->query( + "ALTER TABLE `session` ADD CONSTRAINT `session_ibfk_1` FOREIGN KEY (`user_id`) " . + "REFERENCES `contact` (`contact_id`) ON DELETE CASCADE" + ); + } } catch (\Exception $e) { - $pearDB->rollBack(); + if ($pearDB->inTransaction()) { + $pearDB->rollBack(); + } $centreonLog->insertLog( 4, $versionOfTheUpgrade . $errorMessage .