-
Notifications
You must be signed in to change notification settings - Fork 59
feat(migration): add type renaming to resolve native asset name conflicts #472
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MyvTsv
wants to merge
3
commits into
pluginsGLPI:main
Choose a base branch
from
MyvTsv:ticket42658
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+497
−73
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,8 @@ class PluginGenericobjectType extends CommonDBTM | |
|
|
||
| public const CAN_OPEN_TICKET = 1024; | ||
|
|
||
| public const MAX_TYPE_NAME_LENGTH = 25; | ||
|
|
||
| public $dohistory = true; | ||
|
|
||
| public static $rightname = 'plugin_genericobject_types'; | ||
|
|
@@ -749,80 +751,240 @@ private static function normalizeNamesAndItemtypes(Migration $migration) | |
| continue; | ||
| } | ||
|
|
||
| self::updateNameAndItemtype( | ||
| self::applyTypeRename( | ||
| $migration, | ||
| $type['id'], | ||
| $old_name, | ||
| $new_name, | ||
| $old_itemtype, | ||
| $new_itemtype, | ||
| ); | ||
| } | ||
|
|
||
| $DB->update( | ||
| self::getTable(), | ||
| [ | ||
| 'name' => $new_name, | ||
| 'itemtype' => $new_itemtype, | ||
| ], | ||
| ['id' => $type['id']], | ||
| ); | ||
| ProfileRight::cleanAllPossibleRights(); | ||
| } | ||
|
|
||
| $DB->update( | ||
| self::getTable(), | ||
| [ | ||
| 'linked_itemtypes' => new QueryExpression( | ||
| 'REPLACE(' | ||
| . $DB->quoteName('linked_itemtypes') | ||
| . ',' | ||
| . $DB->quoteValue('"' . $old_itemtype . '"') // itemtype is surrounded by quotes | ||
| . ',' | ||
| . $DB->quoteValue('"' . $new_itemtype . '"') // itemtype is surrounded by quotes | ||
| . ')', | ||
| ), | ||
| ], | ||
| ['linked_itemtypes' => ['LIKE', '%"' . $old_itemtype . '"%']], | ||
| ); | ||
| /** | ||
| * Apply a full rename for a single type: update files, database tables, | ||
| * foreign keys, relation tables, linked_itemtypes, and related dropdowns. | ||
| * | ||
| * @param Migration $migration | ||
| * @param int $type_id ID of the type in glpi_plugin_genericobject_types | ||
| * @param string $old_name Current type name | ||
| * @param string $new_name New type name | ||
| * @param string $old_itemtype Current itemtype class name | ||
| * @param string $new_itemtype New itemtype class name | ||
| */ | ||
| private static function applyTypeRename( | ||
| Migration $migration, | ||
| int $type_id, | ||
| string $old_name, | ||
| string $new_name, | ||
| string $old_itemtype, | ||
| string $new_itemtype, | ||
| ): void { | ||
| /** @var DBmysql $DB */ | ||
| global $DB; | ||
|
|
||
| // Handle dropdowns related to itemtype | ||
| $table = getTableForItemType($new_itemtype); | ||
| $fields = $DB->listFields($table); | ||
| foreach ($fields as $field => $options) { | ||
| if (preg_match("/s_id$/", $field)) { | ||
| $dropdown_old_table = getTableNameForForeignKeyField($field); | ||
|
|
||
| if (!preg_match('/^glpi_plugin_genericobject_/', $dropdown_old_table)) { | ||
| continue; | ||
| } | ||
|
|
||
| $dropdown_old_name = getSingular( | ||
| str_replace( | ||
| "glpi_plugin_genericobject_", | ||
| "", | ||
| $dropdown_old_table, | ||
| ), | ||
| ); | ||
| $dropdown_old_itemtype = 'PluginGenericobject' . ucfirst($dropdown_old_name); | ||
| $dropdown_new_name = self::filterInput($dropdown_old_name); | ||
| $dropdown_new_itemtype = self::getClassByName($dropdown_new_name); | ||
|
|
||
| if ( | ||
| $dropdown_old_name == $dropdown_new_name | ||
| && $dropdown_old_itemtype == $dropdown_new_itemtype | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| self::updateNameAndItemtype( | ||
| $migration, | ||
| $dropdown_old_name, | ||
| $dropdown_new_name, | ||
| $dropdown_old_itemtype, | ||
| $dropdown_new_itemtype, | ||
| ); | ||
| self::updateNameAndItemtype( | ||
| $migration, | ||
| $old_name, | ||
| $new_name, | ||
| $old_itemtype, | ||
| $new_itemtype, | ||
| ); | ||
|
|
||
| $DB->update( | ||
| self::getTable(), | ||
| [ | ||
| 'name' => $new_name, | ||
| 'itemtype' => $new_itemtype, | ||
| ], | ||
| ['id' => $type_id], | ||
| ); | ||
|
|
||
| $DB->update( | ||
| self::getTable(), | ||
| [ | ||
| 'linked_itemtypes' => new QueryExpression( | ||
| 'REPLACE(' | ||
| . $DB->quoteName('linked_itemtypes') | ||
| . ',' | ||
| . $DB->quoteValue('"' . $old_itemtype . '"') // itemtype is surrounded by quotes | ||
| . ',' | ||
| . $DB->quoteValue('"' . $new_itemtype . '"') // itemtype is surrounded by quotes | ||
| . ')', | ||
| ), | ||
| ], | ||
| ['linked_itemtypes' => ['LIKE', '%"' . $old_itemtype . '"%']], | ||
| ); | ||
|
|
||
| // Handle dropdowns related to itemtype | ||
| $table = getTableForItemType($new_itemtype); | ||
| $fields = $DB->listFields($table); | ||
| foreach ($fields as $field => $options) { | ||
| if (preg_match("/s_id$/", $field)) { | ||
| $dropdown_old_table = getTableNameForForeignKeyField($field); | ||
|
|
||
| if (!preg_match('/^glpi_plugin_genericobject_/', $dropdown_old_table)) { | ||
| continue; | ||
| } | ||
|
|
||
| $dropdown_old_name = getSingular( | ||
| str_replace( | ||
| "glpi_plugin_genericobject_", | ||
| "", | ||
| $dropdown_old_table, | ||
| ), | ||
| ); | ||
| $dropdown_old_itemtype = 'PluginGenericobject' . ucfirst($dropdown_old_name); | ||
| $dropdown_new_name = self::filterInput($dropdown_old_name); | ||
| $dropdown_new_itemtype = self::getClassByName($dropdown_new_name); | ||
|
|
||
| if ( | ||
| $dropdown_old_name == $dropdown_new_name | ||
| && $dropdown_old_itemtype == $dropdown_new_itemtype | ||
| ) { | ||
| continue; | ||
| } | ||
|
|
||
| self::updateNameAndItemtype( | ||
| $migration, | ||
| $dropdown_old_name, | ||
| $dropdown_new_name, | ||
| $dropdown_old_itemtype, | ||
| $dropdown_new_itemtype, | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| ProfileRight::cleanAllPossibleRights(); // Clean all possible rights are their name may have change | ||
| self::applyPluginsRename($migration, $old_itemtype, $new_itemtype); | ||
| } | ||
|
|
||
| /** | ||
| * Rename a genericobject type. | ||
| * | ||
| * Updates the type name, itemtype, all generated files, database tables, | ||
| * foreign keys, and all relation tables where the itemtype is referenced. | ||
| * | ||
| * @param int $type_id ID of the type in glpi_plugin_genericobject_types | ||
| * @param string $new_name New name for the type (will be filtered) | ||
| * | ||
| * @return bool True on success, false on failure | ||
| */ | ||
| public static function renameType(int $type_id, string $new_name): bool | ||
| { | ||
| /** @var DBmysql $DB */ | ||
| global $DB; | ||
|
|
||
| $type = new self(); | ||
| if (!$type->getFromDB($type_id)) { | ||
| Session::addMessageAfterRedirect( | ||
| __s('Type not found.', 'genericobject'), | ||
| false, | ||
| ERROR, | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| $old_name = $type->fields['name']; | ||
| $new_name = self::filterInput($new_name); | ||
|
|
||
| if ($new_name === '') { | ||
| Session::addMessageAfterRedirect( | ||
| __s('The new name cannot be empty.', 'genericobject'), | ||
| false, | ||
| ERROR, | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| if ($new_name === $old_name) { | ||
| return true; | ||
| } | ||
|
|
||
| $existing = $DB->request([ | ||
| 'FROM' => self::getTable(), | ||
| 'WHERE' => [ | ||
| 'name' => $new_name, | ||
| 'NOT' => ['id' => $type_id], | ||
| ], | ||
| ]); | ||
| if ($existing->numrows() > 0) { | ||
| Session::addMessageAfterRedirect( | ||
| __s('A type with this name already exists.', 'genericobject'), | ||
| false, | ||
| ERROR, | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| $manager = \Glpi\Asset\AssetDefinitionManager::getInstance(); | ||
| $reserved_pattern = $manager->getReservedSystemNamesPattern(); | ||
| if (preg_match($reserved_pattern, $new_name) === 1) { | ||
| Session::addMessageAfterRedirect( | ||
| __s('This name is reserved by a native GLPI asset type.', 'genericobject'), | ||
| false, | ||
| ERROR, | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| $new_system_name = self::getSystemName($new_name); | ||
| if (strlen($new_system_name) > self::MAX_TYPE_NAME_LENGTH) { | ||
| Session::addMessageAfterRedirect( | ||
| sprintf( | ||
| __s('The name "%s" is too long. The maximum allowed length is %d characters.', 'genericobject'), | ||
| $new_system_name, | ||
| self::MAX_TYPE_NAME_LENGTH, | ||
| ), | ||
| false, | ||
| ERROR, | ||
| ); | ||
| return false; | ||
| } | ||
|
|
||
| $old_itemtype = $type->fields['itemtype']; | ||
| $new_itemtype = self::getClassByName($new_name); | ||
|
|
||
| $migration = new Migration(PLUGIN_GENERICOBJECT_VERSION); | ||
| self::applyTypeRename( | ||
| $migration, | ||
| $type_id, | ||
| $old_name, | ||
| $new_name, | ||
| $old_itemtype, | ||
| $new_itemtype, | ||
| ); | ||
| ProfileRight::cleanAllPossibleRights(); | ||
| $migration->executeMigration(); | ||
|
|
||
| Session::addMessageAfterRedirect( | ||
| sprintf( | ||
| __s('Type "%s" has been renamed to "%s".', 'genericobject'), | ||
| $old_name, | ||
| $new_name, | ||
| ), | ||
| false, | ||
| INFO, | ||
| ); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Get the list of reserved GLPI core asset names. | ||
| * | ||
| * @return string[] | ||
| */ | ||
| public static function getReservedNames(): array | ||
| { | ||
| $manager = \Glpi\Asset\AssetDefinitionManager::getInstance(); | ||
| $pattern = $manager->getReservedSystemNamesPattern(); | ||
| if (preg_match('/\(([^)]+)\)/', $pattern, $matches)) { | ||
| return explode('|', $matches[1]); | ||
| } | ||
| return []; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -963,4 +1125,46 @@ private static function updateNameAndItemtype( | |
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Update all plugin data when a genericobject type is renamed. | ||
| * | ||
| * @param Migration $migration | ||
| * @param string $old_itemtype Old itemtype class name | ||
| * @param string $new_itemtype New itemtype class name | ||
| */ | ||
| private static function applyPluginsRename( | ||
| Migration $migration, | ||
| string $old_itemtype, | ||
| string $new_itemtype, | ||
| ): void { | ||
| /** @var DBmysql $DB */ | ||
| global $DB; | ||
|
|
||
| $columns = $DB->request([ | ||
| 'SELECT' => ['TABLE_NAME'], | ||
| 'FROM' => 'information_schema.COLUMNS', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The SQL account you're using may not have access to |
||
| 'WHERE' => [ | ||
| 'TABLE_SCHEMA' => $DB->dbdefault, | ||
| 'TABLE_NAME' => ['LIKE', 'glpi\_plugin\_%'], | ||
| 'COLUMN_NAME' => 'itemtypes', | ||
| ], | ||
| ]); | ||
|
|
||
| foreach ($columns as $row) { | ||
| $DB->update( | ||
| $row['TABLE_NAME'], | ||
| [ | ||
| 'itemtypes' => new QueryExpression( | ||
| 'REPLACE(' | ||
| . $DB->quoteName('itemtypes') | ||
| . ', ' . $DB->quoteValue(json_encode($old_itemtype)) | ||
| . ', ' . $DB->quoteValue(json_encode($new_itemtype)) | ||
| . ')', | ||
| ), | ||
| ], | ||
| ['itemtypes' => ['LIKE', '%' . $old_itemtype . '%']], | ||
| ); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be moved last. Cases that return false come before those that return true.
For example, if the name is too long, and the user enters the same name again, it returns true, but the name is still too long.