Add DB compact tool (#6125)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2024-07-27 22:03:47 +07:00 committed by Andrey Sobolev
parent 71c2fa0750
commit 9aad4a4561
No known key found for this signature in database
GPG Key ID: BD80F68D68D8F7F2

View File

@ -237,6 +237,42 @@ export function devTool (
})
})
program
.command('compact-db')
.description('compact all db collections')
.option('-w, --workspace <workspace>', 'A selected "workspace" only', '')
.action(async (cmd: { workspace: string }) => {
const { mongodbUri } = prepareTools()
await withDatabase(mongodbUri, async (db, client) => {
console.log('compacting db ...')
let gtotal: number = 0
try {
const workspaces = await listWorkspacesPure(db, productId)
for (const workspace of workspaces) {
if (cmd.workspace !== '' && workspace.workspace !== cmd.workspace) {
continue
}
let total: number = 0
const wsDb = getWorkspaceDB(client, { name: workspace.workspace, productId })
const collections = wsDb.listCollections()
while (true) {
const collInfo = await collections.next()
if (collInfo === null) {
break
}
const result = await wsDb.command({ compact: collInfo.name })
total += result.bytesFreed
}
gtotal += total
console.log('total feed for db', workspace.workspaceName, Math.round(total / (1024 * 1024)))
}
console.log('global total feed', Math.round(gtotal / (1024 * 1024)))
} catch (err: any) {
console.error(err)
}
})
})
program
.command('assign-workspace <email> <workspace>')
.description('assign workspace')