UBERF-7308: Upgrade model improvements (#5847)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2024-06-18 18:02:41 +07:00 committed by GitHub
parent bb6f9d7645
commit b3db3024f3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
35 changed files with 178 additions and 142 deletions

View File

@ -200,5 +200,5 @@ export const activityOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -17,5 +17,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien
export const attachmentOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -25,8 +25,8 @@ import { bitrixId } from '@hcengineering/bitrix'
export const bitrixOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, bitrixId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, bitrixId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -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<void> {
await tryUpgrade(client, boardId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, boardId, [
{
state: 'board0001',
func: async () => {
func: async (client) => {
const ops = new TxOperations(client, core.account.System)
await createDefaults(ops)
}

View File

@ -139,11 +139,12 @@ export const calendarOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, calendarId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
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' })
}
])
}

View File

@ -233,8 +233,8 @@ export const chunterOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, chunterId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, chunterId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -254,8 +254,8 @@ export const contactOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, contactId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, contactId, [
{
state: 'createSpace-v2',
func: async (client) => {

View File

@ -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<void> {
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<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
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)
}
}
])
}
}

View File

@ -185,8 +185,8 @@ export const coreOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, coreId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, coreId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -348,5 +348,5 @@ export const documentOperation: MigrateOperation = {
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -25,8 +25,8 @@ import gmail from './plugin'
export const gmailOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, gmailId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, gmailId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -61,8 +61,8 @@ export const guestOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, guestId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, guestId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -86,8 +86,8 @@ export const hrOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, hrId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, hrId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -25,8 +25,8 @@ import inventory from './plugin'
export const inventoryOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, inventoryId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, inventoryId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -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<void> {
await tryUpgrade(client, leadId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
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)
}

View File

@ -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<void> {
@ -82,9 +82,8 @@ async function createReception (client: MigrationUpgradeClient): Promise<void> {
export const loveOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
const tx = new TxOperations(client, core.account.System)
await tryUpgrade(client, loveId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
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
)
}
}

View File

@ -96,8 +96,8 @@ export const notificationOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, notificationId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, notificationId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -24,8 +24,8 @@ import preference, { preferenceId } from '@hcengineering/preference'
export const preferenceOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, preferenceId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, preferenceId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -17,5 +17,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien
export const loveOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -17,5 +17,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien
export const productsOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -7,5 +7,5 @@ import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClien
export const questionsOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -56,8 +56,8 @@ export const recruitOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, recruitId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, recruitId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -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<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -25,8 +25,8 @@ import setting from './plugin'
export const settingOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, settingId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, settingId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -10,8 +10,8 @@ import tags from './plugin'
export const tagsOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, tagsId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, tagsId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -586,8 +586,8 @@ export const taskOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, taskId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, taskId, [
{
state: 'defaults-v2',
func: createDefaults

View File

@ -32,8 +32,8 @@ export async function createSpace (client: MigrationUpgradeClient): Promise<void
export const telegramOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, telegramId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, telegramId, [
{
state: 'defaults-v2',
func: createSpace

View File

@ -25,8 +25,8 @@ import { templatesId } from '@hcengineering/templates'
export const templatesOperation: MigrateOperation = {
async migrate (client: MigrationClient): Promise<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, templatesId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, templatesId, [
{
state: 'create-defaults',
func: async (client) => {

View File

@ -189,5 +189,5 @@ export const textEditorOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -175,8 +175,8 @@ export const timeOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
await tryUpgrade(client, timeId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, timeId, [
{
state: 'create-defaults-v2',
func: async (client) => {

View File

@ -426,12 +426,12 @@ export const trackerOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
const tx = new TxOperations(client, core.account.System)
await tryUpgrade(client, trackerId, [
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
await tryUpgrade(state, client, trackerId, [
{
state: 'create-defaults',
func: async () => {
func: async (client) => {
const tx = new TxOperations(client, core.account.System)
await createDefaults(tx)
}
}

View File

@ -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<void> {},
async upgrade (client: MigrationUpgradeClient): Promise<void> {
const tx = new TxOperations(client, core.account.System)
await ensureTypedSpace(tx)
await ensureSequence(tx)
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {
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)
}
}
])
}
}

View File

@ -89,5 +89,5 @@ export const viewOperation: MigrateOperation = {
}
])
},
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
async upgrade (state: Map<string, Set<string>>, client: () => Promise<MigrationUpgradeClient>): Promise<void> {}
}

View File

@ -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<string, Set<string>>
}
export type MigrationUpgradeClient = Client
/**
* @public
@ -125,7 +123,11 @@ export interface MigrateOperation {
// Perform low level migration
migrate: (client: MigrationClient, logger: ModelLogger) => Promise<void>
// Perform high level upgrade operations.
upgrade: (client: MigrationUpgradeClient, logger: ModelLogger) => Promise<void>
upgrade: (
state: Map<string, Set<string>>,
client: () => Promise<MigrationUpgradeClient>,
logger: ModelLogger
) => Promise<void>
}
/**
@ -169,19 +171,21 @@ export async function tryMigrate (client: MigrationClient, plugin: string, migra
* @public
*/
export async function tryUpgrade (
client: MigrationUpgradeClient,
state: Map<string, Set<string>>,
client: () => Promise<MigrationUpgradeClient>,
plugin: string,
migrations: UpgradeOperations[]
): Promise<void> {
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<MigrationState> = {
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)
}
}

View File

@ -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<MigrationState>(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<Doc>).objectClass === contact.class.Person ||
(it as TxCUD<Doc>).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<CoreClient & BackupClient> =>
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 }