Force move tool (#7095)

This commit is contained in:
Denis Bykhov 2024-11-04 17:33:44 +05:00 committed by GitHub
parent 753661a7e8
commit 0647206938
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 5 deletions

View File

@ -68,7 +68,8 @@ async function moveWorkspace (
pgClient: postgres.Sql, pgClient: postgres.Sql,
ws: Workspace, ws: Workspace,
region: string, region: string,
include?: Set<string> include?: Set<string>,
force = false
): Promise<void> { ): Promise<void> {
try { try {
const wsId = getWorkspaceId(ws.workspace) const wsId = getWorkspaceId(ws.workspace)
@ -102,12 +103,24 @@ async function moveWorkspace (
insertFields.push(field) insertFields.push(field)
} }
while (true) { while (true) {
const toRemove: string[] = []
while (docs.length < 50000) { while (docs.length < 50000) {
const doc = (await cursor.next()) as Doc | null const doc = (await cursor.next()) as Doc | null
if (doc === null) break if (doc === null) break
if (currentIds.has(doc._id)) continue if (currentIds.has(doc._id)) {
if (force) {
toRemove.push(doc._id)
} else {
continue
}
}
docs.push(doc) docs.push(doc)
} }
if (toRemove.length > 0) {
await retryTxn(pgClient, async (client) => {
await client`DELETE FROM ${client(translateDomain(domain))} WHERE "workspaceId" = ${ws.workspace} AND _id IN (${client(toRemove)})`
})
}
if (docs.length === 0) break if (docs.length === 0) break
while (docs.length > 0) { while (docs.length > 0) {
const part = docs.splice(0, 500) const part = docs.splice(0, 500)
@ -138,7 +151,8 @@ export async function moveWorkspaceFromMongoToPG (
dbUrl: string | undefined, dbUrl: string | undefined,
ws: Workspace, ws: Workspace,
region: string, region: string,
include?: Set<string> include?: Set<string>,
force?: boolean
): Promise<void> { ): Promise<void> {
if (dbUrl === undefined) { if (dbUrl === undefined) {
throw new Error('dbUrl is required') throw new Error('dbUrl is required')
@ -148,7 +162,7 @@ export async function moveWorkspaceFromMongoToPG (
const pg = getDBClient(dbUrl) const pg = getDBClient(dbUrl)
const pgClient = await pg.getClient() const pgClient = await pg.getClient()
await moveWorkspace(accountDb, mongo, pgClient, ws, region, include) await moveWorkspace(accountDb, mongo, pgClient, ws, region, include, force)
pg.close() pg.close()
client.close() client.close()
} }

View File

@ -1669,12 +1669,14 @@ export function devTool (
program program
.command('move-workspace-to-pg <workspace> <region>') .command('move-workspace-to-pg <workspace> <region>')
.option('-i, --include <include>', 'A list of ; separated domain names to include during backup', '*') .option('-i, --include <include>', 'A list of ; separated domain names to include during backup', '*')
.option('-f|--force [force]', 'Force update', false)
.action( .action(
async ( async (
workspace: string, workspace: string,
region: string, region: string,
cmd: { cmd: {
include: string include: string
force: boolean
} }
) => { ) => {
const { dbUrl } = prepareTools() const { dbUrl } = prepareTools()
@ -1694,7 +1696,8 @@ export function devTool (
dbUrl, dbUrl,
workspaceInfo, workspaceInfo,
region, region,
cmd.include === '*' ? undefined : new Set(cmd.include.split(';').map((it) => it.trim())) cmd.include === '*' ? undefined : new Set(cmd.include.split(';').map((it) => it.trim())),
cmd.force
) )
}) })
} }