Skip to content

Commit c7f329c

Browse files
committed
feat(install): run sync issues only once when if several upgrade steps require it
1 parent b64386f commit c7f329c

16 files changed

+67
-108
lines changed

install/install.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ class PluginFormcreatorInstall {
7878
'2.12.5' => '2.13',
7979
];
8080

81+
82+
protected bool $resyncIssues = false;
83+
8184
/**
8285
* Install the plugin
8386
* @param Migration $migration
@@ -171,6 +174,11 @@ public function upgrade(Migration $migration, $args = []): bool {
171174
$this->createMiniDashboard();
172175
Config::setConfigurationValues('formcreator', ['schema_version' => PLUGIN_FORMCREATOR_SCHEMA_VERSION]);
173176

177+
if ($this->resyncIssues) {
178+
// An upgrade step requires a resync of the issues
179+
$task = new CronTask();
180+
PluginFormcreatorIssue::cronSyncIssues($task);
181+
}
174182
return true;
175183
}
176184

@@ -192,6 +200,7 @@ protected function upgradeOneStep($toVersion) {
192200
$upgradeStep = new $updateClass();
193201
$upgradeStep->upgrade($this->migration);
194202
$this->migration->executeMigration();
203+
$this->resyncIssues = $this->resyncIssues || $upgradeStep->isResyncIssuesRequiresd();
195204
}
196205
}
197206

install/upgrade_to_2.10.2.php

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -36,107 +36,10 @@ class PluginFormcreatorUpgradeTo2_10_2 {
3636
* @param Migration $migration
3737
*/
3838
public function upgrade(Migration $migration) {
39-
global $DB;
40-
4139
$this->migration = $migration;
42-
43-
// Versioin 2.10.2 contains fixes on counters requiring repopulation of issues table
44-
$table = 'glpi_plugin_formcreator_issues';
45-
$DB->query("TRUNCATE `$table`");
46-
$this->syncIssues(new CronTask());
4740
}
4841

49-
/**
50-
* This is a copy of PluginFormcreatorIssue::cronSyncIssues as it is in 2.10.2
51-
*
52-
* @param CronTask $task
53-
* @return void
54-
*/
55-
public function syncIssues(CronTask $task) {
56-
global $DB;
57-
58-
$task->log("Sync issues from forms answers and tickets");
59-
$volume = 0;
60-
61-
// Request which merges tickets and formanswers
62-
// 1 ticket not linked to a formanswer => 1 issue which is the ticket sub_itemtype
63-
// 1 form_answer not linked to a ticket => 1 issue which is the formanswer sub_itemtype
64-
// 1 ticket linked to 1 form_answer => 1 issue which is the ticket sub_itemtype
65-
// several tickets linked to the same form_answer => 1 issue which is the form_answer sub_itemtype
66-
$query = "SELECT DISTINCT
67-
NULL AS `id`,
68-
`f`.`name` AS `name`,
69-
CONCAT('f_',`fanswer`.`id`) AS `display_id`,
70-
`fanswer`.`id` AS `original_id`,
71-
'PluginFormcreatorFormAnswer' AS `sub_itemtype`,
72-
`fanswer`.`status` AS `status`,
73-
`fanswer`.`request_date` AS `date_creation`,
74-
`fanswer`.`request_date` AS `date_mod`,
75-
`fanswer`.`entities_id` AS `entities_id`,
76-
`fanswer`.`is_recursive` AS `is_recursive`,
77-
`fanswer`.`requester_id` AS `requester_id`,
78-
`fanswer`.`users_id_validator` AS `users_id_validator`,
79-
`fanswer`.`groups_id_validator` AS `groups_id_validator`,
80-
`fanswer`.`comment` AS `comment`
81-
FROM `glpi_plugin_formcreator_formanswers` AS `fanswer`
82-
LEFT JOIN `glpi_plugin_formcreator_forms` AS `f`
83-
ON`f`.`id` = `fanswer`.`plugin_formcreator_forms_id`
84-
LEFT JOIN `glpi_items_tickets` AS `itic`
85-
ON `itic`.`items_id` = `fanswer`.`id`
86-
AND `itic`.`itemtype` = 'PluginFormcreatorFormAnswer'
87-
GROUP BY `original_id`
88-
HAVING COUNT(`itic`.`tickets_id`) != 1
89-
UNION
90-
SELECT DISTINCT
91-
NULL AS `id`,
92-
`tic`.`name` AS `name`,
93-
CONCAT('t_',`tic`.`id`) AS `display_id`,
94-
`tic`.`id` AS `original_id`,
95-
'Ticket' AS `sub_itemtype`,
96-
if(`tv`.`status` IS NULL,`tic`.`status`, if(`tv`.`status` = 2, 101, if(`tv`.`status` = 3, `tic`.`status`, 102))) AS `status`,
97-
`tic`.`date` AS `date_creation`,
98-
`tic`.`date_mod` AS `date_mod`,
99-
`tic`.`entities_id` AS `entities_id`,
100-
0 AS `is_recursive`,
101-
`tu`.`users_id` AS `requester_id`,
102-
`tv`.`users_id_validate` AS `users_id_validator`,
103-
0 AS `groups_id_validator`,
104-
`tic`.`content` AS `comment`
105-
FROM `glpi_tickets` AS `tic`
106-
LEFT JOIN `glpi_items_tickets` AS `itic`
107-
ON `itic`.`tickets_id` = `tic`.`id`
108-
AND `itic`.`itemtype` = 'PluginFormcreatorFormAnswer'
109-
LEFT JOIN (
110-
SELECT DISTINCT `users_id`, `tickets_id`
111-
FROM `glpi_tickets_users` AS `tu`
112-
WHERE `tu`.`type` = '" . CommonITILActor::REQUESTER . "'
113-
ORDER BY `id` ASC
114-
) AS `tu` ON (`tic`.`id` = `tu`.`tickets_id`)
115-
LEFT JOIN `glpi_ticketvalidations` as `tv`
116-
ON (`tic`.`id` = `tv`.`tickets_id`)
117-
WHERE `tic`.`is_deleted` = 0
118-
GROUP BY `original_id`
119-
HAVING COUNT(`itic`.`items_id`) <= 1";
120-
121-
$countQuery = "SELECT COUNT(*) AS `cpt` FROM ($query) AS `issues`";
122-
$result = $DB->query($countQuery);
123-
if ($result !== false) {
124-
if (version_compare(GLPI_VERSION, '9.5') < 0) {
125-
$fa = 'fetch_assoc';
126-
} else {
127-
$fa = 'fetchAssoc';
128-
}
129-
$count = $DB->$fa($result);
130-
$table = 'glpi_plugin_formcreator_issues';
131-
if (countElementsInTable($table) != $count['cpt']) {
132-
if ($DB->query("TRUNCATE `$table`")) {
133-
$DB->query("INSERT INTO `$table` SELECT * FROM ($query) as `dt`");
134-
$volume = 1;
135-
}
136-
}
137-
}
138-
$task->setVolume($volume);
139-
140-
return 1;
42+
public function isResyncIssuesRequiresd() {
43+
return true;
14144
}
14245
}

install/upgrade_to_2.10.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,8 @@ public function upgrade(Migration $migration) {
7272
$migration->addKey($table, 'users_id_validator');
7373
$migration->addKey($table, 'groups_id_validator');
7474
}
75+
76+
public function isResyncIssuesRequiresd() {
77+
return false;
78+
}
7579
}

install/upgrade_to_2.11.3.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ public function upgrade(Migration $migration) {
4848
$migration->changeField($table, 'date_creation', 'date_creation', 'datetime'. ' NOT NULL');
4949
$migration->changeField($table, 'date_mod', 'date_mod', 'datetime'. ' NOT NULL');
5050
}
51+
52+
public function isResyncIssuesRequiresd() {
53+
return false;
54+
}
5155
}

install/upgrade_to_2.11.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,5 +317,8 @@ public function disableAutomaticAction() {
317317
'state' => '0'
318318
]);
319319
}
320-
}
321320

321+
public function isResyncIssuesRequiresd() {
322+
return false;
323+
}
324+
}

install/upgrade_to_2.12.1.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,8 @@ public function upgrade(Migration $migration) {
7676
$table = 'glpi_plugin_formcreator_questions';
7777
$this->migration->changeField($table, 'description', 'description', 'MEDIUMTEXT');
7878
}
79+
80+
public function isResyncIssuesRequiresd() {
81+
return false;
82+
}
7983
}

install/upgrade_to_2.12.5.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,10 @@ public function addUserRecipient(): void {
4848
$table = 'glpi_plugin_formcreator_issues';
4949
if (!$DB->fieldExists($table, 'users_id_recipient')) {
5050
$this->migration->addField($table, 'users_id_recipient', 'integer', ['after' => 'comment']);
51-
52-
// Update issues
53-
$this->migration->migrationOneTable($table);
54-
if (PLUGIN_FORMCREATOR_SCHEMA_VERSION == '2.12') {
55-
// sync only if this version is 2.12.5. If the table changes later, this will fail
56-
$DB->query("TRUNCATE `$table`");
57-
PluginFormcreatorIssue::syncIssues();
58-
}
5951
}
6052
}
53+
54+
public function isResyncIssuesRequiresd() {
55+
return true;
56+
}
6157
}

install/upgrade_to_2.12.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,8 @@ public function normalizeIssues() {
147147
$this->migration->dropKey($table, 'original_id_sub_itemtype');
148148
$this->migration->addKey($table, ['itemtype', 'items_id'], 'item');
149149
}
150+
151+
public function isResyncIssuesRequiresd() {
152+
return false;
153+
}
150154
}

install/upgrade_to_2.13.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,8 @@ public function addDefaultFormListMode() {
316316

317317
$this->migration->addPostQuery("UPDATE `glpi_plugin_formcreator_entityconfigs` SET `default_form_list_mode`=0 WHERE `entities_id`=0");
318318
}
319+
320+
public function isResyncIssuesRequiresd() {
321+
return false;
322+
}
319323
}

install/upgrade_to_2.6.1.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ public function upgrade(Migration $migration) {
6464
$DB->query("UPDATE `glpi_plugin_formcreator_questions` SET `name`='$name' WHERE `id` = '$id'");
6565
}
6666
}
67+
68+
public function isResyncIssuesRequiresd() {
69+
return false;
70+
}
6771
}

install/upgrade_to_2.6.3.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,8 @@ public function upgrade(Migration $migration) {
8585
$DB->query("UPDATE `$table` SET `name`='$name' WHERE `id` = '$id'");
8686
}
8787
}
88+
89+
public function isResyncIssuesRequiresd() {
90+
return false;
91+
}
8892
}

install/upgrade_to_2.6.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,8 @@ public function upgrade(Migration $migration) {
206206

207207
$migration->executeMigration();
208208
}
209+
210+
public function isResyncIssuesRequiresd() {
211+
return false;
212+
}
209213
}

install/upgrade_to_2.7.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,8 @@ public function upgrade(Migration $migration) {
308308
$DB->query("UPDATE `glpi_plugin_formcreator_forms` SET `name` = '$name', `description` = '$description', `content` = '$content' WHERE `id` = '$id'");
309309
}
310310
}
311+
312+
public function isResyncIssuesRequiresd() {
313+
return true;
314+
}
311315
}

install/upgrade_to_2.8.1.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,8 @@ public function upgrade(Migration $migration) {
3636
$table = 'glpi_plugin_formcreator_issues';
3737
$migration->changeField($table, 'name', 'name', 'string', ['after' => 'id', 'value' => '']);
3838
}
39+
40+
public function isResyncIssuesRequiresd() {
41+
return true;
42+
}
3943
}

install/upgrade_to_2.8.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,8 @@ public function upgrade(Migration $migration) {
6161
'name' => 'Form Creator',
6262
]);
6363
}
64+
65+
public function isResyncIssuesRequiresd() {
66+
return false;
67+
}
6468
}

install/upgrade_to_2.9.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,8 @@ protected function enumToInt($table, $field, array $map, $options = []) {
374374
);
375375
}
376376
}
377+
378+
public function isResyncIssuesRequiresd() {
379+
return false;
380+
}
377381
}

0 commit comments

Comments
 (0)