mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
chore: bump up @blocksuite/affine version to v0.18.0 (#8899)
This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [@blocksuite/affine](https://redirect.github.com/toeverything/blocksuite) ([source](https://redirect.github.com/toeverything/blocksuite/tree/HEAD/packages/affine/all), [changelog](https://redirect.github.com/toeverything/blocksuite/blob/master/packages/blocks/CHANGELOG.md)) | [`0.17.33` -> `0.18.0`](https://renovatebot.com/diffs/npm/@blocksuite%2faffine/0.17.33/0.18.0) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | [](https://docs.renovatebot.com/merge-confidence/) | --- ### Release Notes <details> <summary>toeverything/blocksuite (@​blocksuite/affine)</summary> ### [`v0.18.0`](https://redirect.github.com/toeverything/blocksuite/blob/HEAD/packages/affine/all/CHANGELOG.md#0180) [Compare Source](https://redirect.github.com/toeverything/blocksuite/compare/v0.17.33...v0.18.0) ##### Minor Changes - [`9daa1f3`](https://redirect.github.com/toeverything/blocksuite/commit/9daa1f3): ## Feat - feat(blocks): markdown adapter reference serialization and deserialization ([#​8782](https://redirect.github.com/toeverything/blocksuite/issues/8782)) - feat(blocks): add lazy loading to image and iframe ([#​8773](https://redirect.github.com/toeverything/blocksuite/issues/8773)) </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about these updates again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/toeverything/AFFiNE). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4xOS4wIiwidXBkYXRlZEluVmVyIjoiMzkuMTkuMCIsInRhcmdldEJyYW5jaCI6ImNhbmFyeSIsImxhYmVscyI6WyJkZXBlbmRlbmNpZXMiXX0=-->
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
"@affine/debug": "workspace:*",
|
||||
"@affine/env": "workspace:*",
|
||||
"@affine/templates": "workspace:*",
|
||||
"@blocksuite/affine": "0.17.33",
|
||||
"@blocksuite/affine": "0.18.0",
|
||||
"@datastructures-js/binary-search-tree": "^5.3.2",
|
||||
"eventemitter2": "^6.4.9",
|
||||
"foxact": "^0.2.33",
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { dirname, resolve } from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
|
||||
import { describe, expect, test } from 'vitest';
|
||||
import type { Array as YArray, Map as YMap } from 'yjs';
|
||||
import { applyUpdate, Doc } from 'yjs';
|
||||
|
||||
import { migrateToSubdoc } from '../blocksuite/index.js';
|
||||
|
||||
const fixturePath = resolve(
|
||||
dirname(fileURLToPath(import.meta.url)),
|
||||
'workspace.ydoc'
|
||||
);
|
||||
const yDocBuffer = readFileSync(fixturePath);
|
||||
const doc = new Doc();
|
||||
applyUpdate(doc, new Uint8Array(yDocBuffer));
|
||||
const migratedDoc = migrateToSubdoc(doc);
|
||||
|
||||
describe('migration', () => {
|
||||
test('migration to subdoc', async () => {
|
||||
const { default: json } = await import('@affine-test/fixtures/output.json');
|
||||
const length = Object.keys(json).length;
|
||||
const binary = new Uint8Array(length);
|
||||
for (let i = 0; i < length; i++) {
|
||||
binary[i] = (json as any)[i];
|
||||
}
|
||||
const doc = new Doc();
|
||||
applyUpdate(doc, binary);
|
||||
{
|
||||
// invoke data
|
||||
doc.getMap('space:hello-world');
|
||||
doc.getMap('space:meta');
|
||||
}
|
||||
const blocks = doc.getMap('space:hello-world').toJSON();
|
||||
const newDoc = migrateToSubdoc(doc);
|
||||
const subDoc = newDoc.getMap('spaces').values().next().value as Doc;
|
||||
const data = (subDoc.toJSON() as any).blocks;
|
||||
Object.keys(data).forEach(id => {
|
||||
if (id === 'xyWNqindHH') {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
blocks[id]['sys:flavour'] === 'affine:surface' &&
|
||||
!blocks[id]['prop:elements']
|
||||
) {
|
||||
blocks[id]['prop:elements'] = data[id]['prop:elements'];
|
||||
}
|
||||
expect(data[id]).toEqual(blocks[id]);
|
||||
});
|
||||
});
|
||||
|
||||
test('test fixture should be set correctly', () => {
|
||||
const meta = doc.getMap('space:meta');
|
||||
const versions = meta.get('versions') as YMap<unknown>;
|
||||
expect(versions.get('affine:code')).toBeTypeOf('number');
|
||||
});
|
||||
|
||||
test('metadata should be migrated correctly', () => {
|
||||
const originalMeta = doc.getMap('space:meta');
|
||||
const originalVersions = originalMeta.get('versions') as YMap<unknown>;
|
||||
|
||||
const meta = migratedDoc.getMap('meta');
|
||||
const blockVersions = meta.get('blockVersions') as YMap<unknown>;
|
||||
|
||||
expect(meta.get('workspaceVersion')).toBe(1);
|
||||
expect(blockVersions.get('affine:code')).toBe(
|
||||
originalVersions.get('affine:code')
|
||||
);
|
||||
expect((meta.get('pages') as YArray<unknown>).length).toBe(
|
||||
(originalMeta.get('pages') as YArray<unknown>).length
|
||||
);
|
||||
|
||||
expect(blockVersions.get('affine:embed')).toBeUndefined();
|
||||
expect(blockVersions.get('affine:image')).toBe(
|
||||
originalVersions.get('affine:embed')
|
||||
);
|
||||
|
||||
expect(blockVersions.get('affine:frame')).toBeUndefined();
|
||||
expect(blockVersions.get('affine:note')).toBe(
|
||||
originalVersions.get('affine:frame')
|
||||
);
|
||||
});
|
||||
});
|
||||
Binary file not shown.
@@ -1,19 +1 @@
|
||||
export * from './blocks';
|
||||
export {
|
||||
migratePages as forceUpgradePages,
|
||||
migrateGuidCompatibility,
|
||||
} from './migration/blocksuite'; // campatible with electron
|
||||
export * from './migration/fixing';
|
||||
export { migrateToSubdoc, upgradeV1ToV2 } from './migration/subdoc';
|
||||
export * from './migration/workspace';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Use workspace meta data to determine the workspace version.
|
||||
*/
|
||||
export enum WorkspaceVersion {
|
||||
// v1 is treated as undefined
|
||||
SubDoc = 2,
|
||||
DatabaseV3 = 3,
|
||||
Surface = 4,
|
||||
}
|
||||
|
||||
@@ -1,100 +0,0 @@
|
||||
import type { Schema } from '@blocksuite/affine/store';
|
||||
import type { Array as YArray } from 'yjs';
|
||||
import {
|
||||
applyUpdate,
|
||||
Doc as YDoc,
|
||||
encodeStateAsUpdate,
|
||||
Map as YMap,
|
||||
transact,
|
||||
} from 'yjs';
|
||||
|
||||
export const getLatestVersions = (schema: Schema): Record<string, number> => {
|
||||
return [...schema.flavourSchemaMap.entries()].reduce(
|
||||
(record, [flavour, schema]) => {
|
||||
record[flavour] = schema.version;
|
||||
return record;
|
||||
},
|
||||
{} as Record<string, number>
|
||||
);
|
||||
};
|
||||
|
||||
export async function migratePages(
|
||||
rootDoc: YDoc,
|
||||
schema: Schema
|
||||
): Promise<boolean> {
|
||||
const spaces = rootDoc.getMap('spaces') as YMap<any>;
|
||||
const meta = rootDoc.getMap('meta') as YMap<unknown>;
|
||||
const versions = meta.get('blockVersions') as YMap<number>;
|
||||
const oldVersions = versions?.toJSON() ?? {};
|
||||
|
||||
spaces.forEach((space: YDoc) => {
|
||||
try {
|
||||
// Catch page upgrade error to avoid blocking the whole workspace migration.
|
||||
schema.upgradeDoc(0, oldVersions, space);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
}
|
||||
});
|
||||
schema.upgradeCollection(rootDoc);
|
||||
|
||||
// Hard code to upgrade page version to 2.
|
||||
// Let e2e to ensure the data version is correct.
|
||||
return transact(
|
||||
rootDoc,
|
||||
() => {
|
||||
const pageVersion = meta.get('pageVersion');
|
||||
if (typeof pageVersion !== 'number' || pageVersion < 2) {
|
||||
meta.set('pageVersion', 2);
|
||||
}
|
||||
|
||||
const newVersions = getLatestVersions(schema);
|
||||
meta.set('blockVersions', new YMap(Object.entries(newVersions)));
|
||||
return Object.entries(oldVersions).some(
|
||||
([flavour, version]) => newVersions[flavour] !== version
|
||||
);
|
||||
},
|
||||
'migratePages',
|
||||
/**
|
||||
* transact as remote update, because blocksuite will skip local changes.
|
||||
* https://github.com/toeverything/blocksuite/blob/9c2df3f7aa5617c050e0dccdd73e99bb67e0c0f7/packages/store/src/reactive/utils.ts#L143
|
||||
*/
|
||||
false
|
||||
);
|
||||
}
|
||||
|
||||
// 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 migrateGuidCompatibility(rootDoc: YDoc) {
|
||||
const meta = rootDoc.getMap('meta') as YMap<unknown>;
|
||||
const pages = meta.get('pages') as YArray<YMap<unknown>>;
|
||||
pages?.forEach(page => {
|
||||
const pageId = page.get('id') as string | undefined;
|
||||
if (pageId?.includes(':')) {
|
||||
// remove the prefix "space:" from page id
|
||||
page.set('id', pageId.split(':').at(-1));
|
||||
}
|
||||
});
|
||||
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);
|
||||
console.debug(
|
||||
`fixed space id ${pageId} -> ${newPageId}, doc id: ${doc.guid}`
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
import type { Doc as YDoc, Map as YMap } from 'yjs';
|
||||
import { transact } from 'yjs';
|
||||
|
||||
/**
|
||||
* Hard code to fix workspace version to be compatible with legacy data.
|
||||
* Let e2e to ensure the data version is correct.
|
||||
*/
|
||||
export function fixWorkspaceVersion(rootDoc: YDoc) {
|
||||
const meta = rootDoc.getMap('meta') as YMap<unknown>;
|
||||
|
||||
/**
|
||||
* It doesn't matter to upgrade workspace version from 1 or undefined to 2.
|
||||
* Blocksuite just set the value, do nothing else.
|
||||
*/
|
||||
function doFix() {
|
||||
if (meta.size === 0) {
|
||||
return;
|
||||
}
|
||||
const workspaceVersion = meta.get('workspaceVersion');
|
||||
if (typeof workspaceVersion !== 'number' || workspaceVersion < 2) {
|
||||
transact(
|
||||
rootDoc,
|
||||
() => {
|
||||
meta.set('workspaceVersion', 2);
|
||||
},
|
||||
'fixWorkspaceVersion',
|
||||
// transact as remote update, because blocksuite will skip local changes.
|
||||
false
|
||||
);
|
||||
}
|
||||
const pageVersion = meta.get('pageVersion');
|
||||
if (typeof pageVersion !== 'number' || pageVersion < 2) {
|
||||
transact(
|
||||
rootDoc,
|
||||
() => {
|
||||
meta.set('pageVersion', 2);
|
||||
},
|
||||
'fixPageVersion',
|
||||
// transact as remote update, because blocksuite will skip local changes.
|
||||
false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
doFix();
|
||||
|
||||
// do fix every time when meta changed
|
||||
meta.observe(() => doFix());
|
||||
}
|
||||
@@ -1,284 +0,0 @@
|
||||
import { nanoid } from 'nanoid';
|
||||
import {
|
||||
applyUpdate,
|
||||
Array as YArray,
|
||||
Doc as YDoc,
|
||||
encodeStateAsUpdate,
|
||||
Map as YMap,
|
||||
} from 'yjs';
|
||||
|
||||
const migrationOrigin = 'affine-migration';
|
||||
|
||||
type XYWH = [number, number, number, number];
|
||||
|
||||
function deserializeXYWH(xywh: string): XYWH {
|
||||
return JSON.parse(xywh) as XYWH;
|
||||
}
|
||||
|
||||
function migrateDatabase(data: YMap<unknown>) {
|
||||
data.delete('prop:mode');
|
||||
data.set('prop:views', new YArray());
|
||||
const columns = (data.get('prop:columns') as YArray<unknown>).toJSON() as {
|
||||
id: string;
|
||||
name: string;
|
||||
hide: boolean;
|
||||
type: string;
|
||||
width: number;
|
||||
selection?: unknown[];
|
||||
}[];
|
||||
const views = [
|
||||
{
|
||||
id: 'default',
|
||||
name: 'Table',
|
||||
columns: columns.map(col => ({
|
||||
id: col.id,
|
||||
width: col.width,
|
||||
hide: col.hide,
|
||||
})),
|
||||
filter: { type: 'group', op: 'and', conditions: [] },
|
||||
mode: 'table',
|
||||
},
|
||||
];
|
||||
const cells = (data.get('prop:cells') as YMap<unknown>).toJSON() as Record<
|
||||
string,
|
||||
Record<
|
||||
string,
|
||||
{
|
||||
id: string;
|
||||
value: unknown;
|
||||
}
|
||||
>
|
||||
>;
|
||||
const convertColumn = (
|
||||
id: string,
|
||||
update: (cell: { id: string; value: unknown }) => void
|
||||
) => {
|
||||
Object.values(cells).forEach(row => {
|
||||
if (row[id] !== null && row[id] !== undefined) {
|
||||
update(row[id]);
|
||||
}
|
||||
});
|
||||
};
|
||||
const newColumns = columns.map(v => {
|
||||
let data: Record<string, unknown> = {};
|
||||
if (v.type === 'select' || v.type === 'multi-select') {
|
||||
data = { options: v.selection };
|
||||
if (v.type === 'select') {
|
||||
convertColumn(v.id, cell => {
|
||||
if (Array.isArray(cell.value)) {
|
||||
cell.value = cell.value[0]?.id;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
convertColumn(v.id, cell => {
|
||||
if (Array.isArray(cell.value)) {
|
||||
cell.value = cell.value.map(v => v.id);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
if (v.type === 'number') {
|
||||
convertColumn(v.id, cell => {
|
||||
if (typeof cell.value === 'string') {
|
||||
cell.value = Number.parseFloat(cell.value.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
return {
|
||||
id: v.id,
|
||||
type: v.type,
|
||||
name: v.name,
|
||||
data,
|
||||
};
|
||||
});
|
||||
data.set('prop:columns', newColumns);
|
||||
data.set('prop:views', views);
|
||||
data.set('prop:cells', cells);
|
||||
}
|
||||
|
||||
function runBlockMigration(
|
||||
flavour: string,
|
||||
data: YMap<unknown>,
|
||||
version: number
|
||||
) {
|
||||
if (flavour === 'affine:frame') {
|
||||
data.set('sys:flavour', 'affine:note');
|
||||
return;
|
||||
}
|
||||
if (flavour === 'affine:surface' && version <= 3) {
|
||||
if (data.has('elements')) {
|
||||
const elements = data.get('elements') as YMap<unknown>;
|
||||
migrateSurface(elements);
|
||||
data.set('prop:elements', elements.clone());
|
||||
data.delete('elements');
|
||||
} else {
|
||||
data.set('prop:elements', new YMap());
|
||||
}
|
||||
}
|
||||
if (flavour === 'affine:embed') {
|
||||
data.set('sys:flavour', 'affine:image');
|
||||
data.delete('prop:type');
|
||||
}
|
||||
if (flavour === 'affine:database' && version < 2) {
|
||||
migrateDatabase(data);
|
||||
}
|
||||
}
|
||||
|
||||
function migrateSurface(data: YMap<unknown>) {
|
||||
for (const [, value] of <IterableIterator<[string, YMap<unknown>]>>(
|
||||
data.entries()
|
||||
)) {
|
||||
if (value.get('type') === 'connector') {
|
||||
migrateSurfaceConnector(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function migrateSurfaceConnector(data: YMap<any>) {
|
||||
let id = data.get('startElement')?.id;
|
||||
const controllers = data.get('controllers');
|
||||
const length = controllers.length;
|
||||
const xywh = deserializeXYWH(data.get('xywh'));
|
||||
if (id) {
|
||||
data.set('source', { id });
|
||||
} else {
|
||||
data.set('source', {
|
||||
position: [controllers[0].x + xywh[0], controllers[0].y + xywh[1]],
|
||||
});
|
||||
}
|
||||
|
||||
id = data.get('endElement')?.id;
|
||||
if (id) {
|
||||
data.set('target', { id });
|
||||
} else {
|
||||
data.set('target', {
|
||||
position: [
|
||||
controllers[length - 1].x + xywh[0],
|
||||
controllers[length - 1].y + xywh[1],
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const width = data.get('lineWidth') ?? 4;
|
||||
data.set('strokeWidth', width);
|
||||
const color = data.get('color');
|
||||
data.set('stroke', color);
|
||||
|
||||
data.delete('startElement');
|
||||
data.delete('endElement');
|
||||
data.delete('controllers');
|
||||
data.delete('lineWidth');
|
||||
data.delete('color');
|
||||
data.delete('xywh');
|
||||
}
|
||||
|
||||
function updateBlockVersions(versions: YMap<number>) {
|
||||
const frameVersion = versions.get('affine:frame');
|
||||
if (frameVersion !== undefined) {
|
||||
versions.set('affine:note', frameVersion);
|
||||
versions.delete('affine:frame');
|
||||
}
|
||||
const embedVersion = versions.get('affine:embed');
|
||||
if (embedVersion !== undefined) {
|
||||
versions.set('affine:image', embedVersion);
|
||||
versions.delete('affine:embed');
|
||||
}
|
||||
const databaseVersion = versions.get('affine:database');
|
||||
if (databaseVersion !== undefined && databaseVersion < 2) {
|
||||
versions.set('affine:database', 2);
|
||||
}
|
||||
}
|
||||
|
||||
function migrateMeta(
|
||||
oldDoc: YDoc,
|
||||
newDoc: YDoc,
|
||||
idMap: Record<string, string>
|
||||
) {
|
||||
const originalMeta = oldDoc.getMap('space:meta');
|
||||
const originalVersions = originalMeta.get('versions') as YMap<number>;
|
||||
const originalPages = originalMeta.get('pages') as YArray<YMap<string>>;
|
||||
const meta = newDoc.getMap('meta');
|
||||
const pages = new YArray();
|
||||
const blockVersions = originalVersions.clone();
|
||||
|
||||
meta.set('workspaceVersion', 1);
|
||||
meta.set('blockVersions', blockVersions);
|
||||
meta.set('pages', pages);
|
||||
meta.set('name', originalMeta.get('name') as string);
|
||||
|
||||
updateBlockVersions(blockVersions);
|
||||
const mapList = originalPages.map(page => {
|
||||
const map = new YMap();
|
||||
Array.from(page.entries())
|
||||
.filter(([key]) => key !== 'subpageIds')
|
||||
.forEach(([key, value]) => {
|
||||
if (key === 'id') {
|
||||
idMap[value] = nanoid();
|
||||
map.set(key, idMap[value]);
|
||||
} else {
|
||||
map.set(key, value);
|
||||
}
|
||||
});
|
||||
return map;
|
||||
});
|
||||
pages.push(mapList);
|
||||
}
|
||||
|
||||
function migrateBlocks(
|
||||
oldDoc: YDoc,
|
||||
newDoc: YDoc,
|
||||
idMap: Record<string, string>
|
||||
) {
|
||||
const spaces = newDoc.getMap('spaces');
|
||||
const originalMeta = oldDoc.getMap('space:meta');
|
||||
const originalVersions = originalMeta.get('versions') as YMap<number>;
|
||||
const originalPages = originalMeta.get('pages') as YArray<YMap<unknown>>;
|
||||
originalPages.forEach(page => {
|
||||
const id = page.get('id') as string;
|
||||
const newId = idMap[id];
|
||||
const spaceId = id.startsWith('space:') ? id : `space:${id}`;
|
||||
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]) => {
|
||||
// @ts-expect-error clone method exists
|
||||
const blockData = value.clone();
|
||||
blocks.set(key, blockData);
|
||||
const flavour = blockData.get('sys:flavour') as string;
|
||||
const version = originalVersions.get(flavour);
|
||||
if (version !== undefined) {
|
||||
runBlockMigration(flavour, blockData, version);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export function migrateToSubdoc(oldDoc: YDoc): YDoc {
|
||||
const needMigration =
|
||||
Array.from(oldDoc.getMap('space:meta').keys()).length > 0;
|
||||
if (!needMigration) {
|
||||
return oldDoc;
|
||||
}
|
||||
const newDoc = new YDoc();
|
||||
const idMap = {} as Record<string, string>;
|
||||
migrateMeta(oldDoc, newDoc, idMap);
|
||||
migrateBlocks(oldDoc, newDoc, idMap);
|
||||
return newDoc;
|
||||
}
|
||||
|
||||
/**
|
||||
* upgrade oldDoc to v2, write to targetDoc
|
||||
*/
|
||||
export const upgradeV1ToV2 = async (oldDoc: YDoc, targetDoc: YDoc) => {
|
||||
const newDoc = migrateToSubdoc(oldDoc);
|
||||
applyUpdate(targetDoc, encodeStateAsUpdate(newDoc), migrationOrigin);
|
||||
newDoc.getSubdocs().forEach(subdoc => {
|
||||
targetDoc.getSubdocs().forEach(newDoc => {
|
||||
if (subdoc.guid === newDoc.guid) {
|
||||
applyUpdate(newDoc, encodeStateAsUpdate(subdoc), migrationOrigin);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -1,76 +0,0 @@
|
||||
import type { DocCollection } from '@blocksuite/affine/store';
|
||||
import type { Array as YArray, Doc as YDoc, Map as YMap } from 'yjs';
|
||||
|
||||
/**
|
||||
* For split migrate function from MigrationQueue.
|
||||
*/
|
||||
export enum MigrationPoint {
|
||||
SubDoc = 1,
|
||||
GuidFix = 2,
|
||||
BlockVersion = 3,
|
||||
}
|
||||
|
||||
export function checkWorkspaceCompatibility(
|
||||
docCollection: DocCollection,
|
||||
isCloud: boolean
|
||||
): MigrationPoint | null {
|
||||
// check if there is any key starts with 'space:' on root doc
|
||||
const spaceMetaObj = docCollection.doc.share.get('space:meta') as
|
||||
| YMap<any>
|
||||
| undefined;
|
||||
const docKeys = Array.from(docCollection.doc.share.keys());
|
||||
const haveSpaceMeta = !!spaceMetaObj && spaceMetaObj.size > 0;
|
||||
const haveLegacySpace = docKeys.some(key => key.startsWith('space:'));
|
||||
|
||||
// DON'T UPGRADE SUBDOC ON CLOUD
|
||||
if (!isCloud && (haveSpaceMeta || haveLegacySpace)) {
|
||||
return MigrationPoint.SubDoc;
|
||||
}
|
||||
|
||||
// exit if no pages
|
||||
if (!docCollection.meta.docs?.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// check guid compatibility
|
||||
const meta = docCollection.doc.getMap('meta') as YMap<unknown>;
|
||||
const pages = meta.get('pages') as YArray<YMap<unknown>>;
|
||||
for (const page of pages) {
|
||||
const pageId = page.get('id') as string | undefined;
|
||||
if (pageId?.includes(':')) {
|
||||
return MigrationPoint.GuidFix;
|
||||
}
|
||||
}
|
||||
const spaces = docCollection.doc.getMap('spaces') as YMap<YDoc>;
|
||||
for (const [pageId, _] of spaces) {
|
||||
if (pageId.includes(':')) {
|
||||
return MigrationPoint.GuidFix;
|
||||
}
|
||||
}
|
||||
|
||||
const hasVersion = docCollection.meta.hasVersion;
|
||||
if (!hasVersion) {
|
||||
return MigrationPoint.BlockVersion;
|
||||
}
|
||||
|
||||
// Temporarily follow the check logic of blocksuite.
|
||||
if ((docCollection.meta.docs?.length ?? 0) <= 1) {
|
||||
try {
|
||||
docCollection.meta.validateVersion(docCollection);
|
||||
} catch (e) {
|
||||
console.info('validateVersion error', e);
|
||||
return MigrationPoint.BlockVersion;
|
||||
}
|
||||
}
|
||||
|
||||
// From v2, we depend on blocksuite to check and migrate data.
|
||||
const blockVersions = docCollection.meta.blockVersions;
|
||||
for (const [flavour, version] of Object.entries(blockVersions ?? {})) {
|
||||
const schema = docCollection.schema.flavourSchemaMap.get(flavour);
|
||||
if (schema?.version !== version) {
|
||||
return MigrationPoint.BlockVersion;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
import { Unreachable } from '@affine/env/constant';
|
||||
import { WorkspaceFlavour } from '@affine/env/workspace';
|
||||
import { applyUpdate, Doc as YDoc, encodeStateAsUpdate } from 'yjs';
|
||||
|
||||
import {
|
||||
checkWorkspaceCompatibility,
|
||||
forceUpgradePages,
|
||||
migrateGuidCompatibility,
|
||||
MigrationPoint,
|
||||
upgradeV1ToV2,
|
||||
} from '../../../blocksuite';
|
||||
import { Entity } from '../../../framework';
|
||||
import { LiveData } from '../../../livedata';
|
||||
import type { WorkspaceMetadata } from '../metadata';
|
||||
import type { WorkspaceDestroyService } from '../services/destroy';
|
||||
import type { WorkspaceFactoryService } from '../services/factory';
|
||||
import type { WorkspaceService } from '../services/workspace';
|
||||
|
||||
export class WorkspaceUpgrade extends Entity {
|
||||
needUpgrade$ = new LiveData(false);
|
||||
upgrading$ = new LiveData(false);
|
||||
|
||||
constructor(
|
||||
private readonly workspaceService: WorkspaceService,
|
||||
private readonly workspaceFactory: WorkspaceFactoryService,
|
||||
private readonly workspaceDestroy: WorkspaceDestroyService
|
||||
) {
|
||||
super();
|
||||
this.checkIfNeedUpgrade();
|
||||
workspaceService.workspace.docCollection.doc.on('update', () => {
|
||||
this.checkIfNeedUpgrade();
|
||||
});
|
||||
}
|
||||
|
||||
checkIfNeedUpgrade() {
|
||||
const needUpgrade = !!checkWorkspaceCompatibility(
|
||||
this.workspaceService.workspace.docCollection,
|
||||
this.workspaceService.workspace.flavour === WorkspaceFlavour.AFFINE_CLOUD
|
||||
);
|
||||
this.needUpgrade$.next(needUpgrade);
|
||||
return needUpgrade;
|
||||
}
|
||||
|
||||
async upgrade(): Promise<WorkspaceMetadata | null> {
|
||||
if (this.upgrading$.value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
this.upgrading$.next(true);
|
||||
|
||||
try {
|
||||
await this.workspaceService.workspace.engine.waitForDocSynced();
|
||||
|
||||
const step = checkWorkspaceCompatibility(
|
||||
this.workspaceService.workspace.docCollection,
|
||||
this.workspaceService.workspace.flavour ===
|
||||
WorkspaceFlavour.AFFINE_CLOUD
|
||||
);
|
||||
|
||||
if (!step) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Clone a new doc to prevent change events.
|
||||
const clonedDoc = new YDoc({
|
||||
guid: this.workspaceService.workspace.docCollection.doc.guid,
|
||||
});
|
||||
applyDoc(clonedDoc, this.workspaceService.workspace.docCollection.doc);
|
||||
|
||||
if (step === MigrationPoint.SubDoc) {
|
||||
const newWorkspace = await this.workspaceFactory.create(
|
||||
WorkspaceFlavour.LOCAL,
|
||||
async (workspace, blobStorage) => {
|
||||
await upgradeV1ToV2(clonedDoc, workspace.doc);
|
||||
migrateGuidCompatibility(clonedDoc);
|
||||
await forceUpgradePages(
|
||||
workspace.doc,
|
||||
this.workspaceService.workspace.docCollection.schema
|
||||
);
|
||||
const blobList =
|
||||
await this.workspaceService.workspace.docCollection.blobSync.list();
|
||||
|
||||
for (const blobKey of blobList) {
|
||||
const blob =
|
||||
await this.workspaceService.workspace.docCollection.blobSync.get(
|
||||
blobKey
|
||||
);
|
||||
if (blob) {
|
||||
await blobStorage.set(blobKey, blob);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
await this.workspaceDestroy.deleteWorkspace(
|
||||
this.workspaceService.workspace.meta
|
||||
);
|
||||
return newWorkspace;
|
||||
} else if (step === MigrationPoint.GuidFix) {
|
||||
migrateGuidCompatibility(clonedDoc);
|
||||
await forceUpgradePages(
|
||||
clonedDoc,
|
||||
this.workspaceService.workspace.docCollection.schema
|
||||
);
|
||||
applyDoc(this.workspaceService.workspace.docCollection.doc, clonedDoc);
|
||||
await this.workspaceService.workspace.engine.waitForDocSynced();
|
||||
return null;
|
||||
} else if (step === MigrationPoint.BlockVersion) {
|
||||
await forceUpgradePages(
|
||||
clonedDoc,
|
||||
this.workspaceService.workspace.docCollection.schema
|
||||
);
|
||||
applyDoc(this.workspaceService.workspace.docCollection.doc, clonedDoc);
|
||||
await this.workspaceService.workspace.engine.waitForDocSynced();
|
||||
return null;
|
||||
} else {
|
||||
throw new Unreachable();
|
||||
}
|
||||
} finally {
|
||||
this.upgrading$.next(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function applyDoc(target: YDoc, result: YDoc) {
|
||||
applyUpdate(target, encodeStateAsUpdate(result));
|
||||
for (const targetSubDoc of target.subdocs.values()) {
|
||||
const resultSubDocs = Array.from(result.subdocs.values());
|
||||
const resultSubDoc = resultSubDocs.find(
|
||||
item => item.guid === targetSubDoc.guid
|
||||
);
|
||||
if (resultSubDoc) {
|
||||
applyDoc(targetSubDoc, resultSubDoc);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@ import { WorkspaceDBService } from '../../db';
|
||||
import { getAFFiNEWorkspaceSchema } from '../global-schema';
|
||||
import type { WorkspaceScope } from '../scopes/workspace';
|
||||
import { WorkspaceEngineService } from '../services/engine';
|
||||
import { WorkspaceUpgradeService } from '../services/upgrade';
|
||||
|
||||
export class Workspace extends Entity {
|
||||
constructor(public readonly scope: WorkspaceScope) {
|
||||
@@ -64,10 +63,6 @@ export class Workspace extends Entity {
|
||||
return this.framework.get(WorkspaceEngineService).engine;
|
||||
}
|
||||
|
||||
get upgrade() {
|
||||
return this.framework.get(WorkspaceUpgradeService).upgrade;
|
||||
}
|
||||
|
||||
name$ = LiveData.from<string | undefined>(
|
||||
new Observable(subscriber => {
|
||||
subscriber.next(this.docCollection.meta.name);
|
||||
|
||||
@@ -16,7 +16,6 @@ import { GlobalCache, GlobalState } from '../storage';
|
||||
import { WorkspaceEngine } from './entities/engine';
|
||||
import { WorkspaceList } from './entities/list';
|
||||
import { WorkspaceProfile } from './entities/profile';
|
||||
import { WorkspaceUpgrade } from './entities/upgrade';
|
||||
import { Workspace } from './entities/workspace';
|
||||
import {
|
||||
WorkspaceLocalCacheImpl,
|
||||
@@ -32,7 +31,6 @@ import { WorkspaceListService } from './services/list';
|
||||
import { WorkspaceProfileService } from './services/profile';
|
||||
import { WorkspaceRepositoryService } from './services/repo';
|
||||
import { WorkspaceTransformService } from './services/transform';
|
||||
import { WorkspaceUpgradeService } from './services/upgrade';
|
||||
import { WorkspaceService } from './services/workspace';
|
||||
import { WorkspacesService } from './services/workspaces';
|
||||
import { WorkspaceProfileCacheStore } from './stores/profile-cache';
|
||||
@@ -72,12 +70,6 @@ export function configureWorkspaceModule(framework: Framework) {
|
||||
.entity(Workspace, [WorkspaceScope])
|
||||
.service(WorkspaceEngineService, [WorkspaceScope])
|
||||
.entity(WorkspaceEngine, [WorkspaceService])
|
||||
.service(WorkspaceUpgradeService)
|
||||
.entity(WorkspaceUpgrade, [
|
||||
WorkspaceService,
|
||||
WorkspaceFactoryService,
|
||||
WorkspaceDestroyService,
|
||||
])
|
||||
.impl(WorkspaceLocalState, WorkspaceLocalStateImpl, [
|
||||
WorkspaceService,
|
||||
GlobalState,
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { DebugLogger } from '@affine/debug';
|
||||
|
||||
import { fixWorkspaceVersion } from '../../../blocksuite';
|
||||
import { Service } from '../../../framework';
|
||||
import { ObjectPool } from '../../../utils';
|
||||
import type { Workspace } from '../entities/workspace';
|
||||
@@ -105,9 +104,6 @@ export class WorkspaceRepositoryService extends Service {
|
||||
workspace.engine.setRootDoc(workspace.docCollection.doc);
|
||||
workspace.engine.start();
|
||||
|
||||
// apply compatibility fix
|
||||
fixWorkspaceVersion(workspace.docCollection.doc);
|
||||
|
||||
this.framework.emitEvent(WorkspaceInitialized, workspace);
|
||||
|
||||
this.profileRepo
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
import { Service } from '../../../framework';
|
||||
import { WorkspaceUpgrade } from '../entities/upgrade';
|
||||
|
||||
export class WorkspaceUpgradeService extends Service {
|
||||
upgrade = this.framework.createEntity(WorkspaceUpgrade);
|
||||
}
|
||||
Reference in New Issue
Block a user