Skip to content

Commit

Permalink
feat: convert foreign keys to unsigned integers
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Feb 14, 2022
1 parent fb267cf commit 4af002a
Show file tree
Hide file tree
Showing 10 changed files with 318 additions and 156 deletions.
2 changes: 1 addition & 1 deletion front/targetticket.form.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
if (!$targetTicket->canUpdateItem()) {
Session::addMessageAfterRedirect(__('No right to update this item.', 'formcreator'), false, ERROR);
} else {
$actor_value = $_POST['actor_value_' . $_POST['actor_type']] ?? null;
$actor_value = $_POST['actor_value_' . $_POST['actor_type']] ?? 0;
$use_notification = ($_POST['use_notification'] == 0) ? 0 : 1;
$targetTicket_actor = new PluginFormcreatorTarget_Actor();
$targetTicket_actor->add([
Expand Down
2 changes: 1 addition & 1 deletion inc/abstracttarget.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,7 @@ protected function setTargetDueDate($data, PluginFormcreatorFormAnswer $formansw
global $DB;

$answer = new PluginFormcreatorAnswer();
if ($this->fields['due_date_question'] !== null) {
if ($this->fields['due_date_question'] != 0) {
$request = [
'FROM' => $answer::getTable(),
'WHERE' => [
Expand Down
2 changes: 1 addition & 1 deletion inc/target_actor.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function prepareInputForAdd($input) {
return false;
}

$input['actor_value'] = $input['actor_value_' . $input['actor_type']] ?? null;
$input['actor_value'] = $input['actor_value_' . $input['actor_type']] ?? 0;

if (isset($input['use_notification'])) {
$input['use_notification'] = ($input['use_notification'] == 0) ? 0 : 1;
Expand Down
2 changes: 1 addition & 1 deletion inc/targetchange.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public function prepareInputForUpdate($input) {
unset($input['_destination_entity_value_entity']);
break;
default :
$input['destination_entity_value'] = 'NULL';
$input['destination_entity_value'] = 0;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion inc/targetproblem.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public function prepareInputForUpdate($input) {
unset($input['_destination_entity_value_entity']);
break;
default :
$input['destination_entity_value'] = 'NULL';
$input['destination_entity_value'] = 0;
break;
}
}
Expand Down
2 changes: 1 addition & 1 deletion inc/targetticket.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public function prepareInputForUpdate($input) {
$input['destination_entity_value'] = $input['_destination_entity_value_entity'];
break;
default :
$input['destination_entity_value'] = 'NULL';
$input['destination_entity_value'] = 0;
break;
}
}
Expand Down
294 changes: 147 additions & 147 deletions install/mysql/plugin_formcreator_empty.sql

Large diffs are not rendered by default.

164 changes: 163 additions & 1 deletion install/upgrade_to_2.13.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ class PluginFormcreatorUpgradeTo2_13 {
*/
public function upgrade(Migration $migration) {
$this->migration = $migration;
$this->migrateFkToUnsignedInt();
$this->addFormAnswerTitle();
$this->defaultValuesForTargets();
$this->migrateItemtypeInQuestion();
$this->fixInconsistency();
// $this->fixInconsistency();
$this->addTargetValidationSetting();
$this->addFormVisibility();
$this->addDashboardVisibility();
Expand Down Expand Up @@ -140,4 +141,165 @@ protected function addDashboardVisibility() {

$this->migration->addPostQuery("UPDATE glpi_plugin_formcreator_entityconfigs SET `is_dashboard_visible`=1 WHERE `id`=0");
}

protected function migrateFkToUnsignedInt() {
global $DB;

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

$table = 'glpi_plugin_formcreator_targetchanges';
$DB->queryOrDie("UPDATE `$table`, SET `due_date_question` = 0 WHERE `due_date_question` IS NULL");
$DB->queryOrDie("UPDATE `$table`, SET `destination_entity_value` = 0 WHERE `destination_entity_value` IS NULL");

$table = 'glpi_plugin_formcreator_targettickets';
$DB->queryOrDie("UPDATE `$table`, SET `due_date_question` = 0 WHERE `due_date_question` IS NULL");
$DB->queryOrDie("UPDATE `$table`, SET `destination_entity_value` = 0 WHERE `destination_entity_value` IS NULL");

$table = 'glpi_plugin_formcreator_targetproblems';
$DB->queryOrDie("UPDATE `$table`, SET `destination_entity_value` = 0 WHERE `destination_entity_value` IS NULL");

$table = 'glpi_plugin_formcreator_targets_actors';
$DB->queryOrDie("UPDATE `$table`, SET `actor_value` = 0 WHERE `actor_value` IS NULL");

$tables = [
'glpi_plugin_formcreator_answers' => [
'id',
'plugin_formcreator_formanswers_id',
'plugin_formcreator_questions_id',
],
'glpi_plugin_formcreator_categories' => [
'id',
'plugin_formcreator_categories_id',
'knowbaseitemcategories_id',
],
'glpi_plugin_formcreator_entityconfigs' => [
'plugin_formcreator_categories_id',
],
'glpi_plugin_formcreator_forms' => [
'id',
'entities_id',
'plugin_formcreator_categories_id',
],
'glpi_plugin_formcreator_formanswers' => [
'id',
'entities_id',
'plugin_formcreator_forms_id',
'requester_id',
'users_id_validator',
'groups_id_validator',
],
'glpi_plugin_formcreator_forms_profiles' => [
'id',
'plugin_formcreator_forms_id',
'profiles_id',
],
'glpi_plugin_formcreator_forms_validators' => [
'id',
'plugin_formcreator_forms_id',
'items_id',
],
'glpi_plugin_formcreator_questions' => [
'id',
'plugin_formcreator_sections_id',
],
'glpi_plugin_formcreator_conditions' => [
'id',
'items_id',
'plugin_formcreator_questions_id',
],
'glpi_plugin_formcreator_sections' => [
'id',
'plugin_formcreator_forms_id',
],
'glpi_plugin_formcreator_targetchanges' => [
'id',
'plugin_formcreator_forms_id',
'changetemplates_id',
'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' => [
'id',
'plugin_formcreator_forms_id',
'type_question',
'tickettemplates_id',
'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_targetproblems' => [
'id',
'plugin_formcreator_forms_id',
'problemtemplates_id',
'urgency_question',
'destination_entity_value',
'category_question',
],
'glpi_plugin_formcreator_targets_actors' => [
'id',
'items_id',
'actor_value',
],
'glpi_plugin_formcreator_issues' => [
'id',
'items_id',
'entities_id',
'requester_id',
'users_id_validator',
'groups_id_validator',
'users_id_recipient',
],
'glpi_plugin_formcreator_items_targettickets' => [
'id',
'plugin_formcreator_targettickets_id',
'items_id',
],
'glpi_plugin_formcreator_questiondependencies' => [
'id',
'plugin_formcreator_questions_id',
'plugin_formcreator_questions_id_2',
],
'glpi_plugin_formcreator_questionregexes' => [
'id',
'plugin_formcreator_questions_id',
],
'glpi_plugin_formcreator_questionranges' => [
'id',
'plugin_formcreator_questions_id',
],
'glpi_plugin_formcreator_forms_languages' => [
'id',
'plugin_formcreator_forms_id',
],
];

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

// Exception for ID key of glpi_plugin_formcreator_entityconfigs : it is not an autoincrement
$table = 'glpi_plugin_formcreator_entityconfigs';
$this->migration->changeField($table, 'id', 'id', 'int ' . DBConnection::getDefaultPrimaryKeySignOption() . ' not null');
}
}
2 changes: 1 addition & 1 deletion tests/3-unit/PluginFormcreatorTargetChange.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ public function testImport() {
'urgency_question' => '0',
'validation_followup' => '1',
'destination_entity' => '0',
'destination_entity_value' => null,
'destination_entity_value' => '0',
'tag_type' => \PluginFormcreatorTargetChange::TAG_TYPE_NONE,
'tag_questions' => '0',
'tag_specifics' => '',
Expand Down
2 changes: 1 addition & 1 deletion tests/3-unit/PluginFormcreatorTargetTicket.php
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ public function testImport() {
'location_question' => '0',
'validation_followup' => '1',
'destination_entity' => '0',
'destination_entity_value' => null,
'destination_entity_value' => 0,
'tag_type' => \PluginFormcreatorTargetTicket::TAG_TYPE_NONE,
'tag_questions' => '0',
'tag_specifics' => '',
Expand Down

0 comments on commit 4af002a

Please sign in to comment.