Skip to content

Commit cc88698

Browse files
committed
fix(install): move error messages, find schema file with unstable versions
1 parent 859443f commit cc88698

File tree

2 files changed

+61
-28
lines changed

2 files changed

+61
-28
lines changed

install/install.php

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,25 @@ public function upgrade(Migration $migration, $args = []): bool {
139139
}
140140
}
141141

142-
// Check schema of tables
142+
// Check schema of tables before upgrading
143143
if (!isset($args['skip-db-check'])) {
144144
$oldVersion = Config::getConfigurationValue('formcreator', 'previous_version');
145-
if ($oldVersion !== null && !$this->checkSchema($oldVersion)) {
145+
$log = '';
146+
if ($oldVersion !== null && !$this->checkSchema($oldVersion, $log)) {
147+
if (!isCommandLine()) {
148+
Session::addMessageAfterRedirect(sprintf(
149+
__('The database schema is not consistent with the installed Formcreator %s. To see the logs run the command %s', 'formcreator'),
150+
$oldVersion,
151+
'bin/console glpi:plugin:install formcreator'
152+
), false, ERROR);
153+
} else {
154+
echo sprintf(
155+
__('The database schema is not consistent with the installed Formcreator %s. To see the logs run the command %s', 'formcreator'),
156+
$oldVersion,
157+
'bin/console glpi:plugin:install formcreator'
158+
) . PHP_EOL;
159+
echo $log . PHP_EOL;
160+
}
146161
return false;
147162
}
148163
}
@@ -189,6 +204,26 @@ public function upgrade(Migration $migration, $args = []): bool {
189204
$task = new CronTask();
190205
PluginFormcreatorIssue::cronSyncIssues($task);
191206
}
207+
208+
// Check schema of tables after upgrade
209+
$log = '';
210+
if (!$this->checkSchema(PLUGIN_FORMCREATOR_VERSION, $log)) {
211+
if (!isCommandLine()) {
212+
Session::addMessageAfterRedirect(sprintf(
213+
__('The database schema is not consistent with the installed Formcreator %s. To see the logs enable the plugin and run the command %s', 'formcreator'),
214+
PLUGIN_FORMCREATOR_VERSION,
215+
'bin/console glpi:database:check_schema_integrity -p formcreator'
216+
), false, ERROR);
217+
} else {
218+
echo sprintf(
219+
__('The database schema is not consistent with the installed Formcreator %s. To see the logs run the command %s', 'formcreator'),
220+
PLUGIN_FORMCREATOR_VERSION,
221+
'bin/console glpi:database:check_schema_integrity -p formcreator'
222+
) . PHP_EOL;
223+
echo $log . PHP_EOL;
224+
}
225+
}
226+
192227
return true;
193228
}
194229

@@ -749,7 +784,7 @@ public function deleteMiniDashboard(): bool {
749784
*
750785
* @return boolean
751786
*/
752-
public function checkSchema(string $version): bool {
787+
public function checkSchema(string $version, string &$log = ''): bool {
753788
global $DB;
754789

755790
$schemaFile = plugin_formcreator_getSchemaPath($version);
@@ -777,28 +812,22 @@ public function checkSchema(string $version): bool {
777812
}
778813

779814
if (count($differences) > 0) {
780-
if (!isCommandLine()) {
781-
Session::addMessageAfterRedirect(sprintf(
782-
__('Inconsistencies detected in the database. To see the logs run the command %s', 'formcreator'),
783-
'bin/console glpi:plugin:install formcreator'
784-
), false, ERROR);
785-
}
786-
foreach ($differences as $table_name => $difference) {
787-
$message = null;
788-
switch ($difference['type']) {
789-
case DatabaseSchemaIntegrityChecker::RESULT_TYPE_ALTERED_TABLE:
790-
$message = sprintf(__('Table schema differs for table "%s".'), $table_name);
791-
break;
792-
case DatabaseSchemaIntegrityChecker::RESULT_TYPE_MISSING_TABLE:
793-
$message = sprintf(__('Table "%s" is missing.'), $table_name);
794-
break;
795-
case DatabaseSchemaIntegrityChecker::RESULT_TYPE_UNKNOWN_TABLE:
796-
$message = sprintf(__('Unknown table "%s" has been found in database.'), $table_name);
797-
break;
798-
}
799-
if (isCommandLine()) {
800-
echo $message . PHP_EOL;
801-
echo $difference['diff'] . PHP_EOL;
815+
if (isCommandLine()) {
816+
foreach ($differences as $table_name => $difference) {
817+
$message = null;
818+
switch ($difference['type']) {
819+
case DatabaseSchemaIntegrityChecker::RESULT_TYPE_ALTERED_TABLE:
820+
$message = sprintf(__('Table schema differs for table "%s".'), $table_name);
821+
break;
822+
case DatabaseSchemaIntegrityChecker::RESULT_TYPE_MISSING_TABLE:
823+
$message = sprintf(__('Table "%s" is missing.'), $table_name);
824+
break;
825+
case DatabaseSchemaIntegrityChecker::RESULT_TYPE_UNKNOWN_TABLE:
826+
$message = sprintf(__('Unknown table "%s" has been found in database.'), $table_name);
827+
break;
828+
}
829+
$log .= $message . PHP_EOL;
830+
$log .= $difference['diff'] . PHP_EOL;
802831
}
803832
}
804833

setup.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,13 @@ function plugin_formcreator_options() {
504504
*/
505505
function plugin_formcreator_getSchemaPath(string $version = null): ?string {
506506
if ($version === null) {
507-
$matches = [];
508-
preg_match('/^(\d+\.\d+\.\d+)/', PLUGIN_FORMCREATOR_VERSION, $matches);
509-
$version = $matches[1];
507+
$version = PLUGIN_FORMCREATOR_VERSION;
510508
}
509+
510+
// Drop suffixes for alpha, beta, rc versions
511+
$matches = [];
512+
preg_match('/^(\d+\.\d+\.\d+)/', PLUGIN_FORMCREATOR_VERSION, $matches);
513+
$version = $matches[1];
514+
511515
return Plugin::getPhpDir('formcreator') . "/install/mysql/plugin_formcreator_${version}_empty.sql";
512516
}

0 commit comments

Comments
 (0)