Skip to content

feat(BA-2938): Migrate Session data to RBAC database#9636

Merged
fregataa merged 8 commits into
mainfrom
feature/BA-2938-migrate-session-rbac
Mar 20, 2026
Merged

feat(BA-2938): Migrate Session data to RBAC database#9636
fregataa merged 8 commits into
mainfrom
feature/BA-2938-migrate-session-rbac

Conversation

@fregataa
Copy link
Copy Markdown
Member

@fregataa fregataa commented Mar 4, 2026

Summary

  • Add SESSION entity-type permissions to all role+scope combinations (skip domain member roles)
  • Create AUTO edges from User scope → Session (via user_uuid) and Project scope → Session (via group_id)
  • Follow new RBAC pattern with direct role+scope in permissions table (no permission_groups)
  • Use keyset pagination for scalability with large session tables

This migration brings Session entities into the RBAC system, enabling fine-grained access control. Unlike VFolder, Session has no invitation/sharing mechanism, making the migration simpler with only entity-type permissions and AUTO edges.

Test plan

  • Migration applies successfully (alembic upgrade head)
  • Migration rolls back correctly (alembic downgrade -1)
  • All quality checks pass (fmt, lint, check)
  • CI tests pass

Resolves BA-2938

Copilot AI review requested due to automatic review settings March 4, 2026 18:17
@github-actions github-actions Bot added size:L 100~500 LoC comp:manager Related to Manager component require:db-migration Automatically set when alembic migrations are added or updated labels Mar 4, 2026
fregataa added a commit that referenced this pull request Mar 4, 2026
@fregataa fregataa modified the milestones: Backlog, 26.3 Mar 4, 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

Migrates Session entities into the RBAC system by adding entity-type permissions and creating AUTO scope→entity associations via an Alembic migration.

Changes:

  • Added an Alembic migration to backfill SESSION permissions for existing role+scope combinations.
  • Added AUTO association edges from user/project scopes to Session records using batched pagination.
  • Added a changelog entry describing the migration.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 10 comments.

File Description
src/ai/backend/manager/models/alembic/versions/30c8308738ee_migrate_session_data_to_rbac.py Implements the RBAC data migration for Session permissions and scope associations.
changes/9636.feature.md Documents the feature/migration at a high level.

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

Comment thread changes/9636.feature.md
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

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.


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

@fregataa fregataa marked this pull request as draft March 5, 2026 09:22
fregataa added a commit that referenced this pull request Mar 11, 2026
@fregataa fregataa force-pushed the feature/BA-2938-migrate-session-rbac branch from f47c98a to 591fc12 Compare March 11, 2026 04:52
@fregataa fregataa marked this pull request as ready for review March 11, 2026 06:32
@fregataa fregataa requested a review from a team March 11, 2026 06:32
@jopemachine
Copy link
Copy Markdown
Member

Is it appropriate to classify a data migration PR as a feat?

fregataa added a commit that referenced this pull request Mar 13, 2026
@fregataa fregataa force-pushed the feature/BA-2938-migrate-session-rbac branch from 591fc12 to 79b7a88 Compare March 13, 2026 04:58
@fregataa fregataa requested a review from a team March 13, 2026 04:59
@fregataa
Copy link
Copy Markdown
Member Author

Is it appropriate to classify a data migration PR as a feat?

yes it is because it effects to RBAC features

fregataa and others added 5 commits March 20, 2026 13:39
Add alembic migration to migrate Session entities to the new RBAC system:

- Add SESSION entity-type permissions to all role+scope combinations
  - Skip domain member roles (scope too broad)
  - Member roles get READ operation only
  - Owner/admin roles get all operations
- Create AUTO edges from User scope → Session (via user_uuid)
- Create AUTO edges from Project scope → Session (via group_id)
- Use keyset pagination for scalability with large session tables
- Support both upgrade and downgrade operations

This migration follows the new RBAC pattern where permissions table
includes role_id, scope_type, scope_id directly (no permission_groups).
The association_scopes_entities table uses relation_type='auto' to mark
automatically managed scope-entity relationships.

Unlike VFolder, Session has no invitation/sharing mechanism, making the
migration simpler with only entity-type permissions and AUTO edges.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…tent ordering

Replace text-based UUID comparison with UUID type comparison to avoid
potential ordering discrepancies between lexicographic (text) and binary
(UUID) sort orders.

Changes:
- Import UUID from uuid module
- Initialize last_id as UUID object instead of string
- Remove ::text casting in WHERE and SELECT clauses
- Use native UUID comparison (id > :last_id) instead of text comparison

This ensures that filtering and ordering use the same sort order,
preventing potential data skips or duplicates during batch processing.

Addresses Copilot review comment about UUID/text ordering mismatch.
…LECT

Replace application-side OFFSET pagination with a single set-based
INSERT ... SELECT query for better performance and consistency.

Changes:
- Remove while loop with OFFSET pagination
- Use CTE (WITH clause) to derive role+scope combinations
- Use UNION ALL to combine member and owner operations
- Use unnest() to expand operation arrays inline
- Single database round-trip instead of multiple batches

Benefits:
- O(1) time complexity vs O(N) with OFFSET
- No risk of row skips/duplicates from concurrent changes
- Simpler code without manual batching logic
- Better query plan from database optimizer

Addresses Copilot review comment about OFFSET inefficiency.
…d queries

Replace string interpolation in INSERT and DELETE statements with
parameterized queries to improve security and maintainability.

Changes for INSERT:
- Build values_list with dicts instead of string formatting
- Use parameterized query with :named parameters
- Execute individual inserts in loop (safe for ON CONFLICT)

Changes for DELETE:
- Replace 'SELECT + string join' pattern with subquery
- Use DELETE ... WHERE id IN (SELECT ... LIMIT N)
- Check rowcount instead of empty result set
- Keep all parameters bound safely

Benefits:
- Eliminates SQL injection risks from malformed UUIDs
- Prevents quoting/escaping issues
- Avoids oversized query strings with large batches
- More maintainable and readable code

Addresses Copilot review comments about SQL injection and DELETE pattern.
Rename constant to use more accurate terminology. 'SUFFIX' better
describes the pattern matching with str.endswith() than 'POSTFIX'.

Addresses Copilot review comment about naming convention.
@fregataa fregataa force-pushed the feature/BA-2938-migrate-session-rbac branch from 79b7a88 to b6c1341 Compare March 20, 2026 04:41
…vel constants

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@fregataa fregataa requested a review from jopemachine March 20, 2026 04:46
@fregataa fregataa modified the milestones: 26.3, 26.4 Mar 20, 2026
@fregataa fregataa merged commit f4a11e9 into main Mar 20, 2026
33 checks passed
@fregataa fregataa deleted the feature/BA-2938-migrate-session-rbac branch March 20, 2026 05:55
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:L 100~500 LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants