mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
feat: support migration (#2852)
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
"./blob": "./src/blob/index.ts",
|
||||
"./utils": "./src/utils.ts",
|
||||
"./type": "./src/type.ts",
|
||||
"./migration": "./src/migration/index.ts",
|
||||
"./local/crud": "./src/local/crud.ts",
|
||||
"./providers": "./src/providers/index.ts",
|
||||
"./affine/*": "./src/affine/*.ts",
|
||||
|
||||
@@ -1,13 +1,27 @@
|
||||
import type { WorkspaceFlavour } from '@affine/env/workspace';
|
||||
import type { WorkspaceVersion } from '@affine/env/workspace';
|
||||
import type { EditorContainer } from '@blocksuite/editor';
|
||||
import { atom } from 'jotai';
|
||||
import { atomWithStorage } from 'jotai/utils';
|
||||
import Router from 'next/router';
|
||||
|
||||
export type RootWorkspaceMetadata = {
|
||||
export type RootWorkspaceMetadataV2 = {
|
||||
id: string;
|
||||
flavour: WorkspaceFlavour;
|
||||
version: WorkspaceVersion;
|
||||
};
|
||||
|
||||
export type RootWorkspaceMetadataV1 = {
|
||||
id: string;
|
||||
flavour: WorkspaceFlavour;
|
||||
// force type check
|
||||
version: undefined;
|
||||
};
|
||||
|
||||
export type RootWorkspaceMetadata =
|
||||
| RootWorkspaceMetadataV1
|
||||
| RootWorkspaceMetadataV2;
|
||||
|
||||
// #region root atoms
|
||||
// root primitive atom that stores the necessary data for the whole app
|
||||
// be careful when you use this atom,
|
||||
|
||||
@@ -64,6 +64,18 @@ export const CRUD: WorkspaceCRUD<WorkspaceFlavour.LOCAL> = {
|
||||
WorkspaceFlavour.LOCAL
|
||||
);
|
||||
BlockSuiteWorkspace.Y.applyUpdate(blockSuiteWorkspace.doc, binary);
|
||||
|
||||
doc.getSubdocs().forEach(subdoc => {
|
||||
blockSuiteWorkspace.doc.getSubdocs().forEach(newDoc => {
|
||||
if (subdoc.guid === newDoc.guid) {
|
||||
BlockSuiteWorkspace.Y.applyUpdate(
|
||||
newDoc,
|
||||
BlockSuiteWorkspace.Y.encodeStateAsUpdate(subdoc)
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
const persistence = createIndexedDBProvider(blockSuiteWorkspace.doc);
|
||||
persistence.connect();
|
||||
await persistence.whenSynced.then(() => {
|
||||
|
||||
51
packages/workspace/src/migration/index.ts
Normal file
51
packages/workspace/src/migration/index.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { migrateToSubdoc } from '@affine/env/blocksuite';
|
||||
import type { LocalWorkspace } from '@affine/env/workspace';
|
||||
import { WorkspaceFlavour } from '@affine/env/workspace';
|
||||
import { createEmptyBlockSuiteWorkspace } from '@affine/workspace/utils';
|
||||
import { nanoid, Workspace } from '@blocksuite/store';
|
||||
import { createIndexeddbStorage } from '@blocksuite/store';
|
||||
const Y = Workspace.Y;
|
||||
|
||||
export function upgradeV1ToV2(oldWorkspace: LocalWorkspace): LocalWorkspace {
|
||||
const oldDoc = oldWorkspace.blockSuiteWorkspace.doc;
|
||||
const newDoc = migrateToSubdoc(oldDoc);
|
||||
if (newDoc === oldDoc) {
|
||||
console.warn('do not need update');
|
||||
return oldWorkspace;
|
||||
} else {
|
||||
const id = nanoid();
|
||||
const newBlockSuiteWorkspace = createEmptyBlockSuiteWorkspace(
|
||||
id,
|
||||
WorkspaceFlavour.LOCAL
|
||||
);
|
||||
Y.applyUpdate(newBlockSuiteWorkspace.doc, Y.encodeStateAsUpdate(newDoc));
|
||||
newDoc.getSubdocs().forEach(subdoc => {
|
||||
newBlockSuiteWorkspace.doc.getSubdocs().forEach(newDoc => {
|
||||
if (subdoc.guid === newDoc.guid) {
|
||||
Y.applyUpdate(newDoc, Y.encodeStateAsUpdate(subdoc));
|
||||
}
|
||||
});
|
||||
});
|
||||
console.log(newBlockSuiteWorkspace.doc.toJSON());
|
||||
|
||||
return {
|
||||
blockSuiteWorkspace: newBlockSuiteWorkspace,
|
||||
flavour: WorkspaceFlavour.LOCAL,
|
||||
id,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export async function migrateLocalBlobStorage(from: string, to: string) {
|
||||
const fromStorage = createIndexeddbStorage(from);
|
||||
const toStorage = createIndexeddbStorage(to);
|
||||
const keys = await fromStorage.crud.list();
|
||||
for (const key of keys) {
|
||||
const value = await fromStorage.crud.get(key);
|
||||
if (!value) {
|
||||
console.warn('cannot find blob:', key);
|
||||
continue;
|
||||
}
|
||||
await toStorage.crud.set(key, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user