@@ -143,8 +143,14 @@ public function upgrade(Migration $migration, $args = []): bool {
143143 }
144144 }
145145
146- // Check schema of tables before upgrading
147146 $ oldVersion = Config::getConfigurationValue ('formcreator ' , 'previous_version ' );
147+ // Force fix of signed columns to reduce upgrade errors frequency
148+ // This assumes that all modified columns exist in the database
149+ if (version_compare ($ oldVersion , '2.13.0 ' ) >= 0 ) {
150+ $ this ->migrateFkToUnsignedInt ();
151+ }
152+
153+ // Check schema of tables before upgrading
148154 if (!isset ($ args ['skip-db-check ' ])) {
149155 if ($ oldVersion !== null ) {
150156 $ checkResult = true ;
@@ -874,4 +880,149 @@ public function checkSchema(
874880
875881 return true ;
876882 }
883+
884+ /**
885+ * Upgrade columns containing foreign keys to unsigned int
886+ * picked from upgrade to 2.13.0, duplicated here to reduce upgrade errors
887+ * when checking the DB schema.
888+ *
889+ * @return void
890+ */
891+ protected function migrateFkToUnsignedInt () {
892+ global $ DB ;
893+
894+ $ table = 'glpi_plugin_formcreator_formanswers ' ;
895+ if ($ DB ->fieldExists ($ table , 'requester_id ' )) {
896+ $ DB ->queryOrDie ("UPDATE ` $ table` SET `requester_id` = 0 WHERE `requester_id` IS NULL " );
897+ }
898+
899+ $ table = 'glpi_plugin_formcreator_targetchanges ' ;
900+ if ($ DB ->fieldExists ($ table , 'due_date_question ' )) {
901+ $ DB ->queryOrDie ("UPDATE ` $ table` SET `due_date_question` = 0 WHERE `due_date_question` IS NULL " );
902+ }
903+ if ($ DB ->fieldExists ($ table , 'destination_entity_value ' )) {
904+ $ DB ->queryOrDie ("UPDATE ` $ table` SET `destination_entity_value` = 0 WHERE `destination_entity_value` IS NULL " );
905+ }
906+ $ table = 'glpi_plugin_formcreator_targettickets ' ;
907+ if ($ DB ->fieldExists ($ table , 'due_date_question ' )) {
908+ $ DB ->queryOrDie ("UPDATE ` $ table` SET `due_date_question` = 0 WHERE `due_date_question` IS NULL " );
909+ }
910+ if ($ DB ->fieldExists ($ table , 'destination_entity_value ' )) {
911+ $ DB ->queryOrDie ("UPDATE ` $ table` SET `destination_entity_value` = 0 WHERE `destination_entity_value` IS NULL " );
912+ }
913+ $ table = 'glpi_plugin_formcreator_targets_actors ' ;
914+ if ($ DB ->fieldExists ($ table , 'actor_value ' )) {
915+ $ DB ->queryOrDie ("UPDATE ` $ table` SET `actor_value` = 0 WHERE `actor_value` IS NULL " );
916+ }
917+
918+ $ tables = [
919+ 'glpi_plugin_formcreator_answers ' => [
920+ 'plugin_formcreator_formanswers_id ' ,
921+ 'plugin_formcreator_questions_id ' ,
922+ ],
923+ 'glpi_plugin_formcreator_formanswers ' => [
924+ 'plugin_formcreator_forms_id ' ,
925+ 'requester_id ' ,
926+ 'users_id_validator ' ,
927+ 'groups_id_validator ' ,
928+ ],
929+ 'glpi_plugin_formcreator_forms_languages ' => [
930+ 'plugin_formcreator_forms_id ' ,
931+ ],
932+ 'glpi_plugin_formcreator_forms_profiles ' => [
933+ 'plugin_formcreator_forms_id ' ,
934+ 'profiles_id ' ,
935+ ],
936+ 'glpi_plugin_formcreator_forms_validators ' => [
937+ 'plugin_formcreator_forms_id ' ,
938+ 'items_id ' ,
939+ ],
940+ 'glpi_plugin_formcreator_issues ' => [
941+ 'users_id_recipient ' ,
942+ 'plugin_formcreator_categories_id ' ,
943+ ],
944+ 'glpi_plugin_formcreator_questions ' => [
945+ 'plugin_formcreator_sections_id ' ,
946+ ],
947+ 'glpi_plugin_formcreator_questiondependencies ' => [
948+ 'plugin_formcreator_questions_id ' ,
949+ 'plugin_formcreator_questions_id_2 ' ,
950+ ],
951+ 'glpi_plugin_formcreator_sections ' => [
952+ 'plugin_formcreator_forms_id ' ,
953+ ],
954+ 'glpi_plugin_formcreator_targetchanges ' => [
955+ 'due_date_question ' ,
956+ 'urgency_question ' ,
957+ 'destination_entity_value ' ,
958+ 'category_question ' ,
959+ 'sla_question_tto ' ,
960+ 'sla_question_ttr ' ,
961+ 'ola_question_tto ' ,
962+ 'ola_question_ttr ' ,
963+ ],
964+ 'glpi_plugin_formcreator_targettickets ' => [
965+ 'type_question ' ,
966+ 'due_date_question ' ,
967+ 'urgency_question ' ,
968+ 'destination_entity_value ' ,
969+ 'category_question ' ,
970+ 'associate_question ' ,
971+ 'location_question ' ,
972+ 'sla_question_tto ' ,
973+ 'sla_question_ttr ' ,
974+ 'ola_question_tto ' ,
975+ 'ola_question_ttr ' ,
976+ ],
977+ 'glpi_plugin_formcreator_targets_actors ' => [
978+ 'items_id ' ,
979+ 'actor_value ' ,
980+ ],
981+ 'glpi_plugin_formcreator_questionregexes ' => [
982+ 'plugin_formcreator_questions_id ' ,
983+ ],
984+ 'glpi_plugin_formcreator_questionranges ' => [
985+ 'plugin_formcreator_questions_id ' ,
986+ ],
987+ ];
988+
989+ foreach ($ tables as $ table => $ fields ) {
990+ if (!$ DB ->tableExists ($ table )) {
991+ continue ;
992+ }
993+ foreach ($ fields as $ field ) {
994+ $ type = 'INT ' . DBConnection::getDefaultPrimaryKeySignOption () . ' NOT NULL ' ;
995+ if ($ field == 'id ' ) {
996+ $ type .= ' AUTO_INCREMENT ' ;
997+ } else {
998+ $ type .= ' DEFAULT 0 ' ;
999+ }
1000+ if (!$ DB ->fieldExists ($ table , $ field )) {
1001+ continue ;
1002+ }
1003+ $ this ->migration ->changeField ($ table , $ field , $ field , $ type );
1004+ }
1005+ }
1006+
1007+ $ table = 'glpi_plugin_formcreator_entityconfigs ' ;
1008+ if ($ DB ->tableExists ($ table )) {
1009+ $ rows = $ DB ->request ([
1010+ 'COUNT ' => 'c ' ,
1011+ 'FROM ' => $ table ,
1012+ 'WHERE ' => ['id ' => 0 ]
1013+ ]);
1014+ $ count = $ rows !== null ?$ rows ->current ()['c ' ] : null ;
1015+ if ($ count !== null ) {
1016+ if ($ count == 1 ) {
1017+ $ rows = $ DB ->request ([
1018+ 'SELECT ' => ['MAX ' => 'id AS max_id ' ],
1019+ 'FROM ' => $ table ,
1020+ ]);
1021+ $ newId = (int ) ($ rows ->current ()['max_id ' ] + 1 );
1022+ $ DB ->query ("UPDATE ` $ table` SET `id`=' $ newId' WHERE `id` = 0 " );
1023+ }
1024+ }
1025+ $ this ->migration ->changeField ($ table , 'id ' , 'id ' , 'int ' . DBConnection::getDefaultPrimaryKeySignOption () . ' not null auto_increment ' );
1026+ }
1027+ }
8771028}
0 commit comments