Fix migration to handle locked directories gracefully

This commit is contained in:
2026-02-03 11:51:58 +01:00
parent f21426fc43
commit d8addcfcb7

View File

@@ -192,6 +192,7 @@ export function getDbForGraph(graphName: string): Database.Database {
* Migrate existing .memory directory to new graphs system * Migrate existing .memory directory to new graphs system
*/ */
export function migrateOldDatabase(): boolean { export function migrateOldDatabase(): boolean {
try {
const oldDir = path.join(process.cwd(), '.memory'); const oldDir = path.join(process.cwd(), '.memory');
const oldDbPath = path.join(oldDir, 'cortex.db'); const oldDbPath = path.join(oldDir, 'cortex.db');
@@ -212,12 +213,22 @@ export function migrateOldDatabase(): boolean {
// Copy old database to new location // Copy old database to new location
fs.copyFileSync(oldDbPath, defaultDbPath); fs.copyFileSync(oldDbPath, defaultDbPath);
// Rename old directory as backup // Try to rename old directory as backup (may fail if locked)
try {
const backupDir = `${oldDir}.migrated-${Date.now()}`; const backupDir = `${oldDir}.migrated-${Date.now()}`;
fs.renameSync(oldDir, backupDir); fs.renameSync(oldDir, backupDir);
console.log(`Migrated .memory database to new multi-graph system`); console.log(`Migrated .memory database to new multi-graph system`);
console.log(`Old database backed up to ${backupDir}`); 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; return true;
} catch {
// Migration failed, probably permissions or file lock
// Silently continue - the old database will still work
return false;
}
} }