fix: a group that cannot be deleted says why - #888
Merged
Conversation
Deleting a user group that something still holds came back as "The record is in use" —
MySQL error 1451 translated by the database layer — which names nothing. The group's own
"Used by" panel was no help either: it lists the group's *users*, so a group with no
members but a hundred accounts scoped to it looked entirely safe to delete right up until
it was not.
The delete now asks first, and the refusal says what is holding it: "Group in use", with
"Accounts: 2 - Accounts in history: 1".
Getting that right meant reading the schema rather than the existing helper. Six foreign
keys point at UserGroup and only three of them refuse a delete:
fk_User_userGroupId RESTRICT a user whose main group this is
fk_Account_userGroupId RESTRICT an account owned by the group
fk_AccountHistory_userGroupId RESTRICT a history row, which outlives its account
fk_AccountToUserGroup_userGroupId CASCADE
fk_ItemPreset_userGroupId CASCADE
fk_UserToUserGroup_userGroupId CASCADE
`getUsage()` had it wrong in both directions: it queried UserToUserGroup and
AccountToUserGroup, which cascade, and not AccountHistory, which does not. So wiring it up
as it stood would have refused deletes the database would have allowed — a group whose
only "usage" is the members it is supposed to have — while still letting through the one
case that actually fails. It asks for exactly the three RESTRICT relations now.
That method had never run. It has no caller anywhere in src, and the coverage report shows
count="0" on both it and the service method in front of it — which is presumably how it
came to disagree with the schema without anyone noticing.
The foreign keys still stand behind all this: a row created between the check and the
delete is refused by the database exactly as before. This is about telling an
administrator what to go and fix.
This is the same defect the codebase already fixed for individual users —
`User::getUsageForUser()` carries a comment about the panel disagreeing with the delete —
and it was never carried across to groups.
Checked by making the refusal unreachable: the new service test fails.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Deleting a user group that something still holds came back as "The record is in use" —
MySQL error 1451 translated by the database layer — which names nothing. The group's own
"Used by" panel was no help either: it lists the group's users, so a group with no
members but a hundred accounts scoped to it looked entirely safe to delete right up until
it was not.
The delete now asks first, and the refusal says what is holding it: "Group in use", with
"Accounts: 2 - Accounts in history: 1".
Getting that right meant reading the schema rather than the existing helper. Six foreign
keys point at UserGroup and only three of them refuse a delete:
getUsage()had it wrong in both directions: it queried UserToUserGroup andAccountToUserGroup, which cascade, and not AccountHistory, which does not. So wiring it up
as it stood would have refused deletes the database would have allowed — a group whose
only "usage" is the members it is supposed to have — while still letting through the one
case that actually fails. It asks for exactly the three RESTRICT relations now.
That method had never run. It has no caller anywhere in src, and the coverage report shows
count="0" on both it and the service method in front of it — which is presumably how it
came to disagree with the schema without anyone noticing.
The foreign keys still stand behind all this: a row created between the check and the
delete is refused by the database exactly as before. This is about telling an
administrator what to go and fix.
This is the same defect the codebase already fixed for individual users —
User::getUsageForUser()carries a comment about the panel disagreeing with the delete —and it was never carried across to groups.
Checked by making the refusal unreachable: the new service test fails.