mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-18 14:56:59 +08:00
fix(infra): compatibility fix for space prefix (#4912)
It seems there are some cases that [this upstream PR](https://github.com/toeverything/blocksuite/pull/4747) will cause data loss.
Because of some historical reasons, the page id could be different with its doc id.
It might be caused by subdoc migration in the following (not 100% sure if all white screen issue is caused by it) 0714c12703/packages/common/infra/src/blocksuite/index.ts (L538-L540)
In version 0.10, page id in spaces no longer has prefix "space:"
The data flow for fetching a doc's updates is:
- page id in `meta.pages` -> find `${page-id}` in `doc.spaces` -> `doc` -> `doc.guid`
if `doc` is not found in `doc.spaces`, a new doc will be created and its `doc.guid` is the same with its pageId
- because of guid logic change, the doc that previously prefixed with `space:` will not be found in `doc.spaces`
- when fetching the rows of this doc using the doc id === page id,
it will return EMPTY since there is no updates associated with the page id
The provided fix in the PR will patch the `spaces` field of the root doc so that after 0.10 the page doc can still be found in the `spaces` map. It shall apply to both of the idb & sqlite datasources.
Special thanks to @lawvs 's db file for investigation!
This commit is contained in:
@@ -2,7 +2,7 @@ import type { Page, PageMeta, Workspace } from '@blocksuite/store';
|
||||
import { createIndexeddbStorage } from '@blocksuite/store';
|
||||
import type { createStore, WritableAtom } from 'jotai/vanilla';
|
||||
import type { Doc } from 'yjs';
|
||||
import { Array as YArray, Doc as YDoc, Map as YMap } from 'yjs';
|
||||
import { Array as YArray, Doc as YDoc, Map as YMap, transact } from 'yjs';
|
||||
|
||||
export async function initEmptyPage(page: Page, title?: string) {
|
||||
await page.waitForLoaded();
|
||||
@@ -537,6 +537,7 @@ function migrateBlocks(
|
||||
const originalBlocks = oldDoc.getMap(spaceId) as YMap<unknown>;
|
||||
const subdoc = new YDoc();
|
||||
spaces.set(newId, subdoc);
|
||||
subdoc.guid = id;
|
||||
const blocks = subdoc.getMap('blocks');
|
||||
Array.from(originalBlocks.entries()).forEach(([key, value]) => {
|
||||
const blockData = value.clone();
|
||||
@@ -602,6 +603,7 @@ export async function forceUpgradePages(
|
||||
const versions = meta.get('blockVersions') as YMap<number>;
|
||||
const schema = options.getSchema();
|
||||
const oldVersions = versions.toJSON();
|
||||
guidCompatibilityFix(rootDoc);
|
||||
spaces.forEach((space: Doc) => {
|
||||
try {
|
||||
schema.upgradePage(0, oldVersions, space);
|
||||
@@ -623,6 +625,7 @@ async function upgradeV2ToV3(options: UpgradeOptions): Promise<boolean> {
|
||||
const meta = rootDoc.getMap('meta') as YMap<unknown>;
|
||||
const versions = meta.get('blockVersions') as YMap<number>;
|
||||
const schema = options.getSchema();
|
||||
guidCompatibilityFix(rootDoc);
|
||||
spaces.forEach((space: Doc) => {
|
||||
schema.upgradePage(
|
||||
0,
|
||||
@@ -654,6 +657,39 @@ async function upgradeV2ToV3(options: UpgradeOptions): Promise<boolean> {
|
||||
return true;
|
||||
}
|
||||
|
||||
// patch root doc's space guid compatibility issue
|
||||
//
|
||||
// in version 0.10, page id in spaces no longer has prefix "space:"
|
||||
// The data flow for fetching a doc's updates is:
|
||||
// - page id in `meta.pages` -> find `${page-id}` in `doc.spaces` -> `doc` -> `doc.guid`
|
||||
// if `doc` is not found in `doc.spaces`, a new doc will be created and its `doc.guid` is the same with its pageId
|
||||
// - because of guid logic change, the doc that previously prefixed with "space:" will not be found in `doc.spaces`
|
||||
// - when fetching the rows of this doc using the doc id === page id,
|
||||
// it will return empty since there is no updates associated with the page id
|
||||
export function guidCompatibilityFix(rootDoc: YDoc) {
|
||||
let changed = false;
|
||||
transact(rootDoc, () => {
|
||||
const spaces = rootDoc.getMap('spaces') as YMap<YDoc>;
|
||||
spaces.forEach((doc: YDoc, pageId: string) => {
|
||||
if (pageId.includes(':')) {
|
||||
const newPageId = pageId.split(':').at(-1) ?? pageId;
|
||||
const newDoc = new YDoc();
|
||||
// clone the original doc. yjs is not happy to use the same doc instance
|
||||
applyUpdate(newDoc, encodeStateAsUpdate(doc));
|
||||
newDoc.guid = doc.guid;
|
||||
spaces.set(newPageId, newDoc);
|
||||
// should remove the old doc, otherwise we will do it again in the next run
|
||||
spaces.delete(pageId);
|
||||
changed = true;
|
||||
console.debug(
|
||||
`fixed space id ${pageId} -> ${newPageId}, doc id: ${doc.guid}`
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
return changed;
|
||||
}
|
||||
|
||||
export enum WorkspaceVersion {
|
||||
// v1 is treated as undefined
|
||||
SubDoc = 2,
|
||||
|
||||
@@ -98,38 +98,21 @@ export const createLazyProvider = (
|
||||
async function syncDoc(doc: Doc) {
|
||||
const guid = doc.guid;
|
||||
{
|
||||
// backport from `@blocksuite/store`
|
||||
const prefixId = guid.startsWith('space:') ? guid.slice(6) : guid;
|
||||
const possible1 = `${rootDoc.guid}:space:${prefixId}`;
|
||||
const possible2 = `space:${prefixId}`;
|
||||
const update1 = await datasource.queryDocState(possible1);
|
||||
const update2 = await datasource.queryDocState(possible2);
|
||||
const update = await datasource.queryDocState(guid);
|
||||
let hasUpdate = false;
|
||||
if (
|
||||
update1 &&
|
||||
update1.missing.length !== 2 &&
|
||||
update1.missing[0] !== 0 &&
|
||||
update1.missing[1] !== 0
|
||||
update &&
|
||||
update.missing.length !== 2 &&
|
||||
update.missing[0] !== 0 &&
|
||||
update.missing[1] !== 0
|
||||
) {
|
||||
applyUpdate(doc, update1.missing, origin);
|
||||
hasUpdate = true;
|
||||
}
|
||||
if (
|
||||
update2 &&
|
||||
update2.missing.length !== 2 &&
|
||||
update2.missing[0] !== 0 &&
|
||||
update2.missing[1] !== 0
|
||||
) {
|
||||
applyUpdate(doc, update2.missing, origin);
|
||||
applyUpdate(doc, update.missing, origin);
|
||||
hasUpdate = true;
|
||||
}
|
||||
if (hasUpdate) {
|
||||
await datasource.sendDocUpdate(
|
||||
guid,
|
||||
encodeStateAsUpdate(
|
||||
doc,
|
||||
update1 ? update1.state : update2 ? update2.state : undefined
|
||||
)
|
||||
encodeStateAsUpdate(doc, update ? update.state : undefined)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user