Skip to content

Conversation

@seedspirit
Copy link
Contributor

@seedspirit seedspirit commented Nov 24, 2025

resolves #6888, #6889 (BA-3117, BA-3118)

Apply the DB Source to the Repository within the User resource policy. The DB Source communicates with the actual database, fetching data that meets specified conditions and returning DTOs or Entities to the repository. This enables the Repository layer to combine various Sources without needing to know the detailed implementation of the DB, thereby returning data suitable for the Service layer.

This PR includes the introduction of the DB Source and the resulting modifications to the test code.
As the Repository's current role is not significant, tests communicating with the actual database are conducted within the Repository tests.
The existing integration tests overlap with the newly written tests and are less aligned with Backend.AI's test code writing patterns, so we intend to remove them.

Checklist: (if applicable)

  • Milestone metadata specifying the target backport version
  • Mention to the original issue
  • Installer updates including:
    • Fixtures for db schema changes
    • New mandatory config options
  • Update of end-to-end CLI integration tests in ai.backend.test
  • API server-client counterparts (e.g., manager API -> client SDK)
  • Test case(s) to:
    • Demonstrate the difference of before/after
    • Demonstrate the flow of abstract/conceptual models with a concrete implementation
  • Documentation
    • Contents in the docs directory
    • docstrings in public interfaces and type annotations

📚 Documentation preview 📚: https://sorna--6907.org.readthedocs.build/en/6907/


📚 Documentation preview 📚: https://sorna-ko--6907.org.readthedocs.build/ko/6907/

@seedspirit seedspirit self-assigned this Nov 24, 2025
@github-actions github-actions bot added size:XL 500~ LoC comp:manager Related to Manager component comp:common Related to Common component labels Nov 24, 2025
@github-actions github-actions bot added the area:docs Documentations label Nov 25, 2025
@seedspirit seedspirit marked this pull request as ready for review November 25, 2025 02:14
Copilot AI review requested due to automatic review settings November 25, 2025 02:14
Copilot finished reviewing on behalf of seedspirit November 25, 2025 02:17
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 user resource policy repository to introduce a source-based architecture pattern by adding a DB Source layer that handles database operations. The repository layer now delegates to this DB source, improving separation of concerns and maintainability.

Key changes:

  • Introduced UserResourcePolicyDBSource to encapsulate database operations
  • Refactored UserResourcePolicyRepository to delegate to the new DB source
  • Updated tests from mocked database tests to real database integration tests in the repository layer
  • Replaced generic ObjectNotFound exception with domain-specific UserResourcePolicyNotFound

Reviewed changes

Copilot reviewed 15 out of 16 changed files in this pull request and generated 13 comments.

Show a summary per file
File Description
src/ai/backend/manager/repositories/user_resource_policy/db_source/db_source.py New DB source class implementing database operations for user resource policies
src/ai/backend/manager/repositories/user_resource_policy/repository.py Refactored to delegate CRUD operations to the DB source layer
src/ai/backend/manager/services/user_resource_policy/service.py Simplified to pass creator/modifier objects directly to repository
src/ai/backend/manager/models/resource_policy.py Added from_creator classmethod to construct row from creator object
src/ai/backend/common/exception.py Added domain-specific UserResourcePolicyNotFound exception
src/ai/backend/common/metrics/metric.py Added USER_RESOURCE_POLICY_DB_SOURCE layer type for metrics
tests/manager/repositories/user_resource_policy/test_user_resource_policy_repository.py Migrated from mock-based tests to real database integration tests
tests/manager/services/user_resource_policy/test_user_resource_policy.py Updated assertions to verify creator/modifier objects are passed correctly
tests/manager/integration/services/user_resource_policy/* Removed redundant integration tests that overlap with new repository tests
docs/manager/graphql-reference/*.graphql GraphQL schema changes (unrelated to this PR)
changes/6907.enhance.md Changelog entry for this enhancement

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

You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.

Comment on lines +39 to +43
class UserResourcePolicyDBSource:
"""
Database source for user resource policy operations.
Handles all database operations for user resource policies.
"""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is serializable applied here? It seems like applying Read Committed this time would be worthwhile, don't you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems like applying it in 1.4 would be limited... Let's see after it goes over 2.0 for now.

Co-authored-by: octodog <mu001@lablup.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:docs Documentations comp:common Related to Common component comp:manager Related to Manager component size:XL 500~ LoC

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce DB Source in UserResourcePolicy repository

3 participants