Skip to content

Commit

Permalink
pass tx instead of db reference (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobsee authored Aug 8, 2023
1 parent 69af300 commit cded768
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
11 changes: 6 additions & 5 deletions internal/dbtools/membership_enumeration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/metal-toolbox/governor-api/internal/models"
"github.com/volatiletech/null/v8"
"github.com/volatiletech/sqlboiler/v4/boil"
"github.com/volatiletech/sqlboiler/v4/queries"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
Expand Down Expand Up @@ -168,7 +169,7 @@ type EnumeratedMembership struct {
}

// GetMembershipsForUser returns a fully enumerated list of memberships for a user, optionally with sqlboiler's generated models populated
func GetMembershipsForUser(ctx context.Context, db *sql.DB, userID string, shouldPopulateAllModels bool) ([]EnumeratedMembership, error) {
func GetMembershipsForUser(ctx context.Context, db boil.ContextExecutor, userID string, shouldPopulateAllModels bool) ([]EnumeratedMembership, error) {
enumeratedMemberships := []EnumeratedMembership{}

err := queries.Raw(membershipsByUserQuery, userID).Bind(ctx, db, &enumeratedMemberships)
Expand All @@ -189,7 +190,7 @@ func GetMembershipsForUser(ctx context.Context, db *sql.DB, userID string, shoul
}

// GetMembersOfGroup returns a fully enumerated list of memberships in a group, optionally with sqlboiler's generated models populated
func GetMembersOfGroup(ctx context.Context, db *sql.DB, groupID string, shouldPopulateAllModels bool) ([]EnumeratedMembership, error) {
func GetMembersOfGroup(ctx context.Context, db boil.ContextExecutor, groupID string, shouldPopulateAllModels bool) ([]EnumeratedMembership, error) {
enumeratedMemberships := []EnumeratedMembership{}

err := queries.Raw(membershipsByGroupQuery, groupID).Bind(ctx, db, &enumeratedMemberships)
Expand All @@ -210,7 +211,7 @@ func GetMembersOfGroup(ctx context.Context, db *sql.DB, groupID string, shouldPo
}

// GetAllGroupMemberships returns a fully enumerated list of all memberships in the database, optionally with sqlboiler's generated models populated (use with caution, potentially lots of data)
func GetAllGroupMemberships(ctx context.Context, db *sql.DB, shouldPopulateAllModels bool) ([]EnumeratedMembership, error) {
func GetAllGroupMemberships(ctx context.Context, db boil.ContextExecutor, shouldPopulateAllModels bool) ([]EnumeratedMembership, error) {
enumeratedMemberships := []EnumeratedMembership{}

err := queries.Raw(allMembershipsQuery).Bind(ctx, db, &enumeratedMemberships)
Expand All @@ -231,7 +232,7 @@ func GetAllGroupMemberships(ctx context.Context, db *sql.DB, shouldPopulateAllMo
}

// HierarchyWouldCreateCycle returns true if a given new parent->member relationship would create a cycle in the database
func HierarchyWouldCreateCycle(ctx context.Context, db *sql.DB, parentGroupID, memberGroupID string) (bool, error) {
func HierarchyWouldCreateCycle(ctx context.Context, db boil.ContextExecutor, parentGroupID, memberGroupID string) (bool, error) {
hierarchies := make(map[string][]string)

hierarchyRows, err := models.GroupHierarchies().All(ctx, db)
Expand Down Expand Up @@ -306,7 +307,7 @@ func FindMemberDiff(before, after []EnumeratedMembership) []EnumeratedMembership
return uniqueMembersAfter
}

func populateModels(ctx context.Context, db *sql.DB, memberships []EnumeratedMembership) ([]EnumeratedMembership, error) {
func populateModels(ctx context.Context, db boil.ContextExecutor, memberships []EnumeratedMembership) ([]EnumeratedMembership, error) {
groupIDSet := make(map[string]bool)
userIDSet := make(map[string]bool)

Expand Down
12 changes: 6 additions & 6 deletions pkg/api/v1alpha1/group_membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ func (r *Router) addGroupMember(c *gin.Context) {
return
}

membershipsBefore, err := dbtools.GetMembershipsForUser(c, r.DB.DB, user.ID, false)
membershipsBefore, err := dbtools.GetMembershipsForUser(c, tx, user.ID, false)
if err != nil {
msg := "failed to compute new effective memberships: " + err.Error()

Expand Down Expand Up @@ -232,7 +232,7 @@ func (r *Router) addGroupMember(c *gin.Context) {
return
}

membershipsAfter, err := dbtools.GetMembershipsForUser(c, r.DB.DB, user.ID, false)
membershipsAfter, err := dbtools.GetMembershipsForUser(c, tx, user.ID, false)
if err != nil {
msg := "failed to compute new effective memberships: " + err.Error()

Expand Down Expand Up @@ -518,7 +518,7 @@ func (r *Router) removeGroupMember(c *gin.Context) {
return
}

membershipsBefore, err := dbtools.GetMembershipsForUser(c, r.DB.DB, user.ID, false)
membershipsBefore, err := dbtools.GetMembershipsForUser(c, tx, user.ID, false)
if err != nil {
msg := "failed to compute new effective memberships: " + err.Error()

Expand Down Expand Up @@ -568,7 +568,7 @@ func (r *Router) removeGroupMember(c *gin.Context) {
return
}

membershipsAfter, err := dbtools.GetMembershipsForUser(c, r.DB.DB, user.ID, false)
membershipsAfter, err := dbtools.GetMembershipsForUser(c, tx, user.ID, false)
if err != nil {
msg := "failed to compute new effective memberships: " + err.Error()

Expand Down Expand Up @@ -1028,7 +1028,7 @@ func (r *Router) processGroupRequest(c *gin.Context) {
return
}

membershipsBefore, err := dbtools.GetMembershipsForUser(c, r.DB.DB, user.ID, false)
membershipsBefore, err := dbtools.GetMembershipsForUser(c, tx, user.ID, false)
if err != nil {
msg := "failed to compute new effective memberships: " + err.Error()

Expand Down Expand Up @@ -1090,7 +1090,7 @@ func (r *Router) processGroupRequest(c *gin.Context) {
return
}

membershipsAfter, err := dbtools.GetMembershipsForUser(c, r.DB.DB, user.ID, false)
membershipsAfter, err := dbtools.GetMembershipsForUser(c, tx, user.ID, false)
if err != nil {
msg := "failed to compute new effective memberships: " + err.Error()

Expand Down

0 comments on commit cded768

Please sign in to comment.