Skip to content

feat(BA-5682): backfill project-user scopes from association_groups_users#11080

Draft
fregataa wants to merge 2 commits intomainfrom
feat/BA-5682-migrate-group-users-to-scopes-entities
Draft

feat(BA-5682): backfill project-user scopes from association_groups_users#11080
fregataa wants to merge 2 commits intomainfrom
feat/BA-5682-migrate-group-users-to-scopes-entities

Conversation

@fregataa
Copy link
Copy Markdown
Member

Summary

  • Add Alembic migration c8d4e5f6a7b9 that copies every (user_id, group_id) row from association_groups_users into association_scopes_entities as a project-scope user membership with relation_type='ref', per BEP-1048 (Project ─ref→ User).
  • Single INSERT ... SELECT ... ON CONFLICT ON CONSTRAINT uq_scope_id_entity_id DO NOTHING, so the migration is idempotent and naturally deduplicates. Rows are migrated regardless of is_active to match legacy lookup semantics.
  • downgrade() is a no-op: once the auto-sync path lands, backfilled rows cannot be distinguished from live ones, so the schema is torn down by its own future migration rather than here.

Test plan

  • alembic upgrade head on a DB populated with association_groups_users rows; verify matching (project, group_id, user, user_id, ref) rows appear in association_scopes_entities.
  • Re-run alembic upgrade head and confirm no duplicate rows / no errors (idempotency).
  • Verify inactive users/groups are migrated as-is.
  • RBAC project membership lookups return the same set as the legacy table.

Resolves BA-5682

…sers

Copy every (user_id, group_id) row from association_groups_users into
association_scopes_entities as project-scope user memberships with
relation_type='ref', per BEP-1048 (Project ─ref→ User). Idempotent via
ON CONFLICT on uq_scope_id_entity_id; downgrade is a no-op since
backfilled rows cannot be distinguished from live auto-sync rows.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings April 15, 2026 01:28
@github-actions github-actions bot added size:M 30~100 LoC comp:manager Related to Manager component require:db-migration Automatically set when alembic migrations are added or updated labels Apr 15, 2026
Copy link
Copy Markdown
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

Adds an Alembic data migration to backfill project→user membership edges into the RBAC scope-entity mapping table, aligning project membership lookups with legacy association_groups_users semantics (per BEP-1048).

Changes:

  • Introduce migration c8d4e5f6a7b9 to copy (user_id, group_id)(scope_type='project', scope_id=group_id, entity_type='user', entity_id=user_id, relation_type='ref').
  • Make the backfill idempotent via INSERT ... SELECT ... ON CONFLICT ....
  • Leave downgrade() as a no-op due to inability to distinguish backfilled rows from future live-synced rows.

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

Comment on lines +31 to +42
op.execute("""
INSERT INTO association_scopes_entities
(scope_type, scope_id, entity_type, entity_id, relation_type)
SELECT
'project',
group_id::text,
'user',
user_id::text,
'ref'
FROM association_groups_users
ON CONFLICT ON CONSTRAINT uq_scope_id_entity_id DO NOTHING
""")
Copy link

Copilot AI Apr 15, 2026

Choose a reason for hiding this comment

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

ON CONFLICT ... DO NOTHING can leave pre-existing project→user edges with relation_type='auto' unchanged (e.g. rows inserted by earlier migrations before relation_type existed), even though this migration’s goal is to backfill/ensure relation_type='ref'. Since RelationType affects scope-chain traversal (AUTO edges are traversed; REF terminates), this can preserve incorrect semantics. Consider switching to ON CONFLICT ... DO UPDATE SET relation_type = 'ref' (optionally guarded with a WHERE association_scopes_entities.entity_type = 'user') so existing rows are upgraded to REF as well as inserted when missing.

Copilot uses AI. Check for mistakes.
@fregataa fregataa marked this pull request as draft April 15, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:manager Related to Manager component require:db-migration Automatically set when alembic migrations are added or updated size:M 30~100 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants