mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-30 04:05:40 +00:00
162 lines
4.9 KiB
TypeScript
162 lines
4.9 KiB
TypeScript
//
|
|
// Copyright © 2020, 2021 Anticrm Platform Contributors.
|
|
//
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License. You may
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
//
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
import core, {
|
|
AttachedDoc,
|
|
DOMAIN_BLOB,
|
|
DOMAIN_DOC_INDEX_STATE,
|
|
DOMAIN_MODEL,
|
|
DOMAIN_TX,
|
|
Doc,
|
|
TxCollectionCUD,
|
|
TxCreateDoc
|
|
} from '@hcengineering/core'
|
|
import { MigrateOperation, MigrationClient, MigrationUpgradeClient } from '@hcengineering/model'
|
|
|
|
async function fillCreatedBy (client: MigrationClient): Promise<void> {
|
|
const h = client.hierarchy
|
|
const domains = h.domains()
|
|
for (const domain of domains) {
|
|
if (
|
|
domain === DOMAIN_TX ||
|
|
domain === DOMAIN_MODEL ||
|
|
domain === DOMAIN_BLOB ||
|
|
domain === DOMAIN_DOC_INDEX_STATE
|
|
) {
|
|
continue
|
|
}
|
|
while (true) {
|
|
try {
|
|
const objects = await client.find<Doc>(
|
|
domain,
|
|
{ createdBy: { $exists: false } },
|
|
{ projection: { _id: 1, modifiedBy: 1 }, limit: 10000 }
|
|
)
|
|
if (objects.length === 0) {
|
|
break
|
|
}
|
|
const txes = await client.find<TxCreateDoc<Doc>>(
|
|
DOMAIN_TX,
|
|
{
|
|
_class: core.class.TxCreateDoc,
|
|
objectId: { $in: Array.from(objects.map((it) => it._id)) }
|
|
},
|
|
{ projection: { _id: 1, modifiedBy: 1, createdBy: 1, objectId: 1 } }
|
|
)
|
|
|
|
const txes2 = (
|
|
await client.find<TxCollectionCUD<Doc, AttachedDoc>>(
|
|
DOMAIN_TX,
|
|
{
|
|
_class: core.class.TxCollectionCUD,
|
|
'tx._class': core.class.TxCreateDoc,
|
|
'tx.objectId': { $in: Array.from(objects.map((it) => it._id)) }
|
|
},
|
|
{ projection: { _id: 1, modifiedBy: 1, createdBy: 1, tx: 1 } }
|
|
)
|
|
).map((it) => it.tx as unknown as TxCreateDoc<Doc>)
|
|
|
|
const txMap = new Map(txes.concat(txes2).map((p) => [p.objectId, p]))
|
|
|
|
console.log('migrateCreateBy', domain, objects.length)
|
|
await client.bulk(
|
|
domain,
|
|
objects.map((it) => {
|
|
const createTx = txMap.get(it._id)
|
|
return {
|
|
filter: { _id: it._id },
|
|
update: {
|
|
createdBy: createTx?.modifiedBy ?? it.modifiedBy
|
|
}
|
|
}
|
|
})
|
|
)
|
|
} catch (err) {}
|
|
}
|
|
}
|
|
}
|
|
async function fillCreatedOn (client: MigrationClient): Promise<void> {
|
|
const h = client.hierarchy
|
|
const domains = h.domains()
|
|
for (const domain of domains) {
|
|
if (
|
|
domain === DOMAIN_TX ||
|
|
domain === DOMAIN_MODEL ||
|
|
domain === DOMAIN_BLOB ||
|
|
domain === DOMAIN_DOC_INDEX_STATE
|
|
) {
|
|
continue
|
|
}
|
|
|
|
await client.update<Doc>(domain, { createOn: { $exists: true } }, { $unset: { createOn: 1 } })
|
|
|
|
while (true) {
|
|
try {
|
|
const objects = await client.find<Doc>(
|
|
domain,
|
|
{ createdOn: { $exists: false } },
|
|
{ projection: { _id: 1, modifiedOn: 1 }, limit: 10000 }
|
|
)
|
|
if (objects.length === 0) {
|
|
break
|
|
}
|
|
const txes = await client.find<TxCreateDoc<Doc>>(
|
|
DOMAIN_TX,
|
|
{
|
|
_class: core.class.TxCreateDoc,
|
|
objectId: { $in: Array.from(objects.map((it) => it._id)) }
|
|
},
|
|
{ projection: { _id: 1, modifiedOn: 1, createdOn: 1, objectId: 1 } }
|
|
)
|
|
|
|
const txes2 = (
|
|
await client.find<TxCollectionCUD<Doc, AttachedDoc>>(
|
|
DOMAIN_TX,
|
|
{
|
|
_class: core.class.TxCollectionCUD,
|
|
'tx._class': core.class.TxCreateDoc,
|
|
'tx.objectId': { $in: Array.from(objects.map((it) => it._id)) }
|
|
},
|
|
{ projection: { _id: 1, modifiedOn: 1, createdOn: 1, tx: 1 } }
|
|
)
|
|
).map((it) => it.tx as unknown as TxCreateDoc<Doc>)
|
|
|
|
const txMap = new Map(txes.concat(txes2).map((p) => [p.objectId, p]))
|
|
|
|
console.log('migratecreatedOn', domain, objects.length)
|
|
await client.bulk(
|
|
domain,
|
|
objects.map((it) => {
|
|
const createTx = txMap.get(it._id)
|
|
return {
|
|
filter: { _id: it._id },
|
|
update: {
|
|
createdOn: createTx?.createdOn ?? it.modifiedOn
|
|
}
|
|
}
|
|
})
|
|
)
|
|
} catch (err) {}
|
|
}
|
|
}
|
|
}
|
|
export const coreOperation: MigrateOperation = {
|
|
async migrate (client: MigrationClient): Promise<void> {
|
|
await fillCreatedBy(client)
|
|
await fillCreatedOn(client)
|
|
},
|
|
async upgrade (client: MigrationUpgradeClient): Promise<void> {}
|
|
}
|