Fix migration to handle locked directories gracefully
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user