make cli command for changing attribute value via backup mode (#4520)

Signed-off-by: Vyacheslav Tumanov <me@slavatumanov.me>
This commit is contained in:
Vyacheslav Tumanov 2024-02-05 19:45:20 +05:00 committed by GitHub
parent 1b4a574a1c
commit 779d5318c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 57 additions and 2 deletions

View File

@ -46,7 +46,7 @@ import toolPlugin, { FileModelLogger } from '@hcengineering/server-tool'
import { Command, program } from 'commander'
import { Db, MongoClient } from 'mongodb'
import { clearTelegramHistory } from './telegram'
import { diffWorkspace } from './workspace'
import { diffWorkspace, updateField } from './workspace'
import { Data, getWorkspaceId, RateLimitter, Tx, Version } from '@hcengineering/core'
import { MinioService } from '@hcengineering/minio'
@ -628,6 +628,25 @@ export function devTool (
await fixSkills(mongodbUri, getWorkspaceId(workspace, productId), transactorUrl, step)
})
program
.command('change-field <workspace>')
.description('change field value for the object')
.requiredOption('--objectId <objectId>', 'objectId')
.requiredOption('--objectClass <objectClass>')
.requiredOption('--attribute <attribute>')
.requiredOption('--type <type>', 'number | string')
.requiredOption('--value <value>')
.requiredOption('--domain <domain>')
.action(
async (
workspace: string,
cmd: { objectId: string, objectClass: string, type: string, attribute: string, value: string, domain: string }
) => {
const { mongodbUri } = prepareTools()
await updateField(mongodbUri, getWorkspaceId(workspace, productId), transactorUrl, cmd)
}
)
extendProgram?.(program)
program.parse(process.argv)

View File

@ -15,10 +15,19 @@
//
import contact from '@hcengineering/contact'
import core, { DOMAIN_TX, Tx, WorkspaceId } from '@hcengineering/core'
import core, {
Client as CoreClient,
BackupClient,
DOMAIN_TX,
Tx,
WorkspaceId,
type Ref,
type Doc
} from '@hcengineering/core'
import { getWorkspaceDB } from '@hcengineering/mongo'
import { MongoClient } from 'mongodb'
import { generateModelDiff, printDiff } from './mdiff'
import { connect } from '@hcengineering/server-tool'
export async function diffWorkspace (mongoUrl: string, workspace: WorkspaceId, rawTxes: Tx[]): Promise<void> {
const client = new MongoClient(mongoUrl)
@ -61,3 +70,30 @@ export async function diffWorkspace (mongoUrl: string, workspace: WorkspaceId, r
await client.close()
}
}
export async function updateField (
mongoUrl: string,
workspaceId: WorkspaceId,
transactorUrl: string,
cmd: { objectId: string, objectClass: string, type: string, attribute: string, value: string, domain: string }
): Promise<void> {
const connection = (await connect(transactorUrl, workspaceId, undefined, {
mode: 'backup'
})) as unknown as CoreClient & BackupClient
const client = new MongoClient(mongoUrl)
let valueToPut: string | number = cmd.value
if (cmd.type === 'number') valueToPut = parseFloat(valueToPut)
try {
try {
await client.connect()
const db = getWorkspaceDB(client, workspaceId)
await db
.collection(cmd.domain)
.updateOne({ _id: cmd.objectId as Ref<Doc> }, { $set: { [cmd.attribute]: valueToPut } })
} finally {
await client.close()
}
} finally {
await connection.close()
}
}