Allow to dump workspace (#403)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2021-11-29 18:41:36 +07:00 committed by GitHub
parent 44427c8c45
commit 58b286ca52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 2 deletions

View File

@ -21,7 +21,7 @@ import { createContributingClient } from '@anticrm/contrib'
import core, { TxOperations } from '@anticrm/core'
import { encode } from 'jwt-simple'
import { Client } from 'minio'
import { initWorkspace, upgradeWorkspace } from './workspace'
import { initWorkspace, upgradeWorkspace, dumpWorkspace } from './workspace'
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)

View File

@ -18,10 +18,13 @@ import { MongoClient, Document } from 'mongodb'
import core, { DOMAIN_TX, Tx } from '@anticrm/core'
import { createContributingClient } from '@anticrm/contrib'
import { encode } from 'jwt-simple'
import { Client } from 'minio'
import { BucketItem, Client } from 'minio'
import contact from '@anticrm/contact'
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[]
@ -85,3 +88,49 @@ export async function upgradeWorkspace (mongoUrl: string, dbName: string, client
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()
}
}