From 2ae1b6d4e012cb64afc1d34bdf57f8c040ca3c2a Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Thu, 17 Oct 2024 00:56:32 +0500 Subject: [PATCH] Recreate all workspace indexes tool (#6946) Signed-off-by: Denis Bykhov --- dev/tool/src/index.ts | 19 +++++++++++++++++-- dev/tool/src/workspace.ts | 11 +---------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/dev/tool/src/index.ts b/dev/tool/src/index.ts index fab8aa4cc2..e470b9a64e 100644 --- a/dev/tool/src/index.ts +++ b/dev/tool/src/index.ts @@ -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 diff --git a/dev/tool/src/workspace.ts b/dev/tool/src/workspace.ts index 03a33c3bd2..b6ebc30618 100644 --- a/dev/tool/src/workspace.ts +++ b/dev/tool/src/workspace.ts @@ -98,24 +98,15 @@ export async function updateField ( } } -export async function recreateElastic ( - mongoUrl: string, - workspaceId: WorkspaceId, - transactorUrl: string -): Promise { +export async function recreateElastic (mongoUrl: string, workspaceId: WorkspaceId): Promise { 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() } }