-
Notifications
You must be signed in to change notification settings - Fork 62
LCORE-741: refactored code used to connect to databases with quota #699
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LCORE-741: refactored code used to connect to databases with quota #699
Conversation
WalkthroughThis PR extracts DB connection logic from Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Scheduler as quota_scheduler
participant PGMod as quota.connect_pg
participant SQLiteMod as quota.connect_sqlite
participant PGLib as psycopg2
participant SQLiteLib as sqlite3
rect rgba(220,240,255,0.6)
Scheduler->>Scheduler: determine config.db_type
end
alt PostgreSQL
Scheduler->>PGMod: connect_pg(config)
PGMod->>PGLib: psycopg2.connect(config fields)
PGLib-->>PGMod: connection
PGMod-->>PGMod: set autocommit=True
PGMod-->>Scheduler: return connection
else SQLite
Scheduler->>SQLiteMod: connect_sqlite(config)
SQLiteMod->>SQLiteLib: sqlite3.connect(config.db_path)
SQLiteLib-->>SQLiteMod: connection / sqlite3.Error
SQLiteMod-->>Scheduler: return connection or raise error
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
1f41b2b to
16f4139
Compare
16f4139 to
dfe82f1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (4)
src/quota/connect_sqlite.py (2)
6-7: Use absolute imports per coding guidelines.The coding guidelines require absolute imports for internal modules in
src/**/*.pyfiles. These imports should specify the full path from the project root.Based on coding guidelines.
Apply this diff:
-from log import get_logger -from models.config import SQLiteDatabaseConfiguration +from src.log import get_logger +from src.models.config import SQLiteDatabaseConfiguration
12-13: Expand docstring to follow Google Python style.The coding guidelines require complete docstrings with Args, Returns, and Raises sections.
Based on coding guidelines.
Example:
def connect_sqlite(config: SQLiteDatabaseConfiguration) -> Any: - """Initialize connection to database.""" + """Initialize connection to SQLite database. + + Args: + config: SQLite database configuration containing db_path. + + Returns: + A sqlite3 connection object with autocommit enabled. + + Raises: + sqlite3.Error: If connection to the database fails. + """src/quota/connect_pg.py (2)
6-7: Use absolute imports per coding guidelines.The coding guidelines require absolute imports for internal modules in
src/**/*.pyfiles. These imports should specify the full path from the project root.Based on coding guidelines.
Apply this diff:
-from log import get_logger -from models.config import PostgreSQLDatabaseConfiguration +from src.log import get_logger +from src.models.config import PostgreSQLDatabaseConfiguration
12-13: Expand docstring to follow Google Python style.The coding guidelines require complete docstrings with Args, Returns, and Raises sections.
Based on coding guidelines.
Example:
def connect_pg(config: PostgreSQLDatabaseConfiguration) -> Any: - """Initialize connection to PostgreSQL database.""" + """Initialize connection to PostgreSQL database. + + Args: + config: PostgreSQL database configuration with connection parameters. + + Returns: + A psycopg2 connection object with autocommit enabled. + + Raises: + psycopg2.Error: If connection to the database fails. + """
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/quota/connect_pg.py(1 hunks)src/quota/connect_sqlite.py(1 hunks)src/runners/quota_scheduler.py(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
src/**/*.py
📄 CodeRabbit inference engine (CLAUDE.md)
Use absolute imports for internal modules (e.g., from auth import get_auth_dependency)
Files:
src/quota/connect_pg.pysrc/quota/connect_sqlite.pysrc/runners/quota_scheduler.py
**/*.py
📄 CodeRabbit inference engine (CLAUDE.md)
**/*.py: All modules start with descriptive module-level docstrings explaining purpose
Use logger = logging.getLogger(name) for module logging after import logging
Define type aliases at module level for clarity
All functions require docstrings with brief descriptions
Provide complete type annotations for all function parameters and return types
Use typing_extensions.Self in model validators where appropriate
Use modern union syntax (str | int) and Optional[T] or T | None consistently
Function names use snake_case with descriptive, action-oriented prefixes (get_, validate_, check_)
Avoid in-place parameter modification; return new data structures instead of mutating arguments
Use appropriate logging levels: debug, info, warning, error with clear messages
All classes require descriptive docstrings explaining purpose
Class names use PascalCase with conventional suffixes (Configuration, Error/Exception, Resolver, Interface)
Abstract base classes should use abc.ABC and @AbstractMethod for interfaces
Provide complete type annotations for all class attributes
Follow Google Python docstring style for modules, classes, and functions, including Args, Returns, Raises, Attributes sections as needed
Files:
src/quota/connect_pg.pysrc/quota/connect_sqlite.pysrc/runners/quota_scheduler.py
🧬 Code graph analysis (3)
src/quota/connect_pg.py (3)
src/log.py (1)
get_logger(7-13)src/models/config.py (2)
config(140-146)PostgreSQLDatabaseConfiguration(87-105)src/runners/quota_scheduler.py (1)
connect(203-210)
src/quota/connect_sqlite.py (3)
src/log.py (1)
get_logger(7-13)src/models/config.py (2)
config(140-146)SQLiteDatabaseConfiguration(75-78)src/runners/quota_scheduler.py (1)
connect(203-210)
src/runners/quota_scheduler.py (2)
src/quota/connect_pg.py (1)
connect_pg(12-27)src/quota/connect_sqlite.py (1)
connect_sqlite(12-26)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: build-pr
- GitHub Check: e2e_tests (ci)
- GitHub Check: e2e_tests (azure)
🔇 Additional comments (2)
src/quota/connect_pg.py (1)
22-22: Clarify the commented out SSL certificate parameter.The
sslrootcertparameter is commented out, yetPostgreSQLDatabaseConfigurationincludes aca_cert_pathfield. This impacts SSL/TLS certificate verification.Please verify:
- Is SSL certificate verification intentionally disabled?
- Should this parameter be enabled when
ca_cert_pathis provided?- If intentionally disabled, add a comment explaining why.
If this was left commented during development, consider:
sslmode=config.ssl_mode, - # sslrootcert=config.ca_cert_path, + sslrootcert=config.ca_cert_path if config.ca_cert_path else None, gssencmode=config.gss_encmode,src/runners/quota_scheduler.py (1)
16-17: LGTM! Clean refactoring.The imports and delegation to external connection functions are correctly implemented. The refactoring successfully externalizes the connection logic while preserving the original behavior.
Also applies to: 203-210
src/quota/connect_sqlite.py
Outdated
| except sqlite3.Error as e: | ||
| if connection is not None: | ||
| connection.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Remove unreachable dead code in exception handler.
If sqlite3.connect() raises an exception at line 19, the assignment never completes and connection remains None from line 17. Therefore, the check at line 21 will always be False, and line 22 is unreachable.
Apply this diff:
try:
connection = sqlite3.connect(database=config.db_path)
except sqlite3.Error as e:
- if connection is not None:
- connection.close()
logger.exception("Error initializing SQLite cache:\n%s", e)
raise📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| except sqlite3.Error as e: | |
| if connection is not None: | |
| connection.close() | |
| connection = None | |
| try: | |
| connection = sqlite3.connect(database=config.db_path) | |
| except sqlite3.Error as e: | |
| logger.exception("Error initializing SQLite cache:\n%s", e) | |
| raise |
🤖 Prompt for AI Agents
In src/quota/connect_sqlite.py around lines 20 to 22, the exception handler
contains an unreachable check that tests and closes 'connection' even though
sqlite3.connect() failed and 'connection' remains None; remove the dead if-block
(the conditional and connection.close() call) from the except clause so the
handler no longer references a non-existent connection, and ensure any necessary
error handling or re-raise remains in place.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
Description
LCORE-741: refactored code used to connect to databases with quota
Type of change
Related Tickets & Documents
Summary by CodeRabbit
Refactor
Bug Fixes