Skip to content

Commit acf23dd

Browse files
MyvTsvRom1-Bstonebuzz
authored
fix(migration): ensure GenericObject type dropdowns are migrated (#1199)
* fix(migration): ensure GenericObject type dropdowns are migrated * Update CHANGELOG.md * fix CI * Update inc/field.class.php Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> * Update inc/migration.class.php Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> * review * Update inc/field.class.php Co-authored-by: Stanislas <skita@teclib.com> * lint --------- Co-authored-by: Romain B. <8530352+Rom1-B@users.noreply.github.com> Co-authored-by: Stanislas <skita@teclib.com>
1 parent d62aa60 commit acf23dd

4 files changed

Lines changed: 80 additions & 32 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1414
- Fix search crash when two containers share a dropdown field with the same name.
1515
- Fix handle native GLPI dropdown types when binding additional fields to form destination
1616
- Fix container name/label corruption during GenericObject migration, which could break the migration with a MySQL identifier-length error.
17+
- Fix GenericObject type dropdowns migration
1718

1819
## [1.24.2] - 2026-06-30
1920

inc/container.class.php

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -163,38 +163,7 @@ public static function installBaseData(Migration $migration, $version)
163163

164164
// Get itemtypes from PluginGenericobject
165165
if ($DB->tableExists('glpi_plugin_genericobject_types')) {
166-
// Check GenericObject version
167-
$genericobject_info = Plugin::getInfo('genericobject');
168-
if (version_compare($genericobject_info['version'] ?? '0', '3.0.0', '<')) {
169-
throw new RuntimeException(
170-
'GenericObject plugin cannot be migrated. Please update it to the latest version.',
171-
);
172-
}
173-
174-
// Check glpi_plugin_genericobject_types table
175-
if (!$DB->fieldExists('glpi_plugin_genericobject_types', 'itemtype')) {
176-
throw new RuntimeException(
177-
'Integrity error on the glpi_plugin_genericobject_types table from the GenericObject plugin.',
178-
);
179-
}
180-
181-
$migration_genericobject_itemtype = [];
182-
$result = $DB->request(['FROM' => 'glpi_plugin_genericobject_types']);
183-
foreach ($result as $type) {
184-
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset';
185-
if (str_ends_with((string) $type['itemtype'], 'Model')) {
186-
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetModel';
187-
} elseif (str_ends_with((string) $type['itemtype'], 'Type')) {
188-
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetType';
189-
}
190-
191-
$migration_genericobject_itemtype[$type['itemtype']] = [
192-
'genericobject_itemtype' => $type['itemtype'],
193-
'itemtype' => $customasset_classname,
194-
'genericobject_name' => $type['name'],
195-
'name' => $type['name'] . 'Asset',
196-
];
197-
}
166+
$migration_genericobject_itemtype = PluginFieldsMigration::getGenericObjectTypes();
198167

199168
// Get containers with PluginGenericobject itemtype
200169
$result = $DB->request([

inc/field.class.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,43 @@ public static function installBaseData(Migration $migration, $version)
153153
$migration->addConfig(['stable_search_options' => 'yes'], 'plugin:fields');
154154
}
155155

156+
// Update old genericobject_itemtype dropdown fields to customasset_itemtype dropdown fields
157+
$has_genericobject_fields = $DB->tableExists('glpi_plugin_genericobject_types')
158+
&& $DB->request([
159+
'COUNT' => 'id',
160+
'FROM' => self::getTable(),
161+
'WHERE' => ['type' => ['LIKE', 'dropdown-PluginGenericobject%']],
162+
])->current()['COUNT(id)'] > 0;
163+
if ($has_genericobject_fields) {
164+
// Get all types from PluginGenericobject
165+
$migration_genericobject_itemtypes = PluginFieldsMigration::getGenericObjectTypes();
166+
167+
foreach ($migration_genericobject_itemtypes as $type) {
168+
$itemtype = str_replace('\\\\', '\\', $type['itemtype']);
169+
if (!class_exists($itemtype)) {
170+
$migration->addDebugMessage(sprintf(
171+
'The itemtype %s does not exist, please check if %s.class.php is present',
172+
$itemtype,
173+
$type['name'],
174+
));
175+
continue;
176+
}
177+
178+
// If corresponding customasset_itemtype exists, update field type
179+
$migration->addPostQuery(
180+
$DB->buildUpdate(
181+
self::getTable(),
182+
[
183+
'type' => 'dropdown-' . $itemtype,
184+
],
185+
[
186+
'type' => ['LIKE', 'dropdown-' . $type['genericobject_itemtype']],
187+
],
188+
),
189+
);
190+
}
191+
}
192+
156193
return true;
157194
}
158195

inc/migration.class.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,45 @@ private static function getCustomFieldsInContainerTable(
200200
fn(string $field) => !in_array($field, $basic_fields, true),
201201
);
202202
}
203+
204+
public static function getGenericObjectTypes(): array
205+
{
206+
/** @var DBmysql $DB */
207+
global $DB;
208+
209+
// Check GenericObject version
210+
$genericobject_info = Plugin::getInfo('genericobject');
211+
if (version_compare($genericobject_info['version'] ?? '0', '3.0.0', '<')) {
212+
throw new RuntimeException(
213+
'GenericObject plugin cannot be migrated. Please update it to the latest version.',
214+
);
215+
}
216+
217+
// Check glpi_plugin_genericobject_types table
218+
if (!$DB->fieldExists('glpi_plugin_genericobject_types', 'itemtype')) {
219+
throw new RuntimeException(
220+
'Integrity error on the glpi_plugin_genericobject_types table from the GenericObject plugin.',
221+
);
222+
}
223+
224+
$migration_genericobject_itemtype = [];
225+
$result = $DB->request(['FROM' => 'glpi_plugin_genericobject_types']);
226+
foreach ($result as $type) {
227+
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset';
228+
if (str_ends_with((string) $type['itemtype'], 'Model')) {
229+
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetModel';
230+
} elseif (str_ends_with((string) $type['itemtype'], 'Type')) {
231+
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetType';
232+
}
233+
234+
$migration_genericobject_itemtype[$type['itemtype']] = [
235+
'genericobject_itemtype' => $type['itemtype'],
236+
'itemtype' => $customasset_classname,
237+
'genericobject_name' => $type['name'],
238+
'name' => $type['name'] . 'Asset',
239+
];
240+
}
241+
242+
return $migration_genericobject_itemtype;
243+
}
203244
}

0 commit comments

Comments
 (0)