Skip to content

Commit

Permalink
fix(questionregex,questionrange): repair missing or duplicates rows
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed May 18, 2022
1 parent 39cfcb3 commit f1dc088
Showing 1 changed file with 153 additions and 0 deletions.
153 changes: 153 additions & 0 deletions install/upgrade_to_2.13.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public function upgrade(Migration $migration) {
$this->addFormVisibility();
$this->addRequestSourceSetting();
$this->addEntityOption();
$this->questionParameterUnicity();
$this->questionParameterRepair();
}

public function addEntityOption() {
Expand Down Expand Up @@ -316,4 +318,155 @@ public function addDefaultFormListMode() {

$this->migration->addPostQuery("UPDATE `glpi_plugin_formcreator_entityconfigs` SET `default_form_list_mode`=0 WHERE `entities_id`=0");
}

public function questionParameterUnicity() {
global $DB;

// Remove possible duplicates
$DB->queryOrDie("
DELETE dr FROM glpi_plugin_formcreator_questionranges dr
INNER JOIN (
SELECT
count(qr.id) as cpt,
f.id as form_id,
s.id as section_id,
q.id as question_id,
q.name,
q.fieldtype,
qr.id as qrid
FROM
glpi_plugin_formcreator_questions as q
left join glpi_plugin_formcreator_questionranges as qr on (qr.plugin_formcreator_questions_id = q.id)
inner join glpi_plugin_formcreator_sections as s on (q.plugin_formcreator_sections_id = s.id)
inner join glpi_plugin_formcreator_forms as f on (s.plugin_formcreator_forms_id = f.id)
WHERE q.fieldtype IN ('checkboxes', 'radios', 'multiselect', 'integer', 'float', 'text', 'textarea')
GROUP BY
q.id
HAVING
cpt > 1
) j
WHERE dr.plugin_formcreator_questions_id = j.question_id AND dr.id <> j.qrid
");

$DB->queryOrDie("
DELETE dr FROM glpi_plugin_formcreator_questionregexes dr
INNER JOIN (
SELECT
count(qr.id) as cpt,
f.id as form_id,
s.id as section_id,
q.id as question_id,
q.name,
q.fieldtype,
qr.id as qrid
FROM
glpi_plugin_formcreator_questions as q
left join glpi_plugin_formcreator_questionregexes as qr on (qr.plugin_formcreator_questions_id = q.id)
inner join glpi_plugin_formcreator_sections as s on (q.plugin_formcreator_sections_id = s.id)
inner join glpi_plugin_formcreator_forms as f on (s.plugin_formcreator_forms_id = f.id)
WHERE q.fieldtype IN ('checkboxes', 'radios', 'multiselect', 'integer', 'float', 'text', 'textarea')
GROUP BY
q.id
HAVING
cpt > 1
) j
WHERE dr.plugin_formcreator_questions_id = j.question_id AND dr.id <> j.qrid
");


$tables = [
'glpi_plugin_formcreator_questionranges',
'glpi_plugin_formcreator_questionregexes',
];

foreach ($tables as $table) {
$this->migration->addKey($table, ['plugin_formcreator_questions_id', 'fieldname'], 'unicity', 'UNIQUE');
}
}

public function questionParameterRepair() {
global $DB;

$DB->queryOrDie("
INSERT INTO glpi_plugin_formcreator_questionranges
(id, plugin_formcreator_questions_id, range_min, range_max, fieldname, `uuid`)
SELECT
NULL,
question_id,
0,
0,
CASE
WHEN fieldtype IN ('radios', 'checkboxes', 'multiselect', 'integer', 'float', 'text', 'textarea') THEN 'range'
END,
UUID()
FROM
(
select
count(qr.id) as cpt,
f.id as form_id,
s.id as section_id,
q.id as question_id,
q.name,
q.fieldtype,
qr.id as qrid
from
glpi_plugin_formcreator_questions as q
left join glpi_plugin_formcreator_questionranges as qr on (qr.plugin_formcreator_questions_id = q.id)
inner join glpi_plugin_formcreator_sections as s on (q.plugin_formcreator_sections_id = s.id)
inner join glpi_plugin_formcreator_forms as f on (s.plugin_formcreator_forms_id = f.id)
WHERE
q.fieldtype IN (
'checkboxes',
'radios',
'multiselect',
'integer',
'float',
'text',
'textarea'
)
group by
q.id
having
cpt < 1
) AS q_repair
");

$DB->queryOrDie("
INSERT INTO glpi_plugin_formcreator_questionregexes
(id, plugin_formcreator_questions_id, regex, fieldname, `uuid`)
SELECT
NULL,
question_id,
'',
CASE
WHEN fieldtype IN ('text', 'textarea') THEN 'regex'
END,
UUID()
FROM
(
select
count(qr.id) as cpt,
f.id as form_id,
s.id as section_id,
q.id as question_id,
q.name,
q.fieldtype,
qr.id as qrid
from
glpi_plugin_formcreator_questions as q
left join glpi_plugin_formcreator_questionregexes as qr on (qr.plugin_formcreator_questions_id = q.id)
inner join glpi_plugin_formcreator_sections as s on (q.plugin_formcreator_sections_id = s.id)
inner join glpi_plugin_formcreator_forms as f on (s.plugin_formcreator_forms_id = f.id)
WHERE
q.fieldtype IN (
'text',
'textarea'
)
group by
q.id
having
cpt < 1
) AS q_repair
");
}
}

0 comments on commit f1dc088

Please sign in to comment.