Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 24, 2026

Summary

Optimizes the with_role() method's log_invalid parameter to use a single aggregation query instead of multiple queries and per-row logging.

List of Changes

  • Modified UserProjectMembershipQuerySet.with_role() to use .values().annotate(Count()) aggregation
  • Replaced per-membership logging with single summary log containing count and project IDs
  • Removed unnecessary .select_related("user", "project") joins

Related Issues

Relates to #1096

Detailed Description

The original implementation when log_invalid=True executed two queries (.exists() + iteration with .select_related()) and logged one warning per invalid membership. In API request paths with many invalid memberships, this added overhead and generated noisy logs.

Before:

invalid = queryset.filter(has_role=False).select_related("user", "project")
if invalid.exists():  # Query 1
    for membership in invalid:  # Query 2
        logger.warning(f"UserProjectMembership {membership.pk} for user...")  # N logs

After:

invalid_by_project = list(
    queryset.filter(has_role=False)
    .values("project_id")
    .annotate(count=Count("id"))  # Single query
)
if invalid_by_project:
    invalid_count = sum(item["count"] for item in invalid_by_project)
    project_ids = [item["project_id"] for item in invalid_by_project]
    logger.warning(f"{invalid_count} UserProjectMembership(s) in project(s): {projects_str}")  # 1 log

Reduces query count from 2 to 1 and log output from N to 1 summary message.

How to Test the Changes

  1. Create UserProjectMembership records without corresponding role groups
  2. Call API endpoint that uses .with_role(log_invalid=True) (e.g., GET /api/projects/{id}/members/)
  3. Verify single summary log appears with count and project IDs
  4. Verify API response still filters out invalid memberships correctly

Screenshots

N/A

Deployment Notes

No deployment actions required. Changes are backward compatible.

Checklist

  • I have tested these changes appropriately.
  • I have added and/or modified relevant tests.
  • I updated relevant documentation or comments.
  • I have verified that this PR follows the project's coding standards.
  • Any dependent changes have already been merged to main.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@netlify
Copy link

netlify bot commented Jan 24, 2026

Deploy Preview for antenna-preview canceled.

Name Link
🔨 Latest commit 29320bd
🔍 Latest deploy log https://app.netlify.com/projects/antenna-preview/deploys/6974721315c0cd00086c10c7

Copilot AI and others added 2 commits January 24, 2026 07:16
Co-authored-by: mihow <158175+mihow@users.noreply.github.com>
Co-authored-by: mihow <158175+mihow@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor UserProjectMembership API role fields nesting refactor: optimize log_invalid query pattern in UserProjectMembership.with_role() Jan 24, 2026
Copilot AI requested a review from mihow January 24, 2026 07:19
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