Skip to content

fix(group): Calculate member count as a whole#2257

Merged
javirln merged 4 commits into
chainloop-dev:mainfrom
javirln:fix/pfm-3338
Jul 15, 2025
Merged

fix(group): Calculate member count as a whole#2257
javirln merged 4 commits into
chainloop-dev:mainfrom
javirln:fix/pfm-3338

Conversation

@javirln

@javirln javirln commented Jul 15, 2025

Copy link
Copy Markdown
Member

This patch updates the way group membership counts are maintained. Instead of incrementing or decrementing the counter on each insertion or deletion, the total number of members is now recalculated after every transaction and the count is updated accordingly.

Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
@javirln javirln requested review from jiparis and migmartri July 15, 2025 09:41
@javirln javirln self-assigned this Jul 15, 2025

@migmartri migmartri left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's also add a migration to fix existing group

Comment thread app/controlplane/pkg/data/group.go Outdated

// Only set the member count and add member if userID is provided
// Set initial member count to 0, we'll update it after transaction
builder = builder.SetMemberCount(0)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why isn't this a defalt in the DB?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It's the default, it was a just to make it explicit. I can remove it if it causes confusion

now := time.Now()

// Find all group IDs where this user is a member in this organization
groupMemberships, grpMemErr := tx.GroupMembership.Query().Where(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what's this change about?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

After a user is removed from an organization, we need to update the member count for all groups they were part of. This change handles that by retrieving all associated groups and recalculating their member counts

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but I don't understand who changing from setting -1 to recalculating adds this amount of logic. Or is it that we didn't do it properly before?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The previous logic was missing a condition where it was decrementing all memberships even if it was marked as deleted. I've reverted the changes and used the old logic to take into account that part.

now := time.Now()

// Find all group IDs where this user is a member in this organization
groupMemberships, grpMemErr := tx.GroupMembership.Query().Where(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but I don't understand who changing from setting -1 to recalculating adds this amount of logic. Or is it that we didn't do it properly before?

@migmartri

Copy link
Copy Markdown
Member

please add a migration to fix existing groups, thanks

Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
WHERE gm."group_id" = g."id"
AND gm."deleted_at" IS NULL
)
WHERE 1=1; -- Apply to all groups

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

to non-deleted groups?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, that means count all memberships that are not marked as deleted for the group, aka, active memberships

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but should we update only non-deleted groups?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

let's remove the where please

Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
WHERE gm."group_id" = g."id"
AND gm."deleted_at" IS NULL
)
WHERE 1=1; -- Apply to all groups

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

but should we update only non-deleted groups?

Comment thread app/controlplane/pkg/data/membership.go Outdated
group.HasMembersWith(user.ID(userID)),
group.HasGroupUsersWith(groupmembership.UserID(userID), groupmembership.DeletedAtIsNil()),
group.HasOrganizationWith(organization.ID(orgID)),
group.MemberCountGT(0),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is wrong, it's not guarantee that the computation is correct does it?

@javirln javirln marked this pull request as draft July 15, 2025 10:39
Signed-off-by: Javier Rodriguez <javier@chainloop.dev>
@javirln javirln marked this pull request as ready for review July 15, 2025 11:23
@javirln javirln merged commit 4b97560 into chainloop-dev:main Jul 15, 2025
16 of 17 checks passed
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.

2 participants