Skip to content

Conversation

@tisnik
Copy link
Contributor

@tisnik tisnik commented Oct 21, 2025

Description

LCORE-741: refactored code used to connect to databases with quota

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement

Related Tickets & Documents

  • Related Issue #LCORE-741

Summary by CodeRabbit

  • Refactor

    • Moved database connection logic into dedicated PostgreSQL and SQLite connection handlers and updated the quota scheduler to use them.
  • Bug Fixes

    • SQLite handler improved error handling to leave connections in a known state on failure.
    • PostgreSQL handler now opens connections with autocommit enabled and logs connection outcomes.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 21, 2025

Walkthrough

This PR extracts DB connection logic from src/runners/quota_scheduler.py into two new modules (src/quota/connect_pg.py, src/quota/connect_sqlite.py) and updates quota_scheduler.py to import and delegate to those new connection functions.

Changes

Cohort / File(s) Summary
PostgreSQL connection module
src/quota/connect_pg.py
Adds connect_pg(config: PostgreSQLDatabaseConfiguration) -> Any. Uses psycopg2.connect(...) with fields from config (host, port, user, password, dbname, sslmode, gssencmode), sets autocommit=True, logs attempts via module logger, handles exceptions by logging and re-raising, and returns the connection. References PostgreSQLDatabaseConfiguration from models.config.
SQLite connection module
src/quota/connect_sqlite.py
Adds connect_sqlite(config: SQLiteDatabaseConfiguration) -> Any. Uses sqlite3.connect(config.db_path), logs the attempt, sets connection behavior for autocommit, returns the connection, and on sqlite3.Error logs and re-raises the exception (no silent close of partial connection). References SQLiteDatabaseConfiguration from models.config.
Refactored scheduler (delegation only)
src/runners/quota_scheduler.py
Removes in-file connect_pg and connect_sqlite implementations and related direct DB-configuration imports. Adds imports connect_pg and connect_sqlite from quota.connect_pg / quota.connect_sqlite. connect(config) delegates to the imported functions; other behavior unchanged.

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
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐇 I hopped through files with careful flair,

Moved burrows for Postgres and SQLite there.
Each tunnel logged, each stitch re-tied,
Connections snug where they now reside.
A little hop, a cleaner stride.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The pull request title "LCORE-741: refactored code used to connect to databases with quota" accurately captures the primary objective of the changeset. The main change is the extraction of database connection logic (for both PostgreSQL and SQLite) from src/runners/quota_scheduler.py into dedicated modules (src/quota/connect_pg.py and src/quota/connect_sqlite.py), which is a classic refactoring pattern. The title is concise, clear, and uses specific terminology ("refactored," "connect to databases") that allows a teammate scanning the history to immediately understand the nature and scope of the changes. The ticket reference adds appropriate context without adding noise.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between dfe82f1 and e0c19d1.

📒 Files selected for processing (2)
  • src/quota/connect_pg.py (1 hunks)
  • src/quota/connect_sqlite.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/quota/connect_pg.py
  • src/quota/connect_sqlite.py

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@tisnik tisnik force-pushed the lcore-741-refactored-code-to-connect-to-databases branch from 1f41b2b to 16f4139 Compare October 21, 2025 07:18
@tisnik tisnik force-pushed the lcore-741-refactored-code-to-connect-to-databases branch from 16f4139 to dfe82f1 Compare October 21, 2025 07:21
Copy link
Contributor

@coderabbitai coderabbitai bot left a 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/**/*.py files. 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/**/*.py files. 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

📥 Commits

Reviewing files that changed from the base of the PR and between ddc9c6c and dfe82f1.

📒 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.py
  • src/quota/connect_sqlite.py
  • src/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.py
  • src/quota/connect_sqlite.py
  • src/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 sslrootcert parameter is commented out, yet PostgreSQLDatabaseConfiguration includes a ca_cert_path field. This impacts SSL/TLS certificate verification.

Please verify:

  1. Is SSL certificate verification intentionally disabled?
  2. Should this parameter be enabled when ca_cert_path is provided?
  3. 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

Comment on lines 20 to 22
except sqlite3.Error as e:
if connection is not None:
connection.close()
Copy link
Contributor

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.

Suggested change
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.

@tisnik
Copy link
Contributor Author

tisnik commented Oct 21, 2025

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 21, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@tisnik tisnik merged commit d8e41a2 into lightspeed-core:main Oct 21, 2025
18 of 20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant