Skip to content

Commit

Permalink
feat(install): stronger upgrade for unsigned columns
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed May 15, 2023
1 parent a4bf10a commit 030f93e
Showing 1 changed file with 152 additions and 1 deletion.
153 changes: 152 additions & 1 deletion install/install.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,14 @@ public function upgrade(Migration $migration, $args = []): bool {
}
}

// Check schema of tables before upgrading
$oldVersion = Config::getConfigurationValue('formcreator', 'previous_version');
// Force fix of signed columns to reduce upgrade errors frequency
// This assumes that all modified columns exist in the database
if (version_compare($oldVersion, '2.13.0') >= 0) {
$this->migrateFkToUnsignedInt();
}

// Check schema of tables before upgrading
if (!isset($args['skip-db-check'])) {
if ($oldVersion !== null) {
$checkResult = true;
Expand Down Expand Up @@ -874,4 +880,149 @@ public function checkSchema(

return true;
}

/**
* Upgrade columns containing foreign keys to unsigned int
* picked from upgrade to 2.13.0, duplicated here to reduce upgrade errors
* when checking the DB schema.
*
* @return void
*/
protected function migrateFkToUnsignedInt() {
global $DB;

$table = 'glpi_plugin_formcreator_formanswers';
if ($DB->fieldExists($table, 'requester_id')) {
$DB->queryOrDie("UPDATE `$table` SET `requester_id` = 0 WHERE `requester_id` IS NULL");
}

$table = 'glpi_plugin_formcreator_targetchanges';
if ($DB->fieldExists($table, 'due_date_question')) {
$DB->queryOrDie("UPDATE `$table` SET `due_date_question` = 0 WHERE `due_date_question` IS NULL");
}
if ($DB->fieldExists($table, 'destination_entity_value')) {
$DB->queryOrDie("UPDATE `$table` SET `destination_entity_value` = 0 WHERE `destination_entity_value` IS NULL");
}
$table = 'glpi_plugin_formcreator_targettickets';
if ($DB->fieldExists($table, 'due_date_question')) {
$DB->queryOrDie("UPDATE `$table` SET `due_date_question` = 0 WHERE `due_date_question` IS NULL");
}
if ($DB->fieldExists($table, 'destination_entity_value')) {
$DB->queryOrDie("UPDATE `$table` SET `destination_entity_value` = 0 WHERE `destination_entity_value` IS NULL");
}
$table = 'glpi_plugin_formcreator_targets_actors';
if ($DB->fieldExists($table, 'actor_value')) {
$DB->queryOrDie("UPDATE `$table` SET `actor_value` = 0 WHERE `actor_value` IS NULL");
}

$tables = [
'glpi_plugin_formcreator_answers' => [
'plugin_formcreator_formanswers_id',
'plugin_formcreator_questions_id',
],
'glpi_plugin_formcreator_formanswers' => [
'plugin_formcreator_forms_id',
'requester_id',
'users_id_validator',
'groups_id_validator',
],
'glpi_plugin_formcreator_forms_languages' => [
'plugin_formcreator_forms_id',
],
'glpi_plugin_formcreator_forms_profiles' => [
'plugin_formcreator_forms_id',
'profiles_id',
],
'glpi_plugin_formcreator_forms_validators' => [
'plugin_formcreator_forms_id',
'items_id',
],
'glpi_plugin_formcreator_issues' => [
'users_id_recipient',
'plugin_formcreator_categories_id',
],
'glpi_plugin_formcreator_questions' => [
'plugin_formcreator_sections_id',
],
'glpi_plugin_formcreator_questiondependencies' => [
'plugin_formcreator_questions_id',
'plugin_formcreator_questions_id_2',
],
'glpi_plugin_formcreator_sections' => [
'plugin_formcreator_forms_id',
],
'glpi_plugin_formcreator_targetchanges' => [
'due_date_question',
'urgency_question',
'destination_entity_value',
'category_question',
'sla_question_tto',
'sla_question_ttr',
'ola_question_tto',
'ola_question_ttr',
],
'glpi_plugin_formcreator_targettickets' => [
'type_question',
'due_date_question',
'urgency_question',
'destination_entity_value',
'category_question',
'associate_question',
'location_question',
'sla_question_tto',
'sla_question_ttr',
'ola_question_tto',
'ola_question_ttr',
],
'glpi_plugin_formcreator_targets_actors' => [
'items_id',
'actor_value',
],
'glpi_plugin_formcreator_questionregexes' => [
'plugin_formcreator_questions_id',
],
'glpi_plugin_formcreator_questionranges' => [
'plugin_formcreator_questions_id',
],
];

foreach ($tables as $table => $fields) {
if (!$DB->tableExists($table)) {
continue;
}
foreach ($fields as $field) {
$type = 'INT ' . DBConnection::getDefaultPrimaryKeySignOption() . ' NOT NULL';
if ($field == 'id') {
$type .= ' AUTO_INCREMENT';
} else {
$type .= ' DEFAULT 0';
}
if (!$DB->fieldExists($table, $field)) {
continue;
}
$this->migration->changeField($table, $field, $field, $type);
}
}

$table = 'glpi_plugin_formcreator_entityconfigs';
if ($DB->tableExists($table)) {
$rows = $DB->request([
'COUNT' => 'c',
'FROM' => $table,
'WHERE' => ['id' => 0]
]);
$count = $rows !== null ?$rows->current()['c'] : null;
if ($count !== null) {
if ($count == 1) {
$rows = $DB->request([
'SELECT' => ['MAX' => 'id AS max_id'],
'FROM' => $table,
]);
$newId = (int) ($rows->current()['max_id'] + 1);
$DB->query("UPDATE `$table` SET `id`='$newId' WHERE `id` = 0");
}
}
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null auto_increment');
}
}
}

0 comments on commit 030f93e

Please sign in to comment.