From 72378abe568a825aebfde3b941fa409ee9bc3878 Mon Sep 17 00:00:00 2001 From: Alex Yang Date: Wed, 28 Jun 2023 16:46:08 +0800 Subject: [PATCH] feat: update migration test page (#2871) --- apps/web/src/pages/_debug/migration.tsx | 26 ++++++++++++++++++- .../y-indexeddb/src/__tests__/index.spec.ts | 14 ++++++++++ packages/y-indexeddb/src/utils.ts | 21 +++++++++++++++ 3 files changed, 60 insertions(+), 1 deletion(-) diff --git a/apps/web/src/pages/_debug/migration.tsx b/apps/web/src/pages/_debug/migration.tsx index 3f1efe12f9..672dffe225 100644 --- a/apps/web/src/pages/_debug/migration.tsx +++ b/apps/web/src/pages/_debug/migration.tsx @@ -4,14 +4,17 @@ import type { LocalWorkspace, } from '@affine/env/workspace'; import { WorkspaceFlavour } from '@affine/env/workspace'; +import type { RootWorkspaceMetadataV1 } from '@affine/workspace/atom'; +import { rootWorkspacesMetadataAtom } from '@affine/workspace/atom'; import { migrateLocalBlobStorage, upgradeV1ToV2, } from '@affine/workspace/migration'; import { createIndexedDBDownloadProvider } from '@affine/workspace/providers'; import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models'; -import { Workspace } from '@blocksuite/store'; +import { assertExists, Workspace } from '@blocksuite/store'; import { NoSsr } from '@mui/material'; +import { rootStore } from '@toeverything/plugin-infra/manager'; import { atom, createStore, @@ -20,6 +23,7 @@ import { useAtomValue, useSetAtom, } from 'jotai'; +import { useRouter } from 'next/router'; import type { ReactElement } from 'react'; import { Suspense, use, useCallback } from 'react'; @@ -114,6 +118,25 @@ const WorkspaceInner = () => { const MigrationInner = () => { const ids = useAtomValue(workspaceIdsAtom); const [id, setId] = useAtom(targetIdAtom); + const router = useRouter(); + const onWriteIntoProduction = useCallback(() => { + assertExists(id); + const metadata: RootWorkspaceMetadataV1 = { + id, + flavour: WorkspaceFlavour.LOCAL, + version: undefined, + }; + rootStore.set(rootWorkspacesMetadataAtom, [metadata]); + router.push('/').catch(console.error); + }, [id, router]); + const writeIntoProductionNode = id && ( + + ); return (
{ ))} + {writeIntoProductionNode} {id && }
); diff --git a/packages/y-indexeddb/src/__tests__/index.spec.ts b/packages/y-indexeddb/src/__tests__/index.spec.ts index f056529863..f77445c482 100644 --- a/packages/y-indexeddb/src/__tests__/index.spec.ts +++ b/packages/y-indexeddb/src/__tests__/index.spec.ts @@ -20,6 +20,7 @@ import { downloadBinary, getMilestones, markMilestone, + overwriteBinary, revertUpdate, setMergeCount, } from '../index'; @@ -488,4 +489,17 @@ describe('utils', () => { }, 0) ); }); + + test('overwrite binary', async () => { + await overwriteBinary('test', new Uint8Array([1, 2, 3])); + { + const binary = await downloadBinary('test'); + expect(binary).toEqual(new Uint8Array([1, 2, 3])); + } + await overwriteBinary('test', new Uint8Array([0, 0])); + { + const binary = await downloadBinary('test'); + expect(binary).toEqual(new Uint8Array([0, 0])); + } + }); }); diff --git a/packages/y-indexeddb/src/utils.ts b/packages/y-indexeddb/src/utils.ts index 516885adb2..9258975145 100644 --- a/packages/y-indexeddb/src/utils.ts +++ b/packages/y-indexeddb/src/utils.ts @@ -143,3 +143,24 @@ export async function downloadBinary( return mergeUpdates(doc.updates.map(({ update }) => update)); } } + +export async function overwriteBinary( + guid: string, + update: UpdateMessage['update'], + dbName = DEFAULT_DB_NAME +) { + const dbPromise = openDB(dbName, dbVersion, { + upgrade: upgradeDB, + }); + const db = await dbPromise; + const t = db.transaction('workspace', 'readwrite').objectStore('workspace'); + await t.put({ + id: guid, + updates: [ + { + timestamp: Date.now(), + update, + }, + ], + }); +}