From b3db3024f336aae1533bb3aa29bdbf727280ea3a Mon Sep 17 00:00:00 2001 From: Andrey Sobolev Date: Tue, 18 Jun 2024 18:02:41 +0700 Subject: [PATCH] UBERF-7308: Upgrade model improvements (#5847) Signed-off-by: Andrey Sobolev --- models/activity/src/migration.ts | 2 +- models/attachment/src/migration.ts | 2 +- models/bitrix/src/migration.ts | 4 +- models/board/src/migration.ts | 16 ++--- models/calendar/src/migration.ts | 7 +- models/chunter/src/migration.ts | 4 +- models/contact/src/migration.ts | 4 +- models/controlled-documents/src/migration.ts | 28 +++++--- models/core/src/migration.ts | 4 +- models/document/src/migration.ts | 2 +- models/gmail/src/migration.ts | 4 +- models/guest/src/migration.ts | 4 +- models/hr/src/migration.ts | 4 +- models/inventory/src/migration.ts | 4 +- models/lead/src/migration.ts | 16 ++--- models/love/src/migration.ts | 36 ++++++----- models/notification/src/migration.ts | 4 +- models/preference/src/migration.ts | 4 +- models/print/src/migration.ts | 2 +- models/products/src/migration.ts | 2 +- models/questions/src/migration.ts | 2 +- models/recruit/src/migration.ts | 4 +- models/server-activity/src/migration.ts | 10 ++- models/setting/src/migration.ts | 4 +- models/tags/src/migration.ts | 4 +- models/task/src/migration.ts | 4 +- models/telegram/src/migration.ts | 4 +- models/templates/src/migration.ts | 4 +- models/text-editor/src/migration.ts | 2 +- models/time/src/migration.ts | 4 +- models/tracker/src/migration.ts | 8 +-- models/training/src/migration.ts | 26 ++++++-- models/view/src/migration.ts | 2 +- packages/model/src/migration.ts | 22 ++++--- server/tool/src/index.ts | 67 ++++++++++---------- 35 files changed, 178 insertions(+), 142 deletions(-) diff --git a/models/activity/src/migration.ts b/models/activity/src/migration.ts index bd71b84028..0e3ec28d23 100644 --- a/models/activity/src/migration.ts +++ b/models/activity/src/migration.ts @@ -200,5 +200,5 @@ export const activityOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/attachment/src/migration.ts b/models/attachment/src/migration.ts index 978d4f69a9..b820fd1b74 100644 --- a/models/attachment/src/migration.ts +++ b/models/attachment/src/migration.ts @@ -17,5 +17,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien export const attachmentOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/bitrix/src/migration.ts b/models/bitrix/src/migration.ts index d79acdebd6..be7da16447 100644 --- a/models/bitrix/src/migration.ts +++ b/models/bitrix/src/migration.ts @@ -25,8 +25,8 @@ import { bitrixId } from '@hcengineering/bitrix' export const bitrixOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, bitrixId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, bitrixId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/board/src/migration.ts b/models/board/src/migration.ts index 9e0249587e..4044e41f92 100644 --- a/models/board/src/migration.ts +++ b/models/board/src/migration.ts @@ -13,15 +13,15 @@ // limitations under the License. // -import { type Card, boardId } from '@hcengineering/board' +import { boardId, type Card } from '@hcengineering/board' import { TxOperations } from '@hcengineering/core' import { - type MigrateOperation, - type MigrationClient, - type MigrationUpgradeClient, createOrUpdate, tryMigrate, - tryUpgrade + tryUpgrade, + type MigrateOperation, + type MigrationClient, + type MigrationUpgradeClient } from '@hcengineering/model' import core from '@hcengineering/model-core' import { DOMAIN_TASK, createSequence } from '@hcengineering/model-task' @@ -90,11 +90,11 @@ export const boardOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, boardId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, boardId, [ { state: 'board0001', - func: async () => { + func: async (client) => { const ops = new TxOperations(client, core.account.System) await createDefaults(ops) } diff --git a/models/calendar/src/migration.ts b/models/calendar/src/migration.ts index e00cde0a33..de39d23ec4 100644 --- a/models/calendar/src/migration.ts +++ b/models/calendar/src/migration.ts @@ -139,11 +139,12 @@ export const calendarOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, calendarId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, calendarId, [ { state: 'default-space', - func: () => createDefaultSpace(client, calendar.space.Calendar, { name: 'Space for all events and calendars' }) + func: (client) => + createDefaultSpace(client, calendar.space.Calendar, { name: 'Space for all events and calendars' }) } ]) } diff --git a/models/chunter/src/migration.ts b/models/chunter/src/migration.ts index 798a2fc86b..1aca624a2c 100644 --- a/models/chunter/src/migration.ts +++ b/models/chunter/src/migration.ts @@ -233,8 +233,8 @@ export const chunterOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, chunterId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, chunterId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/contact/src/migration.ts b/models/contact/src/migration.ts index 00f7ea08df..506cc09d57 100644 --- a/models/contact/src/migration.ts +++ b/models/contact/src/migration.ts @@ -254,8 +254,8 @@ export const contactOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, contactId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, contactId, [ { state: 'createSpace-v2', func: async (client) => { diff --git a/models/controlled-documents/src/migration.ts b/models/controlled-documents/src/migration.ts index edb5218203..b619216820 100644 --- a/models/controlled-documents/src/migration.ts +++ b/models/controlled-documents/src/migration.ts @@ -7,6 +7,7 @@ import { createDefaultSpace, createOrUpdate, tryMigrate, + tryUpgrade, type MigrateOperation, type MigrationClient, type MigrationUpgradeClient @@ -293,15 +294,22 @@ export const documentsOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - const tx = new TxOperations(client, core.account.System) - await createDefaultSpace(client, documents.space.Documents, { name: 'Documents', description: 'Documents' }) - await createQualityDocumentsSpace(tx) - await createTemplatesSpace(tx) - await createTemplateSequence(tx) - await createTagCategories(tx) - await createDocumentCategories(tx) - await fixChangeControlsForDocs(tx) - await createProductChangeControlTemplate(tx) + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, documentsId, [ + { + state: 'init-documents', + func: async (client) => { + const tx = new TxOperations(client, core.account.System) + await createDefaultSpace(client, documents.space.Documents, { name: 'Documents', description: 'Documents' }) + await createQualityDocumentsSpace(tx) + await createTemplatesSpace(tx) + await createTemplateSequence(tx) + await createTagCategories(tx) + await createDocumentCategories(tx) + await fixChangeControlsForDocs(tx) + await createProductChangeControlTemplate(tx) + } + } + ]) } } diff --git a/models/core/src/migration.ts b/models/core/src/migration.ts index 4536dccd76..28fe968978 100644 --- a/models/core/src/migration.ts +++ b/models/core/src/migration.ts @@ -185,8 +185,8 @@ export const coreOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, coreId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, coreId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/document/src/migration.ts b/models/document/src/migration.ts index b3067fb350..d237959e87 100644 --- a/models/document/src/migration.ts +++ b/models/document/src/migration.ts @@ -348,5 +348,5 @@ export const documentOperation: MigrateOperation = { ]) }, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/gmail/src/migration.ts b/models/gmail/src/migration.ts index 8e309069bf..d24ee0985f 100644 --- a/models/gmail/src/migration.ts +++ b/models/gmail/src/migration.ts @@ -25,8 +25,8 @@ import gmail from './plugin' export const gmailOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, gmailId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, gmailId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/guest/src/migration.ts b/models/guest/src/migration.ts index bec46e0ebb..4a3b77f9fd 100644 --- a/models/guest/src/migration.ts +++ b/models/guest/src/migration.ts @@ -61,8 +61,8 @@ export const guestOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, guestId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, guestId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/hr/src/migration.ts b/models/hr/src/migration.ts index e86e838440..30486404a8 100644 --- a/models/hr/src/migration.ts +++ b/models/hr/src/migration.ts @@ -86,8 +86,8 @@ export const hrOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, hrId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, hrId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/inventory/src/migration.ts b/models/inventory/src/migration.ts index c02e4359aa..d27e2df771 100644 --- a/models/inventory/src/migration.ts +++ b/models/inventory/src/migration.ts @@ -25,8 +25,8 @@ import inventory from './plugin' export const inventoryOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, inventoryId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, inventoryId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/lead/src/migration.ts b/models/lead/src/migration.ts index 7887118e15..fc06bd5766 100644 --- a/models/lead/src/migration.ts +++ b/models/lead/src/migration.ts @@ -13,20 +13,20 @@ // limitations under the License. // -import { AccountRole, DOMAIN_TX, type Ref, type Status, TxOperations } from '@hcengineering/core' -import { type Lead, leadId } from '@hcengineering/lead' +import { AccountRole, DOMAIN_TX, TxOperations, type Ref, type Status } from '@hcengineering/core' +import { leadId, type Lead } from '@hcengineering/lead' import { - type ModelLogger, tryMigrate, tryUpgrade, type MigrateOperation, type MigrationClient, - type MigrationUpgradeClient + type MigrationUpgradeClient, + type ModelLogger } from '@hcengineering/model' import core, { DOMAIN_SPACE } from '@hcengineering/model-core' -import task, { DOMAIN_TASK, createSequence, migrateDefaultStatusesBase } from '@hcengineering/model-task' import contact from '@hcengineering/model-contact' +import task, { DOMAIN_TASK, createSequence, migrateDefaultStatusesBase } from '@hcengineering/model-task' import lead from './plugin' import { defaultLeadStatuses } from './spaceType' @@ -194,11 +194,11 @@ export const leadOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, leadId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, leadId, [ { state: 'u-default-funnel', - func: async () => { + func: async (client) => { const ops = new TxOperations(client, core.account.System) await createDefaults(ops) } diff --git a/models/love/src/migration.ts b/models/love/src/migration.ts index e854b2a67f..573d40f0f5 100644 --- a/models/love/src/migration.ts +++ b/models/love/src/migration.ts @@ -16,6 +16,7 @@ import contact from '@hcengineering/contact' import { TxOperations, type Ref } from '@hcengineering/core' import drive from '@hcengineering/drive' +import { RoomAccess, RoomType, createDefaultRooms, isOffice, loveId, type Floor } from '@hcengineering/love' import { createDefaultSpace, tryUpgrade, @@ -24,7 +25,6 @@ import { type MigrationUpgradeClient } from '@hcengineering/model' import core from '@hcengineering/model-core' -import { RoomAccess, RoomType, createDefaultRooms, isOffice, loveId, type Floor } from '@hcengineering/love' import love from './plugin' async function createDefaultFloor (tx: TxOperations): Promise { @@ -82,9 +82,8 @@ async function createReception (client: MigrationUpgradeClient): Promise { export const loveOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - const tx = new TxOperations(client, core.account.System) - await tryUpgrade(client, loveId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, loveId, [ { state: 'create-defaults-v2', func: async (client) => { @@ -94,6 +93,7 @@ export const loveOperation: MigrateOperation = { { state: 'initial-defaults', func: async (client) => { + const tx = new TxOperations(client, core.account.System) await createDefaultFloor(tx) } }, @@ -107,19 +107,23 @@ export const loveOperation: MigrateOperation = { func: async (client) => { await createReception(client) } + }, + { + state: 'create-drive', + func: async (client) => { + await createDefaultSpace( + client, + love.space.Drive, + { + name: 'Records', + description: 'Office records', + type: drive.spaceType.DefaultDrive, + autoJoin: true + }, + drive.class.Drive + ) + } } ]) - - await createDefaultSpace( - client, - love.space.Drive, - { - name: 'Records', - description: 'Office records', - type: drive.spaceType.DefaultDrive, - autoJoin: true - }, - drive.class.Drive - ) } } diff --git a/models/notification/src/migration.ts b/models/notification/src/migration.ts index 5001f762e4..be43052634 100644 --- a/models/notification/src/migration.ts +++ b/models/notification/src/migration.ts @@ -96,8 +96,8 @@ export const notificationOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, notificationId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, notificationId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/preference/src/migration.ts b/models/preference/src/migration.ts index fc60d37e58..9170d9a0e1 100644 --- a/models/preference/src/migration.ts +++ b/models/preference/src/migration.ts @@ -24,8 +24,8 @@ import preference, { preferenceId } from '@hcengineering/preference' export const preferenceOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, preferenceId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, preferenceId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/print/src/migration.ts b/models/print/src/migration.ts index a388f11f29..0a67d7efdd 100644 --- a/models/print/src/migration.ts +++ b/models/print/src/migration.ts @@ -17,5 +17,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien export const loveOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/products/src/migration.ts b/models/products/src/migration.ts index c26abeb4bb..d887359231 100644 --- a/models/products/src/migration.ts +++ b/models/products/src/migration.ts @@ -17,5 +17,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien export const productsOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/questions/src/migration.ts b/models/questions/src/migration.ts index 97db1ae447..09638a141c 100644 --- a/models/questions/src/migration.ts +++ b/models/questions/src/migration.ts @@ -7,5 +7,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien export const questionsOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/recruit/src/migration.ts b/models/recruit/src/migration.ts index 3bbf4d6db1..ff36b7a2e3 100644 --- a/models/recruit/src/migration.ts +++ b/models/recruit/src/migration.ts @@ -56,8 +56,8 @@ export const recruitOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, recruitId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, recruitId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/server-activity/src/migration.ts b/models/server-activity/src/migration.ts index eb96ddec6b..86640bd7c3 100644 --- a/models/server-activity/src/migration.ts +++ b/models/server-activity/src/migration.ts @@ -30,7 +30,13 @@ import core, { type TxCollectionCUD, type TxCreateDoc } from '@hcengineering/core' -import { tryMigrate, type MigrateOperation, type MigrationClient, type MigrationIterator } from '@hcengineering/model' +import { + tryMigrate, + type MigrateOperation, + type MigrationClient, + type MigrationIterator, + type MigrationUpgradeClient +} from '@hcengineering/model' import { DOMAIN_ACTIVITY } from '@hcengineering/model-activity' import { getAllObjectTransactions, @@ -275,5 +281,5 @@ export const activityServerOperation: MigrateOperation = { } ]) }, - async upgrade (): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/setting/src/migration.ts b/models/setting/src/migration.ts index 6105352bbe..84a571ba2b 100644 --- a/models/setting/src/migration.ts +++ b/models/setting/src/migration.ts @@ -25,8 +25,8 @@ import setting from './plugin' export const settingOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, settingId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, settingId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/tags/src/migration.ts b/models/tags/src/migration.ts index a07b01c68d..c0b0a47dbc 100644 --- a/models/tags/src/migration.ts +++ b/models/tags/src/migration.ts @@ -10,8 +10,8 @@ import tags from './plugin' export const tagsOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, tagsId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, tagsId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/task/src/migration.ts b/models/task/src/migration.ts index a43aad23f7..740a97e9c8 100644 --- a/models/task/src/migration.ts +++ b/models/task/src/migration.ts @@ -586,8 +586,8 @@ export const taskOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, taskId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, taskId, [ { state: 'defaults-v2', func: createDefaults diff --git a/models/telegram/src/migration.ts b/models/telegram/src/migration.ts index d11bc1eb95..acf909b2ce 100644 --- a/models/telegram/src/migration.ts +++ b/models/telegram/src/migration.ts @@ -32,8 +32,8 @@ export async function createSpace (client: MigrationUpgradeClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, telegramId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, telegramId, [ { state: 'defaults-v2', func: createSpace diff --git a/models/templates/src/migration.ts b/models/templates/src/migration.ts index f5de1438c0..d1bac2a5ec 100644 --- a/models/templates/src/migration.ts +++ b/models/templates/src/migration.ts @@ -25,8 +25,8 @@ import { templatesId } from '@hcengineering/templates' export const templatesOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, templatesId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, templatesId, [ { state: 'create-defaults', func: async (client) => { diff --git a/models/text-editor/src/migration.ts b/models/text-editor/src/migration.ts index ad667d5f99..f999d0910a 100644 --- a/models/text-editor/src/migration.ts +++ b/models/text-editor/src/migration.ts @@ -189,5 +189,5 @@ export const textEditorOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/models/time/src/migration.ts b/models/time/src/migration.ts index 328a6be506..dc42f31704 100644 --- a/models/time/src/migration.ts +++ b/models/time/src/migration.ts @@ -175,8 +175,8 @@ export const timeOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - await tryUpgrade(client, timeId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, timeId, [ { state: 'create-defaults-v2', func: async (client) => { diff --git a/models/tracker/src/migration.ts b/models/tracker/src/migration.ts index 0acd5ff1d1..cc84a53835 100644 --- a/models/tracker/src/migration.ts +++ b/models/tracker/src/migration.ts @@ -426,12 +426,12 @@ export const trackerOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise { - const tx = new TxOperations(client, core.account.System) - await tryUpgrade(client, trackerId, [ + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, trackerId, [ { state: 'create-defaults', - func: async () => { + func: async (client) => { + const tx = new TxOperations(client, core.account.System) await createDefaults(tx) } } diff --git a/models/training/src/migration.ts b/models/training/src/migration.ts index 8b505342e9..918a7e12a8 100644 --- a/models/training/src/migration.ts +++ b/models/training/src/migration.ts @@ -13,17 +13,29 @@ // limitations under the License. // -import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClient } from '@hcengineering/model' -import { type Ref, TxOperations, type TypedSpace } from '@hcengineering/core' +import { TxOperations, type Ref, type TypedSpace } from '@hcengineering/core' +import { + tryUpgrade, + type MigrateOperation, + type MigrationClient, + type MigrationUpgradeClient +} from '@hcengineering/model' import core from '@hcengineering/model-core' -import training, { type Sequence } from '@hcengineering/training' +import training, { trainingId, type Sequence } from '@hcengineering/training' export const trainingOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise {}, - async upgrade (client: MigrationUpgradeClient): Promise { - const tx = new TxOperations(client, core.account.System) - await ensureTypedSpace(tx) - await ensureSequence(tx) + async upgrade (state: Map>, client: () => Promise): Promise { + await tryUpgrade(state, client, trainingId, [ + { + state: 'create-defaults', + func: async (client) => { + const tx = new TxOperations(client, core.account.System) + await ensureTypedSpace(tx) + await ensureSequence(tx) + } + } + ]) } } diff --git a/models/view/src/migration.ts b/models/view/src/migration.ts index 16c469b475..f5587c0547 100644 --- a/models/view/src/migration.ts +++ b/models/view/src/migration.ts @@ -89,5 +89,5 @@ export const viewOperation: MigrateOperation = { } ]) }, - async upgrade (client: MigrationUpgradeClient): Promise {} + async upgrade (state: Map>, client: () => Promise): Promise {} } diff --git a/packages/model/src/migration.ts b/packages/model/src/migration.ts index c0fa75d15d..b901b9606a 100644 --- a/packages/model/src/migration.ts +++ b/packages/model/src/migration.ts @@ -20,8 +20,8 @@ import core, { WorkspaceId, generateId } from '@hcengineering/core' -import { ModelLogger } from './utils' import { StorageAdapter } from '@hcengineering/storage' +import { ModelLogger } from './utils' /** * @public @@ -112,9 +112,7 @@ export interface MigrationClient { /** * @public */ -export type MigrationUpgradeClient = Client & { - migrateState: Map> -} +export type MigrationUpgradeClient = Client /** * @public @@ -125,7 +123,11 @@ export interface MigrateOperation { // Perform low level migration migrate: (client: MigrationClient, logger: ModelLogger) => Promise // Perform high level upgrade operations. - upgrade: (client: MigrationUpgradeClient, logger: ModelLogger) => Promise + upgrade: ( + state: Map>, + client: () => Promise, + logger: ModelLogger + ) => Promise } /** @@ -169,19 +171,21 @@ export async function tryMigrate (client: MigrationClient, plugin: string, migra * @public */ export async function tryUpgrade ( - client: MigrationUpgradeClient, + state: Map>, + client: () => Promise, plugin: string, migrations: UpgradeOperations[] ): Promise { - const states = client.migrateState.get(plugin) ?? new Set() + const states = state.get(plugin) ?? new Set() for (const migration of migrations) { if (states.has(migration.state)) continue - await migration.func(client) + const _client = await client() + await migration.func(_client) const st: Data = { plugin, state: migration.state } - const tx = new TxOperations(client, core.account.System) + const tx = new TxOperations(_client, core.account.System) await tx.createDoc(core.class.MigrationState, core.space.Configuration, st) } } diff --git a/server/tool/src/index.ts b/server/tool/src/index.ts index 1d08c0ad83..cd7091e69b 100644 --- a/server/tool/src/index.ts +++ b/server/tool/src/index.ts @@ -27,7 +27,9 @@ import core, { MigrationState, ModelDb, Tx, - WorkspaceId + WorkspaceId, + type Doc, + type TxCUD } from '@hcengineering/core' import { consoleModelLogger, MigrateOperation, ModelLogger } from '@hcengineering/model' import { createMongoTxAdapter, DBCollectionHelper, getMongoClient, getWorkspaceDB } from '@hcengineering/mongo' @@ -141,13 +143,12 @@ export async function initModel ( const states = await connection.findAll(core.class.MigrationState, {}) const sts = Array.from(groupByArray(states, (it) => it.plugin).entries()) const migrateState = new Map(sts.map((it) => [it[0], new Set(it[1].map((q) => q.state))])) - ;(connection as any).migrateState = migrateState try { let i = 0 for (const op of migrateOperations) { logger.log('Migrate', { name: op[0] }) - await op[1].upgrade(connection as any, logger) + await op[1].upgrade(migrateState, async () => connection as any, logger) i++ await progress(20 + (((100 / migrateOperations.length) * i) / 100) * 10) } @@ -247,7 +248,6 @@ export async function upgradeModel ( }) ) logger.log('transactions deleted.', { workspaceId: workspaceId.name, count: result.deletedCount }) - logger.log('creating model...', { workspaceId: workspaceId.name }) const insert = await ctx.with( 'mongo-insert', @@ -258,8 +258,19 @@ export async function upgradeModel ( logger.log('model transactions inserted.', { workspaceId: workspaceId.name, count: insert.insertedCount }) await progress(20) } + const newModel = [ + ...txes, + ...Array.from( + prevModel.model.filter( + (it) => + it.modifiedBy !== core.account.System || + (it as TxCUD).objectClass === contact.class.Person || + (it as TxCUD).objectClass === 'contact:class:EmployeeAccount' + ) + ) + ] - const { hierarchy, modelDb, model } = await fetchModelFromMongo(ctx, mongodbUri, workspaceId) + const { hierarchy, modelDb, model } = await fetchModelFromMongo(ctx, mongodbUri, workspaceId, newModel) const { migrateClient, migrateState } = await prepareMigrationClient( db, hierarchy, @@ -286,11 +297,13 @@ export async function upgradeModel ( }) logger.log('Apply upgrade operations', { workspaceId: workspaceId.name }) - const connection = (await ctx.with( - 'connect-platform', - {}, - async (ctx) => - await connect( + let connection: (CoreClient & BackupClient) | undefined + const getUpgradeClient = async (): Promise => + await ctx.with('connect-platform', {}, async (ctx) => { + if (connection !== undefined) { + return connection + } + connection = (await connect( transactorUrl, workspaceId, undefined, @@ -300,32 +313,23 @@ export async function upgradeModel ( admin: 'true' }, model - ) - )) as CoreClient & BackupClient + )) as CoreClient & BackupClient + return connection + }) try { await ctx.with('upgrade', {}, async () => { let i = 0 for (const op of migrateOperations) { const t = Date.now() - ;(connection as any).migrateState = migrateState - await op[1].upgrade(connection as any, logger) + await op[1].upgrade(migrateState, getUpgradeClient, logger) logger.log('upgrade:', { operation: op[0], time: Date.now() - t, workspaceId: workspaceId.name }) await progress(60 + ((100 / migrateOperations.length) * i * 40) / 100) i++ } }) - - if (!skipTxUpdate) { - // Create update indexes - await ctx.with('create-indexes', {}, async (ctx) => { - await createUpdateIndexes(ctx, connection, db, logger, async (value) => { - await progress(40 + (Math.min(value, 100) / 100) * 20) - }) - }) - } } finally { - await connection.sendForceClose() - await connection.close() + await connection?.sendForceClose() + await connection?.close() } return model } finally { @@ -357,26 +361,23 @@ async function prepareMigrationClient ( async function fetchModelFromMongo ( ctx: MeasureContext, mongodbUri: string, - workspaceId: WorkspaceId + workspaceId: WorkspaceId, + model?: Tx[] ): Promise<{ hierarchy: Hierarchy, modelDb: ModelDb, model: Tx[] }> { const hierarchy = new Hierarchy() const modelDb = new ModelDb(hierarchy) const txAdapter = await createMongoTxAdapter(ctx, hierarchy, mongodbUri, workspaceId, modelDb) - const model = await ctx.with('get-model', {}, async (ctx) => await txAdapter.getModel(ctx)) + model = model ?? (await ctx.with('get-model', {}, async (ctx) => await txAdapter.getModel(ctx))) await ctx.with('build local model', {}, async () => { - for (const tx of model) { + for (const tx of model ?? []) { try { hierarchy.tx(tx) } catch (err: any) {} } - for (const tx of model) { - try { - await modelDb.tx(tx) - } catch (err: any) {} - } + modelDb.addTxes(ctx, model as Tx[], false) }) await txAdapter.close() return { hierarchy, modelDb, model }