From 1ce18226aba381eebdd723985502095a9eccb050 Mon Sep 17 00:00:00 2001 From: Denis Bykhov Date: Thu, 7 Nov 2024 13:33:04 +0500 Subject: [PATCH] Fix move (#7118) Signed-off-by: Denis Bykhov --- dev/tool/src/db.ts | 20 +++++++++++++------- server/postgres/src/utils.ts | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/dev/tool/src/db.ts b/dev/tool/src/db.ts index a26c4160fb..dde0eba164 100644 --- a/dev/tool/src/db.ts +++ b/dev/tool/src/db.ts @@ -72,6 +72,7 @@ async function moveWorkspace ( force = false ): Promise { try { + console.log('move workspace', ws.workspaceName ?? ws.workspace) const wsId = getWorkspaceId(ws.workspace) const mongoDB = getWorkspaceMongoDB(mongo, wsId) const collections = await mongoDB.collections() @@ -104,7 +105,7 @@ async function moveWorkspace ( } while (true) { const toRemove: string[] = [] - while (docs.length < 50000) { + while (docs.length < 5000) { const doc = (await cursor.next()) as Doc | null if (doc === null) break if (currentIds.has(doc._id)) { @@ -116,25 +117,30 @@ async function moveWorkspace ( } docs.push(doc) } - if (toRemove.length > 0) { + while (toRemove.length > 0) { + const part = toRemove.splice(0, 100) await retryTxn(pgClient, async (client) => { await client.unsafe( - `DELETE FROM ${translateDomain(domain)} WHERE "workspaceId" = '${ws.workspace}' AND _id IN (${toRemove.map((c) => `'${c}'`).join(', ')})` + `DELETE FROM ${translateDomain(domain)} WHERE "workspaceId" = '${ws.workspace}' AND _id IN (${part.map((c) => `'${c}'`).join(', ')})` ) }) } if (docs.length === 0) break while (docs.length > 0) { - const part = docs.splice(0, 500) + const part = docs.splice(0, 100) const values: DBDoc[] = [] for (let i = 0; i < part.length; i++) { const doc = part[i] const d = convertDoc(domain, doc, ws.workspace) values.push(d) } - await retryTxn(pgClient, async (client) => { - await client`INSERT INTO ${client(translateDomain(domain))} ${client(values, insertFields)}` - }) + try { + await retryTxn(pgClient, async (client) => { + await client`INSERT INTO ${client(translateDomain(domain))} ${client(values, insertFields)}` + }) + } catch (err) { + console.log('Error when insert', domain, err) + } } } } diff --git a/server/postgres/src/utils.ts b/server/postgres/src/utils.ts index 47939a2d6d..3ee6566adc 100644 --- a/server/postgres/src/utils.ts +++ b/server/postgres/src/utils.ts @@ -92,7 +92,7 @@ export async function createTables (client: postgres.Sql, domains: string[]): Pr async function getTableSchema (client: postgres.Sql, domain: string): Promise { const res = await client.unsafe(`SELECT column_name, data_type, is_nullable FROM information_schema.columns - WHERE table_name = '${domain}' ORDER BY ordinal_position ASC; + WHERE table_name = '${domain}' and table_schema = 'public' ORDER BY ordinal_position ASC; `) const schema: Schema = {}