Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions scripts/commands/graph_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from typing import Any, Dict, Optional

from commands import register_command
from utils import default_db_path as _default_db_path


def add_args(parser):
Expand All @@ -26,11 +27,6 @@ def add_args(parser):
help="Custom path for SQLite database file")


def _default_db_path(workspace: str) -> str:
"""Return the default SQLite db path for a workspace."""
return os.path.join(workspace, ".codelens", "codelens.db")


def get_graph_schema(workspace: str, db_path: Optional[str] = None) -> Dict[str, Any]:
"""Return the shape of the graph (node/edge counts + type distribution).

Expand Down
7 changes: 1 addition & 6 deletions scripts/graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
from collections import deque
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple

from utils import logger
from utils import default_db_path as _default_db_path, logger


# ─── Schema Constants ─────────────────────────────────────────
Expand Down Expand Up @@ -158,11 +158,6 @@ def init_graph_schema(conn: sqlite3.Connection) -> None:

# ─── Population ───────────────────────────────────────────────

def _default_db_path(workspace: str) -> str:
"""Return the default SQLite db path for a workspace."""
return os.path.join(workspace, ".codelens", "codelens.db")


def _parse_file_line_from_node_id(node_id: str) -> Tuple[str, int]:
"""Extract (file, line) from a flat-registry node id like 'path/file.py:42:fn_name'.

Expand Down
9 changes: 3 additions & 6 deletions scripts/persistent_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
from datetime import datetime, timezone
from typing import Any, Dict, List, Optional, Set, Tuple

from utils import logger
from utils import default_db_path, logger


# ─── Schema Version ────────────────────────────────────────────

SCHEMA_VERSION = 1
DB_FILENAME = "codelens.db"

# ─── SQL Statements ────────────────────────────────────────────

Expand Down Expand Up @@ -129,9 +128,7 @@ def __init__(self, workspace: str, db_path: Optional[str] = None):
Defaults to .codelens/codelens.db
"""
self.workspace = workspace
self._db_path = db_path or os.path.join(
workspace, ".codelens", DB_FILENAME
)
self._db_path = db_path or default_db_path(workspace)
self._local = threading.local() # Thread-local storage for connections
self._initialized = False
self._init_lock = threading.Lock()
Expand Down Expand Up @@ -855,7 +852,7 @@ def vacuum(self) -> None:

def db_exists(workspace: str, db_path: Optional[str] = None) -> bool:
"""Check if a CodeLens SQLite database exists for the workspace."""
path = db_path or os.path.join(workspace, ".codelens", DB_FILENAME)
path = db_path or default_db_path(workspace)
return os.path.exists(path)


Expand Down
9 changes: 9 additions & 0 deletions scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def get_logger(name: str = "codelens") -> logging.Logger:

# ─── Shared Configuration ───────────────────────────────────

DB_FILENAME = "codelens.db"


def default_db_path(workspace: str) -> str:
"""Return the default SQLite database path for the workspace."""
safe_workspace = os.path.abspath(workspace)
return os.path.join(safe_workspace, ".codelens", DB_FILENAME)


DEFAULT_IGNORE_DIRS = frozenset({
'node_modules', '.git', 'dist', 'build', 'target',
'__pycache__', '.codelens', '.next', '.nuxt', '.cache',
Expand Down
23 changes: 23 additions & 0 deletions tests/test_graph_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ def scanned_clean_app():
shutil.rmtree(workspace, ignore_errors=True)


# ─── 0. Default DB Path ───────────────────────────────────────


class TestDefaultDbPath:
"""All call sites must resolve the same default SQLite path."""

def test_default_db_path_consistent_across_modules(self, tmp_path):
workspace = str(tmp_path / "codelens-workspace")

from utils import default_db_path
from graph_model import _default_db_path
from commands.graph_schema import _default_db_path as schema_default_db_path
from persistent_registry import PersistentRegistry, db_exists

expected = default_db_path(workspace)
assert _default_db_path(workspace) == expected
assert schema_default_db_path(workspace) == expected

registry = PersistentRegistry(workspace)
assert registry.db_path == expected
assert db_exists(workspace) == os.path.exists(expected)


# ─── 1. Schema Initialization ────────────────────────────────


Expand Down