Fix UI DuckDB concurrent access with read-only mode
- Add read_only parameter to DataStorage.connect() - UI now connects in read-only mode to avoid lock conflicts with engine
This commit is contained in:
@@ -27,22 +27,24 @@ class DataStorage:
|
|||||||
storage.disconnect()
|
storage.disconnect()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, db_path: Path) -> None:
|
def __init__(self, db_path: Path, *, read_only: bool = False) -> None:
|
||||||
"""Initialize storage with database path.
|
"""Initialize storage with database path.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db_path: Path to DuckDB database file
|
db_path: Path to DuckDB database file
|
||||||
|
read_only: If True, open database in read-only mode (no locking)
|
||||||
"""
|
"""
|
||||||
self.db_path = db_path
|
self.db_path = db_path
|
||||||
|
self._read_only = read_only
|
||||||
self._conn: duckdb.DuckDBPyConnection | None = None
|
self._conn: duckdb.DuckDBPyConnection | None = None
|
||||||
|
|
||||||
def connect(self) -> None:
|
def connect(self) -> None:
|
||||||
"""Connect to the database."""
|
"""Connect to the database."""
|
||||||
# Ensure parent directory exists
|
if not self._read_only:
|
||||||
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
self.db_path.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
self._conn = duckdb.connect(str(self.db_path))
|
self._conn = duckdb.connect(str(self.db_path), read_only=self._read_only)
|
||||||
logger.info("Connected to DuckDB", path=str(self.db_path))
|
logger.info("Connected to DuckDB", path=str(self.db_path), read_only=self._read_only)
|
||||||
|
|
||||||
def disconnect(self) -> None:
|
def disconnect(self) -> None:
|
||||||
"""Close database connection."""
|
"""Close database connection."""
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ def get_storage() -> DataStorage | None:
|
|||||||
if not db_path.exists():
|
if not db_path.exists():
|
||||||
return None
|
return None
|
||||||
|
|
||||||
storage = DataStorage(db_path)
|
storage = DataStorage(db_path, read_only=True)
|
||||||
storage.connect()
|
storage.connect()
|
||||||
return storage
|
return storage
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user