mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 05:14:54 +00:00
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:
@@ -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>
|
||||
|
||||
@@ -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),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user