Skip to content

Commit

Permalink
Write migration for recalc template orders
Browse files Browse the repository at this point in the history
  • Loading branch information
poltak authored and blackforestboi committed Feb 26, 2024
1 parent 6a21bc0 commit 36c4d43
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion external/@worldbrain/memex-common
41 changes: 31 additions & 10 deletions src/background-script/quick-and-dirty-migrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ import { ROOT_NODE_PARENT_ID } from '@worldbrain/memex-common/lib/content-sharin
import {
DEFAULT_KEY,
DEFAULT_SPACE_BETWEEN,
defaultOrderableSorter,
recalculateOrders,
} from '@worldbrain/memex-common/lib/utils/item-ordering'
import { HIGHLIGHT_COLOR_KEY } from 'src/highlighting/constants'
import { DEFAULT_HIGHLIGHT_COLOR } from '@worldbrain/memex-common/lib/annotations/constants'
import type { SyncSettingsByFeature } from 'src/sync-settings/background/types'
import { HIGHLIGHT_COLORS_DEFAULT } from '@worldbrain/memex-common/lib/common-ui/components/highlightColorPicker/constants'
import type { CustomListTree } from '@worldbrain/memex-common/lib/types/core-data-types/client'
import type { Template } from 'src/copy-paster/types'

export interface MigrationProps {
db: Dexie
Expand Down Expand Up @@ -68,6 +69,33 @@ export const MIGRATION_PREFIX = '@QnDMigration-'
// __IMPORTANT NOTE__

export const migrations: Migrations = {
/*
* This exists as we released the templates with a new order field, same as cuomstListTrees,
* though we set the values for the static default templates to be less than the default space between.
* Thus it thinks it is out of room from the start and new templates cannot be added.
*/
[MIGRATION_PREFIX + 'reset-template-order-01']: async ({
storex,
bgModules,
}) => {
await bgModules.personalCloud.waitForSync()
const templates: Template[] = await storex
.collection('templates')
.findAllObjects({})
if (!templates.length) {
return
}
const reorderedTemplates = recalculateOrders(templates)
const batch: OperationBatch = reorderedTemplates.map((template) => ({
operation: 'updateObjects' as const,
collection: 'templates',
placeholder: `template-reorder-${template.id}`,
where: { id: template.id },
updates: { order: template.order },
}))

await storex.operation('executeBatch', batch)
},
/*
* This exists as I messed up the order assignments for new lists, which get inserted at the beginning.
* Previously they got order set to half of whatever the prev first list's order was. Though things
Expand Down Expand Up @@ -96,16 +124,9 @@ export const migrations: Migrations = {
const batch: OperationBatch = []

for (const nodes of nodesByParent.values()) {
nodes.sort(defaultOrderableSorter)
const middleIdx = Math.floor(nodes.length / 2)
for (let idx = 0; idx < nodes.length; idx++) {
const orderOffest = (idx - middleIdx) * DEFAULT_SPACE_BETWEEN
nodes[idx].order =
idx === middleIdx ? DEFAULT_KEY : DEFAULT_KEY + orderOffest
}

const reorderedNodes = recalculateOrders(nodes)
batch.push(
...nodes.map((node) => ({
...reorderedNodes.map((node) => ({
operation: 'updateObjects' as const,
collection: 'customListTrees',
placeholder: `tree-reorder-${node.id}`,
Expand Down

0 comments on commit 36c4d43

Please sign in to comment.