From cc88698555c5e744ef194d7202ef656fa94fdff1 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Mon, 4 Jul 2022 12:42:17 +0200 Subject: [PATCH] fix(install): move error messages, find schema file with unstable versions --- install/install.php | 79 +++++++++++++++++++++++++++++++-------------- setup.php | 10 ++++-- 2 files changed, 61 insertions(+), 28 deletions(-) diff --git a/install/install.php b/install/install.php index ac4aa9605..06fd64e81 100644 --- a/install/install.php +++ b/install/install.php @@ -139,10 +139,25 @@ public function upgrade(Migration $migration, $args = []): bool { } } - // Check schema of tables + // Check schema of tables before upgrading if (!isset($args['skip-db-check'])) { $oldVersion = Config::getConfigurationValue('formcreator', 'previous_version'); - if ($oldVersion !== null && !$this->checkSchema($oldVersion)) { + $log = ''; + if ($oldVersion !== null && !$this->checkSchema($oldVersion, $log)) { + if (!isCommandLine()) { + Session::addMessageAfterRedirect(sprintf( + __('The database schema is not consistent with the installed Formcreator %s. To see the logs run the command %s', 'formcreator'), + $oldVersion, + 'bin/console glpi:plugin:install formcreator' + ), false, ERROR); + } else { + echo sprintf( + __('The database schema is not consistent with the installed Formcreator %s. To see the logs run the command %s', 'formcreator'), + $oldVersion, + 'bin/console glpi:plugin:install formcreator' + ) . PHP_EOL; + echo $log . PHP_EOL; + } return false; } } @@ -189,6 +204,26 @@ public function upgrade(Migration $migration, $args = []): bool { $task = new CronTask(); PluginFormcreatorIssue::cronSyncIssues($task); } + + // Check schema of tables after upgrade + $log = ''; + if (!$this->checkSchema(PLUGIN_FORMCREATOR_VERSION, $log)) { + if (!isCommandLine()) { + Session::addMessageAfterRedirect(sprintf( + __('The database schema is not consistent with the installed Formcreator %s. To see the logs enable the plugin and run the command %s', 'formcreator'), + PLUGIN_FORMCREATOR_VERSION, + 'bin/console glpi:database:check_schema_integrity -p formcreator' + ), false, ERROR); + } else { + echo sprintf( + __('The database schema is not consistent with the installed Formcreator %s. To see the logs run the command %s', 'formcreator'), + PLUGIN_FORMCREATOR_VERSION, + 'bin/console glpi:database:check_schema_integrity -p formcreator' + ) . PHP_EOL; + echo $log . PHP_EOL; + } + } + return true; } @@ -749,7 +784,7 @@ public function deleteMiniDashboard(): bool { * * @return boolean */ - public function checkSchema(string $version): bool { + public function checkSchema(string $version, string &$log = ''): bool { global $DB; $schemaFile = plugin_formcreator_getSchemaPath($version); @@ -777,28 +812,22 @@ public function checkSchema(string $version): bool { } if (count($differences) > 0) { - if (!isCommandLine()) { - Session::addMessageAfterRedirect(sprintf( - __('Inconsistencies detected in the database. To see the logs run the command %s', 'formcreator'), - 'bin/console glpi:plugin:install formcreator' - ), false, ERROR); - } - foreach ($differences as $table_name => $difference) { - $message = null; - switch ($difference['type']) { - case DatabaseSchemaIntegrityChecker::RESULT_TYPE_ALTERED_TABLE: - $message = sprintf(__('Table schema differs for table "%s".'), $table_name); - break; - case DatabaseSchemaIntegrityChecker::RESULT_TYPE_MISSING_TABLE: - $message = sprintf(__('Table "%s" is missing.'), $table_name); - break; - case DatabaseSchemaIntegrityChecker::RESULT_TYPE_UNKNOWN_TABLE: - $message = sprintf(__('Unknown table "%s" has been found in database.'), $table_name); - break; - } - if (isCommandLine()) { - echo $message . PHP_EOL; - echo $difference['diff'] . PHP_EOL; + if (isCommandLine()) { + foreach ($differences as $table_name => $difference) { + $message = null; + switch ($difference['type']) { + case DatabaseSchemaIntegrityChecker::RESULT_TYPE_ALTERED_TABLE: + $message = sprintf(__('Table schema differs for table "%s".'), $table_name); + break; + case DatabaseSchemaIntegrityChecker::RESULT_TYPE_MISSING_TABLE: + $message = sprintf(__('Table "%s" is missing.'), $table_name); + break; + case DatabaseSchemaIntegrityChecker::RESULT_TYPE_UNKNOWN_TABLE: + $message = sprintf(__('Unknown table "%s" has been found in database.'), $table_name); + break; + } + $log .= $message . PHP_EOL; + $log .= $difference['diff'] . PHP_EOL; } } diff --git a/setup.php b/setup.php index 9d7c63f1b..0ae1e6fce 100644 --- a/setup.php +++ b/setup.php @@ -504,9 +504,13 @@ function plugin_formcreator_options() { */ function plugin_formcreator_getSchemaPath(string $version = null): ?string { if ($version === null) { - $matches = []; - preg_match('/^(\d+\.\d+\.\d+)/', PLUGIN_FORMCREATOR_VERSION, $matches); - $version = $matches[1]; + $version = PLUGIN_FORMCREATOR_VERSION; } + + // Drop suffixes for alpha, beta, rc versions + $matches = []; + preg_match('/^(\d+\.\d+\.\d+)/', PLUGIN_FORMCREATOR_VERSION, $matches); + $version = $matches[1]; + return Plugin::getPhpDir('formcreator') . "/install/mysql/plugin_formcreator_${version}_empty.sql"; }