Skip to content

Commit c0f7fd3

Browse files
stonebuzzRom1-B
andauthored
Fix(Core): fix migration error when container are related to Generico… (#1045)
* Fix(Core): fix migration error when container are related to Genericobject Model or Type * Update inc/container.class.php Co-authored-by: Romain B. <[email protected]> * Update inc/container.class.php Co-authored-by: Romain B. <[email protected]> * check before process * add more check and debug * remove useless empty line * adapt CHANGELOG.md --------- Co-authored-by: Romain B. <[email protected]>
1 parent 71d2399 commit c0f7fd3

File tree

2 files changed

+89
-56
lines changed

2 files changed

+89
-56
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010

1111
### Fixed
1212

13+
- Fix `GenericObject` Model and Type migration
1314
- Fix `json_decode` using class with namespace
1415
- Fix drag and drop
1516
- Increased the maximum length of the language column to support longer locale codes

inc/container.class.php

Lines changed: 88 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -150,72 +150,104 @@ public static function installBaseData(Migration $migration, $version)
150150
$migration->migrationOneTable($table);
151151
}
152152

153-
// Get itemtypes from PluginGenericobject
154-
if ($DB->tableExists('glpi_plugin_genericobject_types')) {
155-
// Check GenericObject version
156-
$genericobject_info = Plugin::getInfo('genericobject');
157-
if (version_compare($genericobject_info['version'] ?? '0', '3.0.0', '<')) {
158-
throw new RuntimeException(
159-
'GenericObject plugin cannot be migrated. Please update it to the latest version.',
160-
);
161-
}
162153

163-
// Check glpi_plugin_genericobject_types table
164-
if (!$DB->fieldExists('glpi_plugin_genericobject_types', 'itemtype')) {
165-
throw new RuntimeException(
166-
'Integrity error on the glpi_plugin_genericobject_types table from the GenericObject plugin.',
167-
);
168-
}
154+
// Get containers with PluginGenericobject itemtype
155+
$data = $DB->request([
156+
'FROM' => $table,
157+
'WHERE' => [
158+
new QueryExpression(
159+
$table . ".itemtypes LIKE '%PluginGenericobject%'",
160+
),
161+
],
162+
]);
169163

170-
$migration_genericobject_itemtype = [];
171-
$result = $DB->request(['FROM' => 'glpi_plugin_genericobject_types']);
172-
foreach ($result as $type) {
173-
$migration_genericobject_itemtype[$type['itemtype']] = [
174-
'genericobject_itemtype' => $type['itemtype'],
175-
'itemtype' => 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset',
176-
'genericobject_name' => $type['name'],
177-
'name' => $type['name'] . 'Asset',
178-
];
179-
}
164+
if (count($data) > 0) {
180165

181-
// Get containers with PluginGenericobject itemtype
182-
$result = $DB->request([
183-
'FROM' => $table,
184-
'WHERE' => [
185-
new QueryExpression(
186-
$table . ".itemtypes LIKE '%PluginGenericobject%'",
187-
),
188-
],
189-
]);
166+
// Get itemtypes from PluginGenericobject
167+
if ($DB->tableExists('glpi_plugin_genericobject_types')) {
168+
// Check GenericObject version
169+
$genericobject_info = Plugin::getInfo('genericobject');
170+
if (version_compare($genericobject_info['version'] ?? '0', '3.0.0', '<')) {
171+
throw new RuntimeException(
172+
'GenericObject plugin cannot be migrated. Please update it to the latest version.',
173+
);
174+
}
190175

191-
$container_class = new self();
192-
foreach ($result as $container) {
193-
self::generateTemplate($container);
194-
foreach (PluginFieldsToolbox::decodeJSONItemtypes($container['itemtypes']) as $itemtype) {
195-
$classname = self::getClassname($itemtype, $container["name"]);
196-
$old_table = $classname::getTable();
197-
// Rename genericobject container table
198-
if (
199-
$DB->tableExists($old_table) &&
200-
isset($migration_genericobject_itemtype[$itemtype]) &&
201-
str_contains($old_table, 'glpi_plugin_fields_plugingenericobject' . $migration_genericobject_itemtype[$itemtype]['genericobject_name'])
202-
) {
203-
$new_table = str_replace('plugingenericobject' . $migration_genericobject_itemtype[$itemtype]['genericobject_name'], 'glpicustomasset' . strtolower($migration_genericobject_itemtype[$itemtype]['name']), $old_table);
204-
$migration->renameTable($old_table, $new_table);
176+
// Check glpi_plugin_genericobject_types table
177+
if (!$DB->fieldExists('glpi_plugin_genericobject_types', 'itemtype')) {
178+
throw new RuntimeException(
179+
'Integrity error on the glpi_plugin_genericobject_types table from the GenericObject plugin.',
180+
);
181+
}
182+
183+
$migration_genericobject_itemtype = [];
184+
$result = $DB->request([
185+
'FROM' => 'glpi_plugin_genericobject_types',
186+
'WHERE' => [
187+
new QueryExpression(
188+
$table . ".itemtypes LIKE '%PluginGenericobject%'",
189+
),
190+
],
191+
]);
192+
193+
foreach ($result as $type) {
194+
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'Asset';
195+
if (str_ends_with($type['itemtype'], 'Model')) {
196+
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetModel';
197+
} elseif (str_ends_with($type['itemtype'], 'Type')) {
198+
$customasset_classname = 'Glpi\\\\CustomAsset\\\\' . $type['name'] . 'AssetType';
205199
}
200+
201+
$migration_genericobject_itemtype[$type['itemtype']] = [
202+
'genericobject_itemtype' => $type['itemtype'],
203+
'itemtype' => $customasset_classname,
204+
'genericobject_name' => $type['name'],
205+
'name' => $type['name'] . 'Asset',
206+
];
206207
}
207-
// Update old genericobject itemtypes in container
208-
$map = array_column($migration_genericobject_itemtype, 'itemtype', 'genericobject_itemtype');
209-
$itemtypes = strtr($container['itemtypes'], $map);
210-
$container_class->update(
211-
[
212-
'id' => $container['id'],
213-
'itemtypes' => $itemtypes,
208+
209+
// Get containers with PluginGenericobject itemtype
210+
$result = $DB->request([
211+
'FROM' => $table,
212+
'WHERE' => [
213+
new QueryExpression(
214+
$table . ".itemtypes LIKE '%PluginGenericobject%'",
215+
),
214216
],
217+
]);
218+
219+
$container_class = new self();
220+
foreach ($result as $container) {
221+
self::generateTemplate($container);
222+
foreach (json_decode($container['itemtypes']) as $itemtype) {
223+
$classname = self::getClassname($itemtype, $container["name"]);
224+
$old_table = $classname::getTable();
225+
// Rename genericobject container table
226+
if (
227+
$DB->tableExists($old_table) &&
228+
isset($migration_genericobject_itemtype[$itemtype]) &&
229+
str_contains($old_table, 'glpi_plugin_fields_plugingenericobject' . $migration_genericobject_itemtype[$itemtype]['genericobject_name'])
230+
) {
231+
$new_table = str_replace('plugingenericobject' . $migration_genericobject_itemtype[$itemtype]['genericobject_name'], 'glpicustomasset' . strtolower($migration_genericobject_itemtype[$itemtype]['name']), $old_table);
232+
$migration->renameTable($old_table, $new_table);
233+
}
234+
}
235+
// Update old genericobject itemtypes in container
236+
$map = array_column($migration_genericobject_itemtype, 'itemtype', 'genericobject_itemtype');
237+
$itemtypes = strtr($container['itemtypes'], $map);
238+
$container_class->update(
239+
[
240+
'id' => $container['id'],
241+
'itemtypes' => $itemtypes,
242+
],
243+
);
244+
}
245+
} else {
246+
throw new RuntimeException(
247+
'The Fields plugin is referencing a GenericObject class, but the corresponding table glpi_plugin_genericobject_types could not be found in the database.',
215248
);
216249
}
217250
}
218-
219251
return true;
220252
}
221253

0 commit comments

Comments
 (0)