Skip to content

Commit 7624a20

Browse files
authored
feat: isolate default sqlite db by config dir (#567)
Signed-off-by: phernandez <paul@basicmachines.co>
1 parent d84708c commit 7624a20

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

src/basic_memory/config.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,11 @@ def app_database_path(self) -> Path:
397397
398398
This is the single database that will store all knowledge data
399399
across all projects.
400+
401+
Uses BASIC_MEMORY_CONFIG_DIR when set so each process/worktree can
402+
isolate both config and database state.
400403
"""
401-
database_path = Path.home() / DATA_DIR_NAME / APP_DATABASE_NAME
404+
database_path = self.data_dir_path / APP_DATABASE_NAME
402405
if not database_path.exists(): # pragma: no cover
403406
database_path.parent.mkdir(parents=True, exist_ok=True)
404407
database_path.touch()
@@ -447,8 +450,13 @@ def ensure_project_paths_exists(self) -> "BasicMemoryConfig": # pragma: no cove
447450
return self
448451

449452
@property
450-
def data_dir_path(self):
451-
return Path.home() / DATA_DIR_NAME
453+
def data_dir_path(self) -> Path:
454+
"""Get app state directory for config and default SQLite database."""
455+
if config_dir := os.getenv("BASIC_MEMORY_CONFIG_DIR"):
456+
return Path(config_dir)
457+
458+
home = os.getenv("HOME", Path.home())
459+
return Path(home) / DATA_DIR_NAME
452460

453461

454462
# Module-level cache for configuration

tests/test_config.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,25 @@ def test_basic_memory_home_overrides_existing_main_project(self, config_home, mo
9797
# Note: This tests the current behavior where default_factory takes precedence
9898
assert config.projects["main"] == original_path
9999

100+
def test_app_database_path_uses_custom_config_dir(self, tmp_path, monkeypatch):
101+
"""Default SQLite DB should live under BASIC_MEMORY_CONFIG_DIR when set."""
102+
custom_config_dir = tmp_path / "instance-a" / "state"
103+
monkeypatch.setenv("BASIC_MEMORY_CONFIG_DIR", str(custom_config_dir))
104+
105+
config = BasicMemoryConfig(projects={"main": str(tmp_path / "project")})
106+
107+
assert config.data_dir_path == custom_config_dir
108+
assert config.app_database_path == custom_config_dir / "memory.db"
109+
assert config.app_database_path.exists()
110+
111+
def test_app_database_path_defaults_to_home_data_dir(self, config_home, monkeypatch):
112+
"""Without BASIC_MEMORY_CONFIG_DIR, default DB stays at ~/.basic-memory/memory.db."""
113+
monkeypatch.delenv("BASIC_MEMORY_CONFIG_DIR", raising=False)
114+
config = BasicMemoryConfig()
115+
116+
assert config.data_dir_path == config_home / ".basic-memory"
117+
assert config.app_database_path == config_home / ".basic-memory" / "memory.db"
118+
100119

101120
class TestConfigManager:
102121
"""Test ConfigManager functionality."""

0 commit comments

Comments
 (0)