fix(electron): clone db file when enable cloud for desktop (#5028)

At the moment on desktop the user's local blob data will be lost after enable cloud.
This is because blob data is only synced from old idb to new idb, but not sync into sqlitedb.

This pr will simply clone the db file for desktop app. It should also speed up the time when enabling cloud for a large local workspace.
This commit is contained in:
Peng Xiao
2023-11-23 07:23:16 +00:00
parent 9ded6afb4b
commit 7463e87742
4 changed files with 65 additions and 22 deletions

View File

@@ -8,7 +8,9 @@ import type { WorkspaceMeta } from '../type';
import {
getDeletedWorkspacesBasePath,
getWorkspaceBasePath,
getWorkspaceDBPath,
getWorkspaceMeta,
getWorkspaceMetaPath,
getWorkspacesBasePath,
} from './meta';
import { workspaceSubjects } from './subjects';
@@ -53,6 +55,34 @@ export async function deleteWorkspace(id: string) {
}
}
export async function cloneWorkspace(id: string, newId: string) {
const dbPath = await getWorkspaceDBPath(id);
const newBasePath = await getWorkspaceBasePath(newId);
const newDbPath = await getWorkspaceDBPath(newId);
const metaPath = await getWorkspaceMetaPath(newId);
// check if new workspace dir exists
if (
await fs
.access(newBasePath)
.then(() => true)
.catch(() => false)
) {
throw new Error(`workspace ${newId} already exists`);
}
try {
await fs.ensureDir(newBasePath);
const meta = {
id: newId,
mainDBPath: newDbPath,
};
await fs.writeJSON(metaPath, meta);
await fs.copy(dbPath, newDbPath);
} catch (error) {
logger.error('cloneWorkspace', error);
}
}
export async function storeWorkspaceMeta(
workspaceId: string,
meta: Partial<WorkspaceMeta>

View File

@@ -1,5 +1,5 @@
import type { MainEventRegister, WorkspaceMeta } from '../type';
import { deleteWorkspace, listWorkspaces } from './handlers';
import { cloneWorkspace, deleteWorkspace, listWorkspaces } from './handlers';
import { getWorkspaceMeta } from './meta';
import { workspaceSubjects } from './subjects';
@@ -23,4 +23,5 @@ export const workspaceHandlers = {
getMeta: async (id: string) => {
return getWorkspaceMeta(id);
},
clone: async (id: string, newId: string) => cloneWorkspace(id, newId),
};