Skip to content

Commit

Permalink
chore: remove legacy migration (#8793)
Browse files Browse the repository at this point in the history
  • Loading branch information
Saul-Mirone authored Nov 22, 2024
1 parent c4924fb commit 2c637a3
Show file tree
Hide file tree
Showing 16 changed files with 7 additions and 602 deletions.
129 changes: 1 addition & 128 deletions packages/affine/block-surface/src/surface-model.ts
Original file line number Diff line number Diff line change
@@ -1,134 +1,15 @@
import type { ConnectorElementModel } from '@blocksuite/affine-model';
import type { SurfaceBlockProps } from '@blocksuite/block-std/gfx';
import type { MigrationRunner, Y } from '@blocksuite/store';

import { SurfaceBlockModel as BaseSurfaceModel } from '@blocksuite/block-std/gfx';
import { DisposableGroup } from '@blocksuite/global/utils';
import {
Boxed,
defineBlockSchema,
DocCollection,
Text,
} from '@blocksuite/store';
import { defineBlockSchema, DocCollection } from '@blocksuite/store';

import { elementsCtorMap } from './element-model/index.js';
import { SurfaceBlockTransformer } from './surface-transformer.js';
import { connectorWatcher } from './watchers/connector.js';
import { groupRelationWatcher } from './watchers/group.js';

const migration = {
toV4: data => {
const { elements } = data;
if (elements instanceof Boxed) {
const value = elements.getValue();
if (!value) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
for (const [key, element] of (value as Record<string, any>).entries()) {
const type = element.get('type') as string;
if (type === 'shape' || type === 'text') {
const isBold = element.get('isBold');
const isItalic = element.get('isItalic');
element.delete('isBold');
element.delete('isItalic');
if (isBold) {
element.set('bold', true);
}
if (isItalic) {
element.set('italic', true);
}
}
if (type === 'connector') {
const source = element.get('source');
const target = element.get('target');
const sourceId = source['id'];
const targetId = target['id'];
if (!source['position'] && !sourceId) {
value.delete(key);
return;
}
if (!target['position'] && !targetId) {
value.delete(key);
return;
}
}
}
} else {
for (const key of Object.keys(elements)) {
const element = elements[key] as Record<string, unknown>;
const type = element['type'] as string;
if (type === 'shape' || type === 'text') {
const isBold = element['isBold'];
const isItalic = element['isItalic'];
delete element['isBold'];
delete element['isItalic'];
if (isBold) {
element['bold'] = true;
}
if (isItalic) {
element['italic'] = true;
}
}
if (type === 'connector') {
const source = element['source'] as Record<string, unknown>;
const target = element['target'] as Record<string, unknown>;
const sourceId = source['id'];
const targetId = target['id'];
// @ts-expect-error
if (!source['position'] && (!sourceId || !elements[sourceId])) {
delete elements[key];
return;
}
// @ts-expect-error
if (!target['position'] && (!targetId || !elements[targetId])) {
delete elements[key];
return;
}
}
}
}
},
toV5: data => {
const { elements } = data;
if (!((elements as object | Boxed) instanceof Boxed)) {
const yMap = new DocCollection.Y.Map() as Y.Map<Y.Map<unknown>>;

Object.entries(elements).forEach(([key, value]) => {
const map = new DocCollection.Y.Map();
Object.entries(value).forEach(([_key, _value]) => {
map.set(
_key,
_value instanceof DocCollection.Y.Text
? _value.clone()
: _value instanceof Text
? _value.yText.clone()
: _value
);
});
yMap.set(key, map);
});
const wrapper = new Boxed(yMap);
data.elements = wrapper;
}

const childrenMap = data.elements.getValue() as Y.Map<Y.Map<unknown>>;

for (const [id, element] of childrenMap) {
if (
element.get('type') === 'mindmap' ||
element.get('type') === 'group'
) {
const children = element.get('children') as Y.Map<Y.Map<unknown>>;

if (children?.size === 0) {
childrenMap.delete(id);
}
}
}
},
} satisfies Record<string, MigrationRunner<typeof SurfaceBlockSchema>>;

export const SurfaceBlockSchema = defineBlockSchema({
flavour: 'affine:surface',
props: (internalPrimitives): SurfaceBlockProps => ({
Expand All @@ -147,14 +28,6 @@ export const SurfaceBlockSchema = defineBlockSchema({
'affine:edgeless-text',
],
},
onUpgrade: (data, previousVersion, version) => {
if (previousVersion < 4 && version >= 4) {
migration.toV4(data);
}
if (previousVersion < 5 && version >= 5) {
migration.toV5(data);
}
},
transformer: () => new SurfaceBlockTransformer(),
toModel: () => new SurfaceBlockModel(),
});
Expand Down
39 changes: 2 additions & 37 deletions packages/affine/model/src/blocks/database/database-model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { MigrationRunner, Text } from '@blocksuite/store';
import type { Text } from '@blocksuite/store';

import { BlockModel, defineBlockSchema, nanoid } from '@blocksuite/store';
import { BlockModel, defineBlockSchema } from '@blocksuite/store';

import type { Column, SerializedCells, ViewBasicDataType } from './types.js';

Expand All @@ -13,36 +13,6 @@ export type DatabaseBlockProps = {

export class DatabaseBlockModel extends BlockModel<DatabaseBlockProps> {}

const migration = {
toV3: data => {
const id = nanoid();
// @ts-expect-error
const title = data['titleColumnName'];
// @ts-expect-error
const width = data['titleColumnWidth'];
// @ts-expect-error
delete data['titleColumnName'];
// @ts-expect-error
delete data['titleColumnWidth'];
data.columns.unshift({
id,
type: 'title',
name: title,
data: {},
});
data.views.forEach(view => {
if (view.mode === 'table') {
// @ts-ignore
view.columns.unshift({
id,
width,
statCalcType: 'none',
});
}
});
},
} satisfies Record<string, MigrationRunner<typeof DatabaseBlockSchema>>;

export const DatabaseBlockSchema = defineBlockSchema({
flavour: 'affine:database',
props: (internal): DatabaseBlockProps => ({
Expand All @@ -58,9 +28,4 @@ export const DatabaseBlockSchema = defineBlockSchema({
children: ['affine:paragraph', 'affine:list'],
},
toModel: () => new DatabaseBlockModel(),
onUpgrade: (data, previousVersion, latestVersion) => {
if (previousVersion < 3 && latestVersion >= 3) {
migration.toV3(data);
}
},
});
Loading

0 comments on commit 2c637a3

Please sign in to comment.