fix(electron): remove all migration code in electron (#6969)

The migration code import blocksuite in the dependency tree and prevent affine from running because electron's helper process tries to run browser only code that is part of the side effect of `@blocksuite/blocks`.

![image.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/T2klNLEk0wxLh4NRDzhk/53da3972-7433-4631-b8c2-d3e322066c7d.png)

[The side effect free trick in esbuild config](https://github.com/toeverything/AFFiNE/pull/6415)  does not clean up these - not sure why.

It has been already 6 month since we introduced the migration code in DB.
Instead of finding out the real root cause, I think may be better to remove the migration code completely so that no blocksuite code will be in the import paths in helper.js.

![image.png](https://graphite-user-uploaded-assets-prod.s3.amazonaws.com/T2klNLEk0wxLh4NRDzhk/3b03522a-2a25-4bdb-8287-86a6b94623e1.png)
This commit is contained in:
pengx17
2024-05-17 03:01:14 +00:00
parent 10015c59b7
commit bd5023d4ab
7 changed files with 5 additions and 294 deletions

View File

@@ -1,86 +0,0 @@
import path from 'node:path';
import { SqliteConnection } from '@affine/native';
import { removeWithRetry } from '@affine-test/kit/utils/utils';
import {
afterAll,
afterEach,
beforeAll,
describe,
expect,
it,
vi,
} from 'vitest';
import { applyUpdate, Doc as YDoc } from 'yjs';
const tmpDir = path.join(__dirname, 'tmp');
const testDBFilePath = path.resolve(__dirname, 'old-db.affine');
const appDataPath = path.join(tmpDir, 'app-data');
beforeAll(() => {
vi.doMock('@affine/electron/helper/main-rpc', () => ({
mainRPC: {
getPath: async () => appDataPath,
channel: {
on: () => {},
send: () => {},
},
},
}));
});
afterEach(async () => {
await removeWithRetry(tmpDir);
});
afterAll(() => {
vi.doUnmock('@affine/electron/helper/main-rpc');
});
describe('migrateToSubdocAndReplaceDatabase', () => {
it('should migrate and replace the database', async () => {
const { copyToTemp, migrateToSubdocAndReplaceDatabase } = await import(
'@affine/electron/helper/db/migration'
);
const copiedDbFilePath = await copyToTemp(testDBFilePath);
await migrateToSubdocAndReplaceDatabase(copiedDbFilePath);
const db = new SqliteConnection(copiedDbFilePath);
await db.connect();
// check if db has two rows, one for root doc and one for subdoc
const rows = await db.getAllUpdates();
expect(rows.length).toBe(2);
const rootUpdate = rows.find(row => row.docId === undefined)!.data;
const subdocUpdate = rows.find(row => row.docId !== undefined)!.data;
expect(rootUpdate).toBeDefined();
expect(subdocUpdate).toBeDefined();
// apply updates
const rootDoc = new YDoc();
applyUpdate(rootDoc, rootUpdate);
// check if root doc has one subdoc
expect(rootDoc.subdocs.size).toBe(1);
// populates subdoc
applyUpdate(rootDoc.subdocs.values().next().value, subdocUpdate);
// check if root doc's meta is correct
const meta = rootDoc.getMap('meta').toJSON();
expect(meta.workspaceVersion).toBe(1);
expect(meta.name).toBe('hiw');
expect(meta.pages.length).toBe(1);
const pageMeta = meta.pages[0];
expect(pageMeta.title).toBe('Welcome to AFFiNEd');
// get the subdoc through id
const subDoc = rootDoc.getMap('spaces').get(pageMeta.id) as YDoc;
expect(subDoc).toEqual(rootDoc.subdocs.values().next().value);
await db.close();
});
});