Skip to content

Commit

Permalink
fix(install): move error messages, find schema file with unstable ver…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
btry committed Jul 18, 2022
1 parent 859443f commit cc88698
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 28 deletions.
79 changes: 54 additions & 25 deletions install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
}

Expand Down
10 changes: 7 additions & 3 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit cc88698

Please sign in to comment.