Skip to content

Conversation

@dnephin
Copy link
Contributor

@dnephin dnephin commented Sep 12, 2022

Summary

Updates the data query functions for CreateGroup, AddUserstoGroup, RemoveUsersFromGroup, and DeleteGroup to use SQL.

Related to #2415

CreatedBy uid.ID
CreatedByProvider uid.ID

Identities []Identity `gorm:"many2many:identities_groups"`
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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 {
Copy link
Contributor Author

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.

@dnephin dnephin force-pushed the dnephin/sql-for-groups branch from 06a143d to 381ddfe Compare September 12, 2022 20:29
Copy link
Contributor

@pdevine pdevine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@dnephin dnephin force-pushed the dnephin/sql-for-groups branch from 381ddfe to e376554 Compare September 20, 2022 20:02
@dnephin dnephin merged commit d7beca9 into main Sep 21, 2022
@dnephin dnephin deleted the dnephin/sql-for-groups branch September 21, 2022 15:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants