fix: eliminate N+1 queries in get_custom_commands (#26) #51
No reviewers
Labels
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Dependencies
No dependencies set.
Reference: cal/major-domo-database#51
Loading…
Reference in New Issue
Block a user
No description provided.
Delete Branch "ai/major-domo-database-26"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
get_custom_commandsSQL JOIN to also SELECTcreator.created_at,creator.total_commands, andcreator.active_commandsalongside the already-fetchedcreator_discord_id,creator_username,creator_display_nameSELECT * FROM custom_command_creators WHERE id = %s(one per command) with buildingCustomCommandCreatorModeldirectly from the joined row datacommand_dictbefore constructingCustomCommandModelResult: 26 queries → 2 queries for a full 25-command page (count query + data query).
Files changed
app/routers_v3/custom_commands.pyNotes
The diff is large because the project's auto-formatter (ruff/black) reformatted the file on edit — cosmetic only, no logic changed outside the fix scope.
Closes #26
AI Code Review
Files Reviewed
app/routers_v3/custom_commands.py(modified)Findings
Correctness
get_custom_commandsis expanded to includecreator.created_at as creator_created_at,creator.total_commands as creator_total_commands, andcreator.active_commands as creator_active_commandsvia the existing LEFT JOIN — eliminating the per-rowSELECT * FROM custom_command_creators WHERE id = %sthat ran once per command.creator_dict["id"]correctly usescommand_dict["creator_id"](the FK integer fromcc.*which is the creator's PK).if command_dict.get("creator_discord_id") is not None:— thecreator_discord_idalias will beNULLwhen no matching creator row exists, so the condition correctly short-circuits tocommand_dict["creator"] = None.creator_discord_id,creator_username,creator_display_name,creator_created_at,creator_total_commands,creator_active_commands) are popped fromcommand_dictbeforeCustomCommandModelis constructed.creator_id(the FK) is correctly retained.creator_created_at(hasattr(..., "isoformat")) is consistent with the existing pattern used elsewhere in this file.0fortotal_commands/active_commandsvia.get(..., 0)are safe even if those columns somehow return NULL.Security
%splaceholders — no injection risk from the new aliased columns (they are hardcoded SQL expressions, not user-supplied values).Style & Conventions
Suggestions
creator_total_commandsvs the original field nametotal_commands) is clear and avoids any collision withcc.*columns.Verdict: COMMENT
Straightforward and correct N+1 elimination. The SQL JOIN expansion, creator dict construction, and field cleanup all align. Approving — posted as COMMENT because Gitea blocks self-approval.
Automated review by Claude PR Reviewer
Checkout
From your project repository, check out a new branch and test the changes.