Skip to content

Commit

Permalink
Merge branch 'feature/db_schema_check_before_upgrade' into support/2.…
Browse files Browse the repository at this point in the history
…13.0
  • Loading branch information
btry committed Jul 20, 2022
2 parents 63bb428 + c9338cd commit 521f452
Show file tree
Hide file tree
Showing 72 changed files with 12,017 additions and 1,325 deletions.
1 change: 1 addition & 0 deletions inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
$item->update($input);
} else {
unset($input['id']);
$input['actor_value_' . $input['actor_type']] = $input['actor_value'];
$itemId = $item->add($input);
}
if ($itemId === false) {
Expand Down
138 changes: 126 additions & 12 deletions install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@
use Glpi\Dashboard\Dashboard;
use Glpi\Dashboard\Item as Dashboard_Item;
use Glpi\Dashboard\Right as Dashboard_Right;

use Glpi\System\Diagnostic\DatabaseSchemaIntegrityChecker;
use Ramsey\Uuid\Uuid;

class PluginFormcreatorInstall {
protected $migration;

Expand Down Expand Up @@ -78,7 +79,6 @@ class PluginFormcreatorInstall {
'2.12.5' => '2.13',
];


protected bool $resyncIssues = false;

/**
Expand Down Expand Up @@ -125,15 +125,55 @@ public function upgrade(Migration $migration, $args = []): bool {
}
if ($hasMyisamTables) {
// Need to convert myisam tables into innodb first
$message = sprintf(
__('Upgrade tables to innoDB; run %s', 'formcreator'),
'php bin/console glpi:migration:myisam_to_innodb'
);
if (isCommandLine()) {
echo "Upgrade tables to innoDB; run php bin/console glpi:migration:myisam_to_innodb" . PHP_EOL;
echo $message . PHP_EOL;
} else {
Session::addMessageAfterRedirect(__('Upgrade tables to innoDB; run php bin/console glpi:migration:myisam_to_innodb', 'formcreator'), false, ERROR);
Session::addMessageAfterRedirect($message, false, ERROR);
}
return false;
}
}

// Check schema of tables before upgrading
if (!isset($args['skip-db-check'])) {
$oldVersion = Config::getConfigurationValue('formcreator', 'previous_version');
if ($oldVersion !== null) {
$checkResult = true;
if (version_compare($oldVersion, '2.13.0') >= 0) {
$checkResult = $this->checkSchema(
$oldVersion,
false,
false,
false,
false,
false,
false
);
}
if (!$checkResult) {
$message = 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 -f'
);
if (!isCommandLine()) {
Session::addMessageAfterRedirect($message, false, ERROR);
} else {
echo $message . PHP_EOL;
echo sprintf(
__('To ignore the inconsistencies and upgrade anyway run %s', 'formcreator'),
'bin/console glpi:plugin:install formcreator -f -p skip-db-check'
) . PHP_EOL;
}
return false;
}
}
}

$this->migration = $migration;
if (isset($args['force-upgrade']) && $args['force-upgrade'] === true) {
// Might return false
Expand All @@ -143,28 +183,26 @@ public function upgrade(Migration $migration, $args = []): bool {
}

if (version_compare($fromSchemaVersion, '2.5') < 0) {
$message = __('Upgrade from version older than 2.5.0 is no longer supported. Please upgrade to GLPI 9.5.7, upgrade Formcreator to version 2.12.5, then upgrade again to GLPI 10 or later and Formcreator 2.13 or later.', 'formcreator');
if (isCommandLine()) {
echo 'Upgrade from version < 2.5.0 is no longer supported.' . PHP_EOL;
echo 'Please upgrade to GLPI 9.5, upgrade Formcreator to version 2.12,' . PHP_EOL;
echo 'then upgrade again to GLPI 10 or later and Formcreator 2.13 or later.' . PHP_EOL;
echo $message;
} else {
Session::addMessageAfterRedirect(
'Upgrade from version < 2.5.0 is no longer supported.<br>' .
'Please upgrade to GLPI 9.5, upgrade Formcreator to version 2.12,<br>' .
'then upgrade again to GLPI 10 or later and Formcreator 2.13 or later.',
$message,
true,
ERROR
);
}
return false;
}

ob_start();
while ($fromSchemaVersion && isset($this->upgradeSteps[$fromSchemaVersion])) {
$this->upgradeOneStep($this->upgradeSteps[$fromSchemaVersion]);
$fromSchemaVersion = $this->upgradeSteps[$fromSchemaVersion];
}

$this->migration->executeMigration();

// if the schema contains new tables
$this->installSchema();
$this->configureExistingEntities();
Expand All @@ -173,12 +211,41 @@ public function upgrade(Migration $migration, $args = []): bool {
$this->createCronTasks();
$this->createMiniDashboard();
Config::setConfigurationValues('formcreator', ['schema_version' => PLUGIN_FORMCREATOR_SCHEMA_VERSION]);
ob_get_flush();

if ($this->resyncIssues) {
// An upgrade step requires a resync of the issues
$task = new CronTask();
PluginFormcreatorIssue::cronSyncIssues($task);
}

// Check schema of tables after upgrade
$checkResult = $this->checkSchema(
PLUGIN_FORMCREATOR_VERSION,
false,
false,
false,
false,
false,
false
);
if (!$checkResult) {
$message = 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'
);
if (!isCommandLine()) {
Session::addMessageAfterRedirect($message, false, ERROR);
} else {
echo $message . PHP_EOL;
}
} else {
if (isCommandLine()) {
echo __('The tables of the plugin passed the schema integrity check.', 'formcreator') . PHP_EOL;
}
}

return true;
}

Expand Down Expand Up @@ -262,7 +329,7 @@ public function isPluginInstalled() {
protected function installSchema() {
global $DB;

$dbFile = __DIR__ . '/mysql/plugin_formcreator_empty.sql';
$dbFile = plugin_formcreator_getSchemaPath();
if (!$DB->runFile($dbFile)) {
$this->migration->displayWarning("Error creating tables : " . $DB->error(), true);
die('Giving up');
Expand Down Expand Up @@ -733,4 +800,51 @@ public function deleteMiniDashboard(): bool {

return true;
}

/**
* Check the schema of all tables of the plugin against the expected schema of the given version
*
* @return boolean
*/
public function checkSchema(
string $version,
bool $strict = true,
bool $ignore_innodb_migration = false,
bool $ignore_timestamps_migration = false,
bool $ignore_utf8mb4_migration = false,
bool $ignore_dynamic_row_format_migration = false,
bool $ignore_unsigned_keys_migration = false
): bool {
global $DB;

$schemaFile = plugin_formcreator_getSchemaPath($version);

$checker = new DatabaseSchemaIntegrityChecker(
$DB,
$strict,
$ignore_innodb_migration,
$ignore_timestamps_migration,
$ignore_utf8mb4_migration,
$ignore_dynamic_row_format_migration,
$ignore_unsigned_keys_migration
);

try {
$differences = $checker->checkCompleteSchema($schemaFile, true, 'plugin:formcreator');
} catch (\Throwable $e) {
$message = __('Failed to check the sanity of the tables!', 'formcreator');
if (isCommandLine()) {
echo $message . PHP_EOL;
} else {
Session::addMessageAfterRedirect($message, false, ERROR);
}
return false;
}

if (count($differences) > 0) {
return false;
}

return true;
}
}
Loading

0 comments on commit 521f452

Please sign in to comment.