diff --git a/src/core/db.ts b/src/core/db.ts index 0a7c9f5..208d4a4 100644 --- a/src/core/db.ts +++ b/src/core/db.ts @@ -192,32 +192,43 @@ export function getDbForGraph(graphName: string): Database.Database { * Migrate existing .memory directory to new graphs system */ export function migrateOldDatabase(): boolean { - const oldDir = path.join(process.cwd(), '.memory'); - const oldDbPath = path.join(oldDir, 'cortex.db'); + try { + const oldDir = path.join(process.cwd(), '.memory'); + const oldDbPath = path.join(oldDir, 'cortex.db'); - if (!fs.existsSync(oldDbPath)) { + if (!fs.existsSync(oldDbPath)) { + return false; + } + + // Check if we've already migrated + const defaultDbPath = getGraphDbPath('default'); + if (fs.existsSync(defaultDbPath)) { + return false; + } + + // Create default graph directory + const defaultGraphDir = path.dirname(defaultDbPath); + fs.mkdirSync(defaultGraphDir, { recursive: true }); + + // Copy old database to new location + fs.copyFileSync(oldDbPath, defaultDbPath); + + // Try to rename old directory as backup (may fail if locked) + try { + const backupDir = `${oldDir}.migrated-${Date.now()}`; + fs.renameSync(oldDir, backupDir); + console.log(`Migrated .memory database to new multi-graph system`); + console.log(`Old database backed up to ${backupDir}`); + } catch { + // Directory might be locked, just leave it + console.log(`Migrated .memory database to new multi-graph system`); + console.log(`Note: Old .memory directory is in use and was not renamed`); + } + + return true; + } catch { + // Migration failed, probably permissions or file lock + // Silently continue - the old database will still work return false; } - - // Check if we've already migrated - const defaultDbPath = getGraphDbPath('default'); - if (fs.existsSync(defaultDbPath)) { - return false; - } - - // Create default graph directory - const defaultGraphDir = path.dirname(defaultDbPath); - fs.mkdirSync(defaultGraphDir, { recursive: true }); - - // Copy old database to new location - fs.copyFileSync(oldDbPath, defaultDbPath); - - // Rename old directory as backup - const backupDir = `${oldDir}.migrated-${Date.now()}`; - fs.renameSync(oldDir, backupDir); - - console.log(`Migrated .memory database to new multi-graph system`); - console.log(`Old database backed up to ${backupDir}`); - - return true; }