Recreate all workspace indexes tool (#6946)

Signed-off-by: Denis Bykhov <bykhov.denis@gmail.com>
This commit is contained in:
Denis Bykhov 2024-10-17 00:56:32 +05:00 committed by GitHub
parent 78c1938e3e
commit 2ae1b6d4e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 12 deletions

View File

@ -1522,8 +1522,23 @@ export function devTool (
.action(async (workspace: string) => {
const { dbUrl, mongodbUri } = prepareTools()
const wsid = getWorkspaceId(workspace)
const endpoint = await getTransactorEndpoint(generateToken(systemAccountEmail, wsid), 'external')
await recreateElastic(mongodbUri ?? dbUrl, wsid, endpoint)
await recreateElastic(mongodbUri ?? dbUrl, wsid)
})
program
.command('recreate-all-elastic-indexes')
.description('reindex elastic')
.action(async () => {
const { dbUrl, mongodbUri } = prepareTools()
await withDatabase(dbUrl, async (db) => {
const workspaces = await listWorkspacesRaw(db)
workspaces.sort((a, b) => b.lastVisit - a.lastVisit)
for (const workspace of workspaces) {
const wsid = getWorkspaceId(workspace.workspace)
await recreateElastic(mongodbUri ?? dbUrl, wsid)
}
})
})
program

View File

@ -98,24 +98,15 @@ export async function updateField (
}
}
export async function recreateElastic (
mongoUrl: string,
workspaceId: WorkspaceId,
transactorUrl: string
): Promise<void> {
export async function recreateElastic (mongoUrl: string, workspaceId: WorkspaceId): Promise<void> {
const client = getMongoClient(mongoUrl)
const _client = await client.getClient()
const connection = (await connect(transactorUrl, workspaceId, undefined, {
mode: 'backup'
})) as unknown as CoreClient & BackupClient
try {
const db = getWorkspaceMongoDB(_client, workspaceId)
await db
.collection(DOMAIN_DOC_INDEX_STATE)
.updateMany({ _class: core.class.DocIndexState }, { $set: { stages: {}, needIndex: true } })
await connection.sendForceClose()
} finally {
client.close()
await connection.close()
}
}