diff --git a/models/controlled-documents/src/migration.ts b/models/controlled-documents/src/migration.ts index 817c49fffd..edb5218203 100644 --- a/models/controlled-documents/src/migration.ts +++ b/models/controlled-documents/src/migration.ts @@ -2,18 +2,7 @@ // Copyright @ 2022-2023 Hardcore Engineering Inc. // -import { - type Data, - type Ref, - TxOperations, - type Class, - type Space, - generateId, - DOMAIN_TX, - AccountRole, - DOMAIN_DOC_INDEX_STATE, - getCollaborativeDoc -} from '@hcengineering/core' +import { type Data, type Ref, TxOperations, generateId, DOMAIN_TX, getCollaborativeDoc } from '@hcengineering/core' import { createDefaultSpace, createOrUpdate, @@ -22,20 +11,11 @@ import { type MigrationClient, type MigrationUpgradeClient } from '@hcengineering/model' -import core, { DOMAIN_SPACE } from '@hcengineering/model-core' -import contact from '@hcengineering/model-contact' +import core from '@hcengineering/model-core' import tags from '@hcengineering/tags' import { type ChangeControl, type DocumentCategory, - type Document, - type DocumentMeta, - type DocumentSpace, - type HierarchyDocument, - type ProjectDocument, - type ProjectMeta, - TEMPLATE_PREFIX, - getDocumentId, DocumentState, documentsId, createDocumentTemplate, @@ -43,154 +23,7 @@ import { createChangeControl } from '@hcengineering/controlled-documents' -import documents, { DOMAIN_DOCUMENTS } from './index' - -async function migrateDocumentCategories (client: MigrationClient): Promise { - const oldSpace = 'documents:space:Documents' as Ref - - await client.update( - DOMAIN_DOCUMENTS, - { - _class: documents.class.DocumentCategory, - space: oldSpace - }, - { - $set: { - space: documents.space.QualityDocuments - } - } - ) -} - -async function migrateUnusedDocumentSpaces (client: MigrationClient): Promise { - const spaces: Ref[] = [ - 'events:space:EventsDocuments' as Ref, - 'clients:space:ClientsDocuments' as Ref, - 'supplier:space:SuppliersDocuments' as Ref, - 'trainings:space:TrainingsDocuments' as Ref - ] - - await client.update( - DOMAIN_SPACE, - { - _id: { $in: spaces }, - _class: documents.class.DocumentSpace, - archived: false - }, - { - $set: { - archived: true - } - } - ) -} - -async function migrateProjectMeta (client: MigrationClient): Promise { - type ExDocumentMeta = DocumentMeta & { path: Ref[] } - - const metaToMigrate = await client.find(DOMAIN_DOCUMENTS, { - _class: documents.class.DocumentMeta, - path: { $exists: true } - }) - - for (const meta of metaToMigrate) { - const projectMetaId: Ref = generateId() - - const docs = await client.find(DOMAIN_DOCUMENTS, { - attachedTo: meta._id - }) - - for (const doc of docs) { - await client.create(DOMAIN_DOCUMENTS, { - _id: generateId(), - _class: documents.class.ProjectDocument, - space: doc.space, - modifiedBy: core.account.System, - modifiedOn: Date.now(), - attachedTo: projectMetaId, - attachedToClass: documents.class.ProjectMeta, - collection: 'documents', - project: documents.ids.NoProject, - initial: documents.ids.NoProject, - document: doc._id - }) - } - - await client.create(DOMAIN_DOCUMENTS, { - _id: projectMetaId, - _class: documents.class.ProjectMeta, - space: meta.space, - modifiedBy: core.account.System, - modifiedOn: Date.now(), - project: documents.ids.NoProject, - meta: meta._id, - path: meta.path, - parent: meta.path[0] ?? documents.ids.NoParent, - documents: docs.length - }) - - await client.update( - DOMAIN_DOCUMENTS, - { _id: meta._id }, - { - $unset: { - path: undefined - } - } - ) - } -} - -async function migrateDocumentSpaces (client: MigrationClient): Promise { - const affectedClasses: Ref>[] = [ - documents.class.DocumentSpace, - documents.class.OrgSpace, - documents.class.ExternalSpace - ] - - await client.update( - DOMAIN_SPACE, - { - _class: { $in: affectedClasses }, - type: { $exists: false } - }, - { - $set: { - type: documents.spaceType.DocumentSpaceType - } - } - ) - - await client.update( - DOMAIN_SPACE, - { - _class: { $in: affectedClasses }, - type: 'documents:spaceType:DefaultDocLibraryType' - }, - { - $set: { - type: documents.spaceType.DocumentSpaceType - }, - $rename: { - 'documents:spaceType:DefaultDocLibraryType:type:mixin': 'documents:spaceType:DocumentSpaceType:type:mixin' - } - } - ) -} - -async function migrateLibraryDocumentSpace (client: MigrationClient): Promise { - await client.update( - DOMAIN_SPACE, - { - _class: 'documents:class:LibraryDocumentSpace' as Ref> - }, - { - $set: { - _class: documents.class.DocumentSpace - } - } - ) -} +import documents from './index' async function createTemplatesSpace (tx: TxOperations): Promise { const existingSpace = await tx.findOne(documents.class.DocumentSpace, { @@ -239,153 +72,6 @@ async function createQualityDocumentsSpace (tx: TxOperations): Promise { } } -async function migrateSpace (client: MigrationClient): Promise { - await client.update( - DOMAIN_SPACE, - { - _id: documents.space.Documents, - _class: documents.class.DocumentSpace - }, - { - $set: { - _class: core.class.Space - } - } - ) -} - -async function migrateEffectiveDates (client: MigrationClient): Promise { - await client.update( - DOMAIN_DOCUMENTS, - { - effectiveDate: 0 - }, - { - $set: { - plannedEffectiveDate: 0 - }, - $unset: { - effectiveDate: undefined - } - } - ) - - const docsToRelease = await client.find(DOMAIN_DOCUMENTS, { effectiveDate: { $gt: Date.now() } }) - - for (const doc of docsToRelease) { - const plannedEffectiveDate = (doc as any).effectiveDate - await client.update( - DOMAIN_DOCUMENTS, - { _id: doc._id }, - { - $set: { - plannedEffectiveDate - }, - $unset: { - effectiveDate: undefined - } - } - ) - } -} - -async function migrateChangeControls (client: MigrationClient): Promise { - await client.update( - DOMAIN_DOCUMENTS, - { _class: documents.class.ChangeControl, impactedDocuments: { $exists: false } }, - { - $set: { - impactedDocuments: [] - } - } - ) -} - -async function migrateTemplatePrefixes (client: MigrationClient): Promise { - const templatesToMigrate = await client.find(DOMAIN_DOCUMENTS, { - [documents.mixin.DocumentTemplate]: { $exists: true }, - prefix: { $ne: TEMPLATE_PREFIX } - }) - - for (const tmp of templatesToMigrate) { - await client.update( - DOMAIN_DOCUMENTS, - { - _id: tmp._id - }, - { - $set: { - prefix: TEMPLATE_PREFIX, - [`${documents.mixin.DocumentTemplate}.docPrefix`]: tmp.prefix - } - } - ) - } -} - -async function migrateDocumentCodes (client: MigrationClient): Promise { - const classes = client.hierarchy.getDescendants(documents.class.Document) - const docsToMigrate = await client.find(DOMAIN_DOCUMENTS, { - _class: { $in: classes }, - code: { $exists: false } - }) - - for (const doc of docsToMigrate) { - await client.update( - DOMAIN_DOCUMENTS, - { - _id: doc._id - }, - { - $set: { - code: getDocumentId(doc) - } - } - ) - } -} - -async function migrateCollaborativeDocument (client: MigrationClient): Promise { - const mixin = 'documents:mixin:CollaborativeDocument' - - const docsToMigrate = await client.find(DOMAIN_DOCUMENTS, { - [`${mixin}.document`]: { $exists: true } - }) - - for (const doc of docsToMigrate) { - const content = (doc as any)[mixin].document - const snapshots = (doc as any)[mixin].snapshots ?? [] - - // move mixin document -> content field - await client.update( - DOMAIN_DOCUMENTS, - { _id: doc._id }, - { - $set: { - content, - snapshots: snapshots.length - }, - $unset: { - [mixin]: undefined - } - } - ) - - // make snapshots attached docs - await client.update( - DOMAIN_DOCUMENTS, - { _id: { $in: snapshots } }, - { - $set: { - attachedTo: doc._id, - attachedToClass: doc._class, - collection: 'snapshots' - } - } - ) - } -} - async function fixChangeControlsForDocs (tx: TxOperations): Promise { const defaultCCSpec: Data = { description: '', @@ -582,104 +268,6 @@ async function createTagCategories (tx: TxOperations): Promise { ) } -async function migrateDocumentSpacesMixins (client: MigrationClient): Promise { - const oldSpaceTypeMixin = `${documents.spaceType.DocumentSpaceType}:type:mixin` - const newSpaceTypeMixin = documents.mixin.DocumentSpaceTypeData - const affectedClasses: Ref>[] = [ - documents.class.DocumentSpace, - documents.class.OrgSpace, - documents.class.ExternalSpace - ] - - await client.update( - DOMAIN_TX, - { - objectClass: core.class.Attribute, - 'attributes.attributeOf': oldSpaceTypeMixin - }, - { - $set: { - 'attributes.attributeOf': newSpaceTypeMixin - } - } - ) - - await client.update( - DOMAIN_SPACE, - { - _class: { $in: affectedClasses }, - [oldSpaceTypeMixin]: { $exists: true } - }, - { - $rename: { - [oldSpaceTypeMixin]: newSpaceTypeMixin - } - } - ) -} - -async function migrateDefaultProjectOwners (client: MigrationClient): Promise { - const workspaceOwners = await client.model.findAll(contact.class.PersonAccount, { - role: AccountRole.Owner - }) - - await client.update( - DOMAIN_SPACE, - { - _id: documents.space.QualityDocuments, - $or: [{ owners: { $exists: false } }, { owners: [core.account.System] }] - }, - { - $set: { - owners: workspaceOwners.map((it) => it._id) - } - } - ) -} - -async function migrateMetaTitles (client: MigrationClient): Promise { - const targetMetas = await client.find(DOMAIN_DOCUMENTS, { title: { $exists: false } }) - - for (const meta of targetMetas) { - let targetDoc = ( - await client.find(DOMAIN_DOCUMENTS, { attachedTo: meta._id, state: DocumentState.Effective }) - )[0] - - if (targetDoc === undefined) { - targetDoc = (await client.find(DOMAIN_DOCUMENTS, { attachedTo: meta._id }))[0] - } - - if (targetDoc === undefined) { - continue - } - - await client.update( - DOMAIN_DOCUMENTS, - { _id: meta._id }, - { - $set: { - title: `${getDocumentId(targetDoc)} ${targetDoc.title}` - } - } - ) - } -} - -async function migrateMetaIndexState (client: MigrationClient): Promise { - await client.update( - DOMAIN_DOC_INDEX_STATE, - { - _class: core.class.DocIndexState, - objectClass: documents.class.DocumentMeta - }, - { - $set: { - stages: {} - } - } - ) -} - async function migrateSpaceTypes (client: MigrationClient): Promise { await client.update( DOMAIN_TX, @@ -698,26 +286,7 @@ async function migrateSpaceTypes (client: MigrationClient): Promise { export const documentsOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise { - await migrateSpace(client) - await migrateEffectiveDates(client) - await migrateChangeControls(client) - await migrateTemplatePrefixes(client) - // Note: Order matters! It is important to migrate template prefixes first. - await migrateDocumentCodes(client) - await migrateCollaborativeDocument(client) - await migrateLibraryDocumentSpace(client) - await migrateDocumentSpaces(client) - await migrateDocumentCategories(client) - await migrateUnusedDocumentSpaces(client) - await migrateProjectMeta(client) - await migrateDocumentSpacesMixins(client) - await migrateDefaultProjectOwners(client) - await migrateMetaTitles(client) await tryMigrate(client, documentsId, [ - { - state: 'migrateMetaIndexState', - func: migrateMetaIndexState - }, { state: 'migrateSpaceTypes', func: migrateSpaceTypes diff --git a/models/products/src/migration.ts b/models/products/src/migration.ts index f9b048191d..c26abeb4bb 100644 --- a/models/products/src/migration.ts +++ b/models/products/src/migration.ts @@ -13,101 +13,9 @@ // limitations under the License. // -import { type Employee } from '@hcengineering/contact' -import { type Ref, DOMAIN_TX } from '@hcengineering/core' -import { - tryMigrate, - type MigrateOperation, - type MigrationClient, - type MigrationUpgradeClient -} from '@hcengineering/model' -import core, { DOMAIN_SPACE } from '@hcengineering/model-core' -import { type Product, productsId } from '@hcengineering/products' -import documents from '@hcengineering/controlled-documents' - -import products from './plugin' - -async function migrateProductSpacesMixins (client: MigrationClient): Promise { - const oldSpaceTypeMixin = `${products.spaceType.ProductType}:type:mixin` - const newSpaceTypeMixin = products.mixin.ProductTypeData - - await client.update( - DOMAIN_TX, - { - objectClass: core.class.Attribute, - 'attributes.attributeOf': oldSpaceTypeMixin - }, - { - $set: { - 'attributes.attributeOf': newSpaceTypeMixin - } - } - ) - - await client.update( - DOMAIN_SPACE, - { - _class: products.class.Product, - [oldSpaceTypeMixin]: { $exists: true } - }, - { - $rename: { - [oldSpaceTypeMixin]: newSpaceTypeMixin - } - } - ) -} - -async function migrateProductOwner (client: MigrationClient): Promise { - type ExProduct = Product & { owner: Ref } - - const docs = await client.find(DOMAIN_SPACE, { _class: products.class.Product, owner: { $exists: true } }) - for (const doc of docs) { - if (doc.owner == null) continue - const currentOwners = doc.owners ?? [] - const currenMembers = doc.members ?? [] - - const owners = Array.from(new Set(...currentOwners, doc.owner)) - const members = Array.from(new Set(...currenMembers, ...owners)) - - await client.update( - DOMAIN_SPACE, - { _id: doc._id }, - { - $set: { owners, members }, - $unset: { owner: undefined } - } - ) - } -} - -async function migrateSpaceTypes (client: MigrationClient): Promise { - await client.update( - DOMAIN_TX, - { - _class: core.class.TxCreateDoc, - objectClass: core.class.SpaceType, - 'attributes.descriptor': products.spaceTypeDescriptor.ProductType - }, - { - $set: { - objectClass: documents.class.DocumentSpaceType, - 'attributes.projects': true - } - } - ) -} +import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClient } from '@hcengineering/model' export const productsOperation: MigrateOperation = { - async migrate (client: MigrationClient): Promise { - await migrateProductSpacesMixins(client) - await migrateProductOwner(client) - await tryMigrate(client, productsId, [ - { - state: 'migrateSpaceTypes', - func: migrateSpaceTypes - } - ]) - }, + async migrate (client: MigrationClient): Promise {}, async upgrade (client: MigrationUpgradeClient): Promise {} } diff --git a/models/training/src/migration.ts b/models/training/src/migration.ts index a440dd53db..8b505342e9 100644 --- a/models/training/src/migration.ts +++ b/models/training/src/migration.ts @@ -13,27 +13,13 @@ // limitations under the License. // -import { - tryMigrate, - type MigrateOperation, - type MigrationClient, - type MigrationUpgradeClient -} from '@hcengineering/model' +import { type MigrateOperation, type MigrationClient, type MigrationUpgradeClient } from '@hcengineering/model' import { type Ref, TxOperations, type TypedSpace } from '@hcengineering/core' -import core, { DOMAIN_SPACE } from '@hcengineering/model-core' -import training, { trainingId, type Sequence } from '@hcengineering/training' -import contact, { DOMAIN_CONTACT } from '@hcengineering/model-contact' -import { Person, PersonAccount } from '@hcengineering/contact' +import core from '@hcengineering/model-core' +import training, { type Sequence } from '@hcengineering/training' export const trainingOperation: MigrateOperation = { - async migrate (client: MigrationClient): Promise { - await tryMigrate(client, trainingId, [ - { - state: 'migrateDefaultSpaceMembers', - func: migrateDefaultSpaceMembers - } - ]) - }, + async migrate (client: MigrationClient): Promise {}, async upgrade (client: MigrationUpgradeClient): Promise { const tx = new TxOperations(client, core.account.System) await ensureTypedSpace(tx) @@ -41,41 +27,6 @@ export const trainingOperation: MigrateOperation = { } } -async function migrateDefaultSpaceMembers (client: MigrationClient): Promise { - await client.update( - DOMAIN_SPACE, - { - _id: training.space.Trainings, - autoJoin: false - }, - { - $set: { - autoJoin: true - } - } - ) - - const employees = await client.find(DOMAIN_CONTACT, { - _class: contact.class.Person, - [contact.mixin.Employee]: { $exists: true } - }) - const empAccs = await client.model.findAll(contact.class.PersonAccount, { - person: { $in: employees.map((e) => e._id) } - }) - - await client.update( - DOMAIN_SPACE, - { - _id: training.space.Trainings - }, - { - $addToSet: { - members: { $each: empAccs.map((e) => e._id) } - } - } - ) -} - async function ensureTypedSpace (tx: TxOperations): Promise> { const existing = await tx.findOne(core.class.TypedSpace, { _id: training.space.Trainings