Skip to content

There can be more than Notion and Miro. AFFiNE(pronounced [ə‘fain]) is a next-gen knowledge base that brings planning, sorting and creating all together. Privacy first, open-source, customizable and ready to use.

fix: migration logic (#3238)

Alex Yang1bc427e7a69a
diff --git a/apps/web/src/bootstrap/index.ts b/apps/web/src/bootstrap/index.ts
index 7c398961de..d11bf29810 100644
--- a/apps/web/src/bootstrap/index.ts
+++ b/apps/web/src/bootstrap/index.ts
@@ -4,7 +4,7 @@ import type {
LocalIndexedDBDownloadProvider,
WorkspaceAdapter,
} from '@affine/env/workspace';
-import { WorkspaceFlavour } from '@affine/env/workspace';
+import { WorkspaceFlavour, WorkspaceVersion } from '@affine/env/workspace';
import type { RootWorkspaceMetadata } from '@affine/workspace/atom';
import { workspaceAdaptersAtom } from '@affine/workspace/atom';
import {
@@ -123,6 +123,7 @@ if (environment.isBrowser) {
try {
const metadata = JSON.parse(value) as RootWorkspaceMetadata[];
const promises: Promise<void>[] = [];
+ const newMetadata = [...metadata];
metadata.forEach(oldMeta => {
if (!('version' in oldMeta)) {
const adapter = WorkspaceAdapters[oldMeta.flavour];
@@ -160,6 +161,13 @@ if (environment.isBrowser) {
);
await adapter.CRUD.delete(workspace as any);
+ console.log('migrated', oldMeta.id, newId);
+ const index = newMetadata.findIndex(meta => meta.id === oldMeta.id);
+ newMetadata[index] = {
+ ...oldMeta,
+ id: newId,
+ version: WorkspaceVersion.SubDoc,
+ };
await migrateLocalBlobStorage(workspace.id, newId);
};
@@ -176,6 +184,7 @@ if (environment.isBrowser) {
console.error('migration failed');
})
.finally(() => {
+ localStorage.setItem('jotai-workspaces', JSON.stringify(newMetadata));
window.dispatchEvent(new CustomEvent('migration-done'));
window.$migrationDone = true;
});
diff --git a/packages/workspace/src/atom.ts b/packages/workspace/src/atom.ts
index bb6130781b..22b7283177 100644
--- a/packages/workspace/src/atom.ts
+++ b/packages/workspace/src/atom.ts
@@ -81,6 +81,19 @@ const rootWorkspacesMetadataPromiseAtom = atom<
// fixme(himself65): we might not need step 1
// step 1: try load metadata from localStorage
{
+ // migration step, only data in `METADATA_STORAGE_KEY` will be migrated
+ if (
+ metadata.some(meta => !('version' in meta)) &&
+ !globalThis.$migrationDone
+ ) {
+ await new Promise<void>((resolve, reject) => {
+ signal.addEventListener('abort', () => reject(), { once: true });
+ window.addEventListener('migration-done', () => resolve(), {
+ once: true,
+ });
+ });
+ }
+
// don't change this key,
// otherwise it will cause the data loss in the production
const primitiveMetadata = localStorage.getItem(METADATA_STORAGE_KEY);
@@ -95,19 +108,6 @@ const rootWorkspacesMetadataPromiseAtom = atom<
console.error('cannot parse worksapce', e);
}
}
-
- // migration step, only data in `METADATA_STORAGE_KEY` will be migrated
- if (
- metadata.some(meta => !('version' in meta)) &&
- !globalThis.$migrationDone
- ) {
- await new Promise<void>((resolve, reject) => {
- signal.addEventListener('abort', () => reject(), { once: true });
- window.addEventListener('migration-done', () => resolve(), {
- once: true,
- });
- });
- }
}
// step 2: fetch from adapters
{