mirror of
https://github.com/hcengineering/platform.git
synced 2026-09-12 04:37:44 +02:00
uberf-9428: migrate accounts with multiple active services (#8027)
Signed-off-by: Alexey Zinoviev <alexey.zinoviev@xored.com>
This commit is contained in:
@@ -102,6 +102,7 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap
|
||||
const migrations = accountsDb.then(async ([db]) => {
|
||||
if (oldAccsUrl !== undefined) {
|
||||
await migrateFromOldAccounts(oldAccsUrl, db)
|
||||
console.log('Migrations verified/done')
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
@@ -23,18 +23,41 @@ import {
|
||||
import { type AccountDB, createAccount } from '@hcengineering/account'
|
||||
import { getMongoAccountDB } from './utils'
|
||||
import { type Account as OldAccount, type Workspace as OldWorkspace } from './types'
|
||||
import { type MongoAccountDB } from './collections/mongo'
|
||||
|
||||
async function shouldMigrate (oldAccountDb: MongoAccountDB, migrationKey: string): Promise<boolean> {
|
||||
while (true) {
|
||||
const migration = await oldAccountDb.migration.findOne({ key: migrationKey })
|
||||
if (migration?.completed === true) {
|
||||
return false
|
||||
}
|
||||
|
||||
if (migration?.lastProcessedTime === undefined || Date.now() - migration.lastProcessedTime > 1000 * 15) {
|
||||
return true
|
||||
}
|
||||
|
||||
console.log('Migration of accounts database from old accounts is still in progress, waiting...')
|
||||
await new Promise((resolve) => setTimeout(resolve, 5000))
|
||||
}
|
||||
}
|
||||
|
||||
export async function migrateFromOldAccounts (oldAccsUrl: string, accountDB: AccountDB): Promise<void> {
|
||||
const migrationKey = 'migrate-from-old-accounts'
|
||||
// Check if old accounts exist
|
||||
const [oldAccountDb, closeOldDb] = await getMongoAccountDB(oldAccsUrl)
|
||||
let processingHandle
|
||||
|
||||
try {
|
||||
const migration = await oldAccountDb.migration.findOne({ key: migrationKey })
|
||||
if (migration?.completed === true) {
|
||||
if (!(await shouldMigrate(oldAccountDb, migrationKey))) {
|
||||
return
|
||||
}
|
||||
|
||||
await oldAccountDb.migration.insertOne({ key: migrationKey, completed: false, lastProcessedTime: Date.now() })
|
||||
|
||||
processingHandle = setInterval(() => {
|
||||
void oldAccountDb.migration.updateOne({ key: migrationKey }, { lastProcessedTime: Date.now() })
|
||||
}, 1000 * 5)
|
||||
|
||||
// Mapping between <ObjectId, UUID>
|
||||
const accountsIdToUuid: Record<string, PersonUuid> = {}
|
||||
// Mapping between <email, UUID>
|
||||
@@ -131,9 +154,12 @@ export async function migrateFromOldAccounts (oldAccsUrl: string, accountDB: Acc
|
||||
}
|
||||
|
||||
console.log('Total invites processed:', invitesProcessed)
|
||||
await oldAccountDb.migration.insertOne({ key: migrationKey, completed: true })
|
||||
await oldAccountDb.migration.updateOne({ key: migrationKey }, { completed: true })
|
||||
console.log('Migration of accounts database from old accounts COMPLETED')
|
||||
} finally {
|
||||
if (processingHandle !== undefined) {
|
||||
clearTimeout(processingHandle)
|
||||
}
|
||||
closeOldDb()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,6 +174,7 @@ export interface UpgradeStatistic {
|
||||
export interface Migration {
|
||||
key: string
|
||||
completed: boolean
|
||||
lastProcessedTime: number
|
||||
}
|
||||
|
||||
interface Operator<T, P extends keyof T> {
|
||||
|
||||
Reference in New Issue
Block a user