Skip to content

refactor(BA-4080): add DB Source Pattern in User repository#8455

Merged
jopemachine merged 6 commits intomainfrom
refactor/BA-4080-introduce-db-source-pattern-in-user-repository
Feb 2, 2026
Merged

refactor(BA-4080): add DB Source Pattern in User repository#8455
jopemachine merged 6 commits intomainfrom
refactor/BA-4080-introduce-db-source-pattern-in-user-repository

Conversation

@MinJuTur
Copy link
Copy Markdown
Contributor

@MinJuTur MinJuTur commented Jan 30, 2026

Summary

  • Extract all DB session-opening logic from UserRepository into UserDBSource, following the established db_source pattern used across other repository domains
  • UserRepository now delegates pure DB methods to UserDBSource while retaining resilience decorators and orchestration logic
  • Mixed methods (delete_vfolders, delete_keypairs_with_valkey, _get_time_binned_monthly_stats) are restructured so that DB access is handled by UserDBSource with no behavioral changes

Related Issue

Test plan

  • pants check passes on all modified files
  • pants lint passes on all modified files
  • Existing unit tests pass (test_user_repository.py)

🤖 Generated with Claude Code

Copilot AI review requested due to automatic review settings January 30, 2026 05:17
@MinJuTur MinJuTur added this to the 26.2 milestone Jan 30, 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

This PR refactors the UserRepository by extracting all DB session-opening logic into a new UserDBSource class, following the established db_source pattern used across other repository domains (e.g., AgentDBSource, ArtifactDBSource, DeploymentDBSource).

Changes:

  • Created UserDBSource class that handles all database operations with session management
  • UserRepository now delegates pure DB methods to UserDBSource while retaining resilience decorators
  • Test files updated to access internal methods through the new _db_source attribute

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

File Description
src/ai/backend/manager/repositories/user/repository.py Refactored to delegate DB operations to UserDBSource, keeping only orchestration logic and resilience decorators
src/ai/backend/manager/repositories/user/db_source/db_source.py New file containing all DB session-opening logic and data access operations extracted from UserRepository
src/ai/backend/manager/repositories/user/db_source/__init__.py New module initialization file exporting UserDBSource
tests/unit/manager/repositories/user/test_user_repository.py Updated test calls to _validate_user_access to access it through _db_source

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

Comment thread src/ai/backend/manager/repositories/user/db_source/db_source.py
Comment thread src/ai/backend/manager/repositories/user/db_source/db_source.py Outdated
Comment thread src/ai/backend/manager/repositories/user/db_source/db_source.py Outdated
Comment thread src/ai/backend/manager/repositories/user/db_source/db_source.py

return len(target_vfs)

async def delete_keypairs_with_valkey(
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Similarly, this method takes ValkeyStatClient as a parameter because the original _delete_keypairs_with_valkey performed Valkey cleanup and DB deletion within a single transaction. Splitting them would change the transaction boundary — if Valkey cleanup fails, the DB transaction should roll back, which requires both operations to be in the same begin() scope.

result = await conn.execute(query)
rows = result.fetchall()
# DB query via db_source
rows = await self._db_source.get_kernel_rows_for_monthly_stats(user_uuid)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Only the DB query part was extracted to db_source

Comment thread tests/unit/manager/repositories/user/test_user_repository.py Outdated
Comment thread src/ai/backend/manager/repositories/user/db_source/db_source.py Outdated
@MinJuTur MinJuTur requested a review from jopemachine January 30, 2026 07:38
Comment thread src/ai/backend/manager/repositories/user/db_source/db_source.py Outdated
@MinJuTur MinJuTur requested a review from jopemachine February 2, 2026 04:12
MinJuTur and others added 6 commits February 2, 2026 04:17
Extract all DB session-opening logic from UserRepository into UserDBSource,
following the established db_source pattern used in other repository domains.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The `except VFolderOperationFailed: raise` block has no effect and
can be safely removed along with its unused import.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
SQLAlchemy 2.0 properly infers scalar() return types, making
explicit cast(Optional[str], result) redundant.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The method always returns True and is never called in production
code. Remove it along with the 3 associated test cases.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Remove cast(list[VFolderRow], ...) and cast(Optional[UUID], ...)
that are no longer needed with SQLAlchemy 2.0 type inference.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@MinJuTur MinJuTur force-pushed the refactor/BA-4080-introduce-db-source-pattern-in-user-repository branch from a454479 to 1135c84 Compare February 2, 2026 04:26
@github-actions github-actions Bot added size:XL 500~ LoC comp:manager Related to Manager component labels Feb 2, 2026
Comment on lines +298 to +301
await execute_batch_purger(session, create_user_error_log_purger(user_uuid))
await execute_batch_purger(session, create_user_keypair_purger(user_uuid))
await execute_batch_purger(session, create_user_vfolder_permission_purger(user_uuid))
await execute_batch_purger(session, create_user_group_association_purger(user_uuid))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Resolved merge conflict with purge_user from main, integrated into db_source pattern

@jopemachine
Copy link
Copy Markdown
Member

Overall, there seems to be a lot of code that needs improvement, but it doesn’t seem appropriate to modify the existing code here.

@jopemachine jopemachine added this pull request to the merge queue Feb 2, 2026
Merged via the queue into main with commit b1df76b Feb 2, 2026
30 checks passed
@jopemachine jopemachine deleted the refactor/BA-4080-introduce-db-source-pattern-in-user-repository branch February 2, 2026 07:59
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 Intern-OKR size:XL 500~ LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants