Skip to content

optimize Cedar migration: replace N+1 queries with single query#1656

Merged
gugu merged 1 commit intomainfrom
backend_optimize_cedar_migration
Mar 10, 2026
Merged

optimize Cedar migration: replace N+1 queries with single query#1656
gugu merged 1 commit intomainfrom
backend_optimize_cedar_migration

Conversation

@gugu
Copy link
Contributor

@gugu gugu commented Mar 10, 2026

Fetch all groups needing migration in one query instead of iterating over every connection and querying groups per connection.

Fetch all groups needing migration in one query instead of
iterating over every connection and querying groups per connection.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 10, 2026 09:16
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the Cedar policy migration script to migrate all groups missing a cedarPolicy in a single query/loop rather than iterating connection-by-connection.

Changes:

  • Removes per-connection iteration and instead fetches all groups with missing/empty cedarPolicy.
  • Generates Cedar policies using each group’s associated connection and permissions, then saves them back to the group.
  • Simplifies repositories used by removing now-unneeded ConnectionEntity/PermissionEntity repositories.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +13 to +20
.leftJoinAndSelect('group.connection', 'connection')
.leftJoinAndSelect('group.permissions', 'permission')
.where('group.cedarPolicy IS NULL OR group.cedarPolicy = :empty', { empty: '' })
.getMany();

for (const group of groups) {
const permissions = group.permissions || [];
for (const group of groups) {
const connection = group.connection;
if (!connection) continue;
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

leftJoinAndSelect('group.connection', ...) plus if (!connection) continue; silently skips groups with missing connections. Consider using an innerJoinAndSelect (or adding a connection.id IS NOT NULL condition) so the query only returns migratable groups, and/or log/count skipped groups so the final message isn't misleading.

Copilot uses AI. Check for mistakes.
Comment on lines +11 to +16
const groups = await groupRepository
.createQueryBuilder('group')
.leftJoinAndSelect('group.connection', 'connection')
.leftJoinAndSelect('group.permissions', 'permission')
.where('group.cedarPolicy IS NULL OR group.cedarPolicy = :empty', { empty: '' })
.getMany();
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

This loads all groups lacking cedarPolicy (plus all their permissions) into memory at once via getMany(). If this runs during app bootstrap and the dataset is large, it can cause long startup times or OOM. Consider processing in pages/chunks (take/skip) or iterating by connection/group ids to bound memory.

Copilot uses AI. Check for mistakes.
Comment on lines +29 to 33
for (const tp of tablePermissions) {
const existing = tableMap.get(tp.tableName) || {
tableName: tp.tableName,
accessLevel: { visibility: false, readonly: false, add: false, delete: false, edit: false },
};
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

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

tp.tableName can be an empty string (PermissionEntity defaults tableName to ''), which would collapse multiple table permissions into the same map key and generate an incorrect policy. Add a guard to skip/log table permissions with missing/blank tableName (or treat them as invalid data).

Copilot uses AI. Check for mistakes.
@gugu gugu enabled auto-merge (squash) March 10, 2026 09:19
@gugu gugu merged commit e35570b into main Mar 10, 2026
21 of 23 checks passed
@gugu gugu deleted the backend_optimize_cedar_migration branch March 10, 2026 09:32
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