mirror of
https://github.com/hcengineering/platform.git
synced 2025-04-23 00:37:47 +00:00
Allow to dump workspace (#403)
Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
parent
44427c8c45
commit
58b286ca52
@ -21,7 +21,7 @@ import { createContributingClient } from '@anticrm/contrib'
|
|||||||
import core, { TxOperations } from '@anticrm/core'
|
import core, { TxOperations } from '@anticrm/core'
|
||||||
import { encode } from 'jwt-simple'
|
import { encode } from 'jwt-simple'
|
||||||
import { Client } from 'minio'
|
import { Client } from 'minio'
|
||||||
import { initWorkspace, upgradeWorkspace } from './workspace'
|
import { initWorkspace, upgradeWorkspace, dumpWorkspace } from './workspace'
|
||||||
|
|
||||||
import contact, { combineName } from '@anticrm/contact'
|
import contact, { combineName } from '@anticrm/contact'
|
||||||
|
|
||||||
@ -173,4 +173,11 @@ program
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
program
|
||||||
|
.command('dump-workspace <name> <fileName>')
|
||||||
|
.description('dump workspace transactions and minio resources')
|
||||||
|
.action(async (workspace, fileName, cmd) => {
|
||||||
|
return await dumpWorkspace(mongodbUri, workspace, fileName, minio)
|
||||||
|
})
|
||||||
|
|
||||||
program.parse(process.argv)
|
program.parse(process.argv)
|
||||||
|
@ -18,10 +18,13 @@ import { MongoClient, Document } from 'mongodb'
|
|||||||
import core, { DOMAIN_TX, Tx } from '@anticrm/core'
|
import core, { DOMAIN_TX, Tx } from '@anticrm/core'
|
||||||
import { createContributingClient } from '@anticrm/contrib'
|
import { createContributingClient } from '@anticrm/contrib'
|
||||||
import { encode } from 'jwt-simple'
|
import { encode } from 'jwt-simple'
|
||||||
import { Client } from 'minio'
|
import { BucketItem, Client } from 'minio'
|
||||||
import contact from '@anticrm/contact'
|
import contact from '@anticrm/contact'
|
||||||
|
|
||||||
import * as txJson from './model.tx.json'
|
import * as txJson from './model.tx.json'
|
||||||
|
import { existsSync } from 'fs'
|
||||||
|
import { mkdir, writeFile } from 'fs/promises'
|
||||||
|
import { join } from 'path'
|
||||||
|
|
||||||
const txes = (txJson as any).default as Tx[]
|
const txes = (txJson as any).default as Tx[]
|
||||||
|
|
||||||
@ -85,3 +88,49 @@ export async function upgradeWorkspace (mongoUrl: string, dbName: string, client
|
|||||||
await client.close()
|
await client.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
export async function dumpWorkspace (mongoUrl: string, dbName: string, fileName: string, minio: Client): Promise<void> {
|
||||||
|
const client = new MongoClient(mongoUrl)
|
||||||
|
try {
|
||||||
|
await client.connect()
|
||||||
|
const db = client.db(dbName)
|
||||||
|
|
||||||
|
console.log('dumping transactions...')
|
||||||
|
|
||||||
|
const dbTxes = await db.collection<Tx>(DOMAIN_TX).find().toArray()
|
||||||
|
await writeFile(fileName + '.tx.json', JSON.stringify(dbTxes, undefined, 2))
|
||||||
|
|
||||||
|
console.log('Dump minio objects')
|
||||||
|
if (await minio.bucketExists(dbName)) {
|
||||||
|
const minioData: BucketItem[] = []
|
||||||
|
const list = await minio.listObjects(dbName, undefined, true)
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
list.on('data', data => {
|
||||||
|
minioData.push(data)
|
||||||
|
})
|
||||||
|
list.on('end', () => {
|
||||||
|
resolve(null)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
const minioDbLocation = dbName + '.minio'
|
||||||
|
if (!existsSync(minioDbLocation)) {
|
||||||
|
await mkdir(minioDbLocation)
|
||||||
|
}
|
||||||
|
await writeFile(fileName + '.minio.json', JSON.stringify(minioData, undefined, 2))
|
||||||
|
for (const d of minioData) {
|
||||||
|
const data = await minio.getObject(dbName, d.name)
|
||||||
|
const allData = []
|
||||||
|
let chunk
|
||||||
|
while ((chunk = data.read()) !== null) {
|
||||||
|
allData.push(chunk)
|
||||||
|
}
|
||||||
|
await writeFile(join(minioDbLocation, d.name), allData.join(''))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
await client.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user