-
Notifications
You must be signed in to change notification settings - Fork 0
Fix vector_full_scan error and implement code quality improvements #19
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
Merged
+345
−124
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e22f611
Initial plan
Copilot 5bff056
Fix vector search initialization and clear_project_data issues
Copilot ba30044
Implement code quality improvements: unified connection and retry uti…
Copilot 1423799
Remove deprecated _retry_on_db_locked function and refactor to use de…
Copilot 90fbbaf
Remove deprecated _get_connection wrapper and use get_db_connection d…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """ | ||
| Unified database connection utilities. | ||
| Provides consistent connection management across all database operations. | ||
| """ | ||
| import os | ||
| import sqlite3 | ||
| from typing import Optional | ||
| from contextlib import contextmanager | ||
| from utils.logger import get_logger | ||
|
|
||
| logger = get_logger(__name__) | ||
|
|
||
|
|
||
| def get_db_connection( | ||
| db_path: str, | ||
| timeout: float = 30.0, | ||
| enable_wal: bool = True, | ||
| enable_vector: bool = False, | ||
| row_factory: bool = True | ||
| ) -> sqlite3.Connection: | ||
| """ | ||
| Create a database connection with consistent configuration. | ||
|
|
||
| Args: | ||
| db_path: Path to the SQLite database file | ||
| timeout: Timeout in seconds for waiting on locks (default: 30.0) | ||
| enable_wal: Enable Write-Ahead Logging mode (default: True) | ||
| enable_vector: Load sqlite-vector extension (default: False) | ||
| row_factory: Use sqlite3.Row factory for dict-like access (default: True) | ||
|
|
||
| Returns: | ||
| sqlite3.Connection object configured for the specified operations | ||
|
|
||
| Raises: | ||
| RuntimeError: If vector extension fails to load when enable_vector=True | ||
| """ | ||
| # Create directory if needed | ||
| dirname = os.path.dirname(os.path.abspath(db_path)) | ||
| if dirname and not os.path.isdir(dirname): | ||
| os.makedirs(dirname, exist_ok=True) | ||
|
|
||
| # Create connection with consistent settings | ||
| conn = sqlite3.connect(db_path, timeout=timeout, check_same_thread=False) | ||
|
|
||
| if row_factory: | ||
| conn.row_factory = sqlite3.Row | ||
|
|
||
| # Enable WAL mode for better concurrency | ||
| if enable_wal: | ||
| try: | ||
| conn.execute("PRAGMA journal_mode = WAL;") | ||
| except Exception as e: | ||
| logger.warning(f"Failed to enable WAL mode: {e}") | ||
|
|
||
| # Set busy timeout (milliseconds) | ||
| try: | ||
| conn.execute(f"PRAGMA busy_timeout = {int(timeout * 1000)};") | ||
| except Exception as e: | ||
| logger.warning(f"Failed to set busy_timeout: {e}") | ||
|
|
||
| # Load vector extension if requested | ||
| if enable_vector: | ||
| from .vector_operations import load_sqlite_vector_extension | ||
| load_sqlite_vector_extension(conn) | ||
| logger.debug(f"Vector extension loaded for connection to {db_path}") | ||
|
|
||
| return conn | ||
|
|
||
|
|
||
| @contextmanager | ||
| def db_connection(db_path: str, **kwargs): | ||
| """ | ||
| Context manager for database connections with automatic cleanup. | ||
|
|
||
| Args: | ||
| db_path: Path to the SQLite database file | ||
| **kwargs: Additional arguments passed to get_db_connection() | ||
|
|
||
| Yields: | ||
| sqlite3.Connection object | ||
|
|
||
| Example: | ||
| with db_connection(db_path) as conn: | ||
| cur = conn.cursor() | ||
| cur.execute("SELECT * FROM files") | ||
| results = cur.fetchall() | ||
| """ | ||
| conn = get_db_connection(db_path, **kwargs) | ||
| try: | ||
| yield conn | ||
| finally: | ||
| try: | ||
| conn.close() | ||
| except Exception as e: | ||
| logger.warning(f"Error closing database connection: {e}") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@copilot I don't care about backward comp atibility,remove it
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.
Removed the deprecated
_get_connectionwrapper function completely. Changes in commit 90fbbaf:get_db_connectionfromdb.connectionget_db_connection(path, timeout=5.0, enable_wal=True)All database operations now use the unified connection function directly with no backward compatibility layer.