-
Notifications
You must be signed in to change notification settings - Fork 62
Use SQL for Create, Update, and Delete groups #3199
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
Conversation
| CreatedBy uid.ID | ||
| CreatedByProvider uid.ID | ||
|
|
||
| Identities []Identity `gorm:"many2many:identities_groups"` |
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.
This field was not used, so removing it was as easy as removing this one line. We can always add it back in the future if we need it, but we'll have to add support to CreateGroup to actually use the column.
Right now most code seems to use the Groups field on Identity.
| // RemoveUsersFromGroup removes any user ID listed in idsToRemove from the group | ||
| // with ID groupID. | ||
| // Note that DeleteGroup also removes users from the group. | ||
| func RemoveUsersFromGroup(tx WriteTxn, groupID uid.ID, idsToRemove []uid.ID) error { |
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.
This changed from a for loop performing one query per userID, to a single query using an IN clause. There will be some limit to the size of the query I guess, but fewer round trips to the database for most cases.
DeleteGroup has a similar query without the identity_id IN clause for removing all the members of a group.
| _, err := db.Exec("INSERT INTO identities_groups (group_id, identity_id) SELECT ?, ? WHERE NOT EXISTS (SELECT 1 FROM identities_groups WHERE group_id = ? AND identity_id = ?)", groupID, id, groupID, id) | ||
| if err != nil { | ||
| return err | ||
| func AddUsersToGroup(tx WriteTxn, groupID uid.ID, idsToAdd []uid.ID) error { |
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.
This changed from a for loop performing one query per userID, to a single query that inserts multiple entries. It also uses ON CONFLICT DO NOTHING now that we only need to support postgreSQL.
|
|
||
| func DeleteGroups(db GormTxn, selectors ...SelectorFunc) error { | ||
| toDelete, err := ListGroups(db, nil, selectors...) | ||
| func DeleteGroup(tx WriteTxn, id uid.ID) error { |
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.
This was only ever called with a single group ID, so this function has changed to only accept that one ID, which removes the need to list groups.
Group membership also changed from using RemoveUsersFromGroup to a query that deletes all rows that match the group ID.
06a143d to
381ddfe
Compare
pdevine
left a comment
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.
LGTM.
381ddfe to
e376554
Compare
Summary
Updates the data query functions for
CreateGroup,AddUserstoGroup,RemoveUsersFromGroup, andDeleteGroupto use SQL.Related to #2415