|
| 1 | +# App-Level Database Refactoring |
| 2 | + |
| 3 | +This document outlines the plan for migrating Basic Memory from per-project SQLite databases to a single app-level database that manages all knowledge data across projects. |
| 4 | + |
| 5 | +## Goals |
| 6 | + |
| 7 | +- Move to a single app-level SQLite database for all knowledge data |
| 8 | +- Deprecate per-project databases completely |
| 9 | +- Add project information to entities, observations, and relations |
| 10 | +- Simplify project switching and management |
| 11 | +- Enable better multi-project support for the Pro app |
| 12 | +- Prepare for cloud/GoHighLevel integration |
| 13 | + |
| 14 | +## Architecture Changes |
| 15 | + |
| 16 | +We're moving from: |
| 17 | +``` |
| 18 | +~/.basic-memory/config.json (project list) |
| 19 | +~/basic-memory/[project-name]/.basic-memory/memory.db (one DB per project) |
| 20 | +``` |
| 21 | + |
| 22 | +To: |
| 23 | +``` |
| 24 | +~/.basic-memory/config.json (project list) <- same |
| 25 | +~/.basic-memory/memory.db (app-level DB with project/entity/observation/search_index tables) |
| 26 | +~/basic-memory/[project-name]/.basic-memory/memory.db (project DBs deprecated) <- we are removing these |
| 27 | +``` |
| 28 | + |
| 29 | +## Implementation Tasks |
| 30 | + |
| 31 | +### 1. Configuration Changes |
| 32 | + |
| 33 | +- [x] Update config.py to use a single app database for all projects |
| 34 | +- [x] Add functions to get app database path for all operations |
| 35 | +- [x] Keep JSON-based config.json for project listing/paths |
| 36 | +- [x] Update project configuration loading to use app DB for all operations |
| 37 | + |
| 38 | + |
| 39 | +### 3. Project Model Implementation |
| 40 | + |
| 41 | +- [x] Create Project SQLAlchemy model in models/project.py |
| 42 | +- [x] Define attributes: id, name, path, config, etc. |
| 43 | +- [x] Add proper indexes and constraints |
| 44 | +- [x] Add project_id foreign key to Entity, Observation, and Relation models |
| 45 | +- [x] Create migration script for updating schema with project relations |
| 46 | +- [x] Implement app DB initialization with project table |
| 47 | + |
| 48 | +### 4. Repository Layer Updates |
| 49 | + |
| 50 | +- [x] Create ProjectRepository for CRUD operations on Project model |
| 51 | +- [x] Update base Repository class to filter queries by project_id |
| 52 | +- [x] Update existing repositories to use project context automatically |
| 53 | +- [x] Implement query scoping to specific projects |
| 54 | +- [x] Add functions for project context management |
| 55 | + |
| 56 | +### 5. Search Functionality Updates |
| 57 | + |
| 58 | +- [x] Update search_index table to include project_id |
| 59 | +- [x] Modify search queries to filter by project_id |
| 60 | +- [x] Update FTS (Full Text Search) to be project-aware |
| 61 | +- [x] Add appropriate indices for efficient project-scoped searches |
| 62 | +- [x] Update search repository for project context |
| 63 | + |
| 64 | +### 6. Service Layer Updates |
| 65 | + |
| 66 | +- [x] Update ProjectService to manage projects in the database |
| 67 | +- [x] Add methods for project creation, deletion, updating |
| 68 | +- [x] Modify existing services to use project context |
| 69 | +- [x] Update initialization service for app DB setup |
| 70 | +- [x] ~~Implement project switching logic~~ |
| 71 | + |
| 72 | +### 7. Sync Service Updates |
| 73 | + |
| 74 | +- [x] Modify background sync service to handle project context |
| 75 | +- [x] Update file watching to support multiple project directories |
| 76 | +- [x] Add project context to file sync events |
| 77 | +- [x] Update file path resolution to respect project boundaries |
| 78 | +- [x] Handle file change detection with project awareness |
| 79 | + |
| 80 | +### 8. API Layer Updates |
| 81 | + |
| 82 | +- [x] Update API endpoints to include project context |
| 83 | +- [x] Create new endpoints for project management |
| 84 | +- [x] Modify dependency injection to include project context |
| 85 | +- [x] Add request/response models for project operations |
| 86 | +- [x] ~~Implement middleware for project context handling~~ |
| 87 | +- [x] Update error handling to include project information |
| 88 | + |
| 89 | +### 9. MCP Tools Updates |
| 90 | + |
| 91 | +- [x] Update MCP tools to include project context |
| 92 | +- [x] Add project selection capabilities to MCP server |
| 93 | +- [x] Update context building to respect project boundaries |
| 94 | +- [x] Update file operations to handle project paths correctly |
| 95 | +- [x] Add project-aware helper functions for MCP tools |
| 96 | + |
| 97 | +### 10. CLI Updates |
| 98 | + |
| 99 | +- [x] Update CLI commands to work with app DB |
| 100 | +- [x] Add or update project management commands |
| 101 | +- [x] Implement project switching via app DB |
| 102 | +- [x] Ensure CLI help text reflects new project structure |
| 103 | +- [x] ~~Add migration commands for existing projects~~ |
| 104 | +- [x] Update project CLI commands to use the API with direct config fallback |
| 105 | +- [x] Added tests for CLI project commands |
| 106 | + |
| 107 | +### 11. Performance Optimizations |
| 108 | + |
| 109 | +- [x] Add proper indices for efficient project filtering |
| 110 | +- [x] Optimize queries for multi-project scenarios |
| 111 | +- [x] ~~Add query caching if needed~~ |
| 112 | +- [x] Monitor and optimize performance bottlenecks |
| 113 | + |
| 114 | +### 12. Testing Updates |
| 115 | + |
| 116 | +- [x] Update test fixtures to support project context |
| 117 | +- [x] Add multi-project testing scenarios |
| 118 | +- [x] Create tests for migration processes |
| 119 | +- [ ] Test performance with larger multi-project datasets |
| 120 | + |
| 121 | +### 13 Migrations |
| 122 | + |
| 123 | +- [x] project table |
| 124 | +- [x] search project_id index |
| 125 | +- [x] project import/sync - during initialization |
| 126 | + |
| 127 | +## Database Schema Changes |
| 128 | + |
| 129 | +### New Project Table |
| 130 | +```sql |
| 131 | +CREATE TABLE project ( |
| 132 | + id INTEGER PRIMARY KEY, |
| 133 | + name TEXT NOT NULL UNIQUE, |
| 134 | + description TEXT, |
| 135 | + path TEXT NOT NULL, |
| 136 | + config JSON, |
| 137 | + is_active BOOLEAN DEFAULT TRUE, |
| 138 | + is_default BOOLEAN DEFAULT FALSE, |
| 139 | + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, |
| 140 | + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP |
| 141 | +); |
| 142 | +``` |
| 143 | + |
| 144 | +### Modified Entity Table |
| 145 | +```sql |
| 146 | +ALTER TABLE entity ADD COLUMN project_id INTEGER REFERENCES project(id); |
| 147 | +CREATE INDEX ix_entity_project_id ON entity(project_id); |
| 148 | +``` |
| 149 | + |
| 150 | +### Modified Observation Table |
| 151 | +```sql |
| 152 | +-- No direct changes needed as observations are linked to entities which have project_id |
| 153 | +CREATE INDEX ix_observation_entity_project_id ON observation(entity_id, project_id); |
| 154 | +``` |
| 155 | + |
| 156 | +### Modified Relation Table |
| 157 | +```sql |
| 158 | +-- No direct changes needed as relations are linked to entities which have project_id |
| 159 | +CREATE INDEX ix_relation_from_project_id ON relation(from_id, project_id); |
| 160 | +CREATE INDEX ix_relation_to_project_id ON relation(to_id, project_id); |
| 161 | +``` |
| 162 | + |
| 163 | +## Migration Path |
| 164 | + |
| 165 | +For existing projects, we'll: |
| 166 | +1. Create the project table in the app database |
| 167 | +2. For each project in config.json: |
| 168 | + a. Register the project in the project table |
| 169 | + b. Import all entities, observations, and relations from the project's DB |
| 170 | + c. Set the project_id on all imported records |
| 171 | +3. Validate that all data has been migrated correctly |
| 172 | +4. Keep config.json but use the database as the source of truth |
| 173 | + |
| 174 | +## Testing |
| 175 | + |
| 176 | +- [x] Test project creation, switching, deletion |
| 177 | +- [x] Test knowledge operations (entity, observation, relation) with project context |
| 178 | +- [x] Verify existing projects can be migrated successfully |
| 179 | +- [x] Test multi-project operations |
| 180 | +- [x] Test error cases (missing project, etc.) |
| 181 | +- [x] Test CLI commands with multiple projects |
| 182 | +- [x] Test CLI error handling for API failures |
| 183 | +- [x] Test CLI commands use only API, no config fallback |
| 184 | + |
| 185 | +## Current Status |
| 186 | + |
| 187 | +The app-level database refactoring is now complete! We have successfully: |
| 188 | + |
| 189 | +1. Migrated from per-project SQLite databases to a single app-level database |
| 190 | +2. Added project context to all layers of the application (models, repositories, services, API) |
| 191 | +3. Implemented bidirectional synchronization between config.json and the database |
| 192 | +4. Updated all API endpoints to include project context |
| 193 | +5. Enhanced project management capabilities in both the API and CLI |
| 194 | +6. Added comprehensive test coverage for project operations |
| 195 | +7. Modified the directory router and all other routers to respect project boundaries |
| 196 | + |
| 197 | +The only remaining task is to thoroughly test performance with larger multi-project datasets, which can be done as part of regular usage monitoring. |
| 198 | + |
| 199 | +## CLI API Integration |
| 200 | + |
| 201 | +The CLI commands have been updated to use the API endpoints for project management operations. This includes: |
| 202 | + |
| 203 | +1. The `project list` command now fetches projects from the API |
| 204 | +2. The `project add` command creates projects through the API |
| 205 | +3. The `project remove` command removes projects through the API |
| 206 | +4. The `project default` command sets the default project through the API |
| 207 | +5. Added a new `project sync` command to synchronize projects between config and database |
| 208 | +6. The `project current` command now shows detailed project information from the API |
| 209 | + |
| 210 | +This approach ensures that project operations performed through the CLI are synchronized with the database, maintaining consistency between the configuration file and the app-level database. Failed API requests result in a proper error message instructing the user to ensure the Basic Memory server is running, rather than falling back to direct config updates. This ensures that the database remains the single source of truth for project information. |
0 commit comments