From cf1001f11d3dd7b1f0ba5570e032e05b464f2e90 Mon Sep 17 00:00:00 2001 From: Pedro Guzman Date: Mon, 27 Oct 2025 18:39:48 +0100 Subject: [PATCH] use truncate in db teardown (#13875) * use truncate in db teardown * fix empty table list condition --- packages/medusa-test-utils/src/database.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/medusa-test-utils/src/database.ts b/packages/medusa-test-utils/src/database.ts index b8e1390987..0e30da98f0 100644 --- a/packages/medusa-test-utils/src/database.ts +++ b/packages/medusa-test-utils/src/database.ts @@ -228,7 +228,6 @@ export const dbTestUtilFactory = (): any => ({ const runRawQuery = this.pgConnection_.raw.bind(this.pgConnection_) schema ??= "public" - await runRawQuery(`SET session_replication_role = 'replica';`) const { rows: tableNames } = await runRawQuery(`SELECT table_name FROM information_schema.tables WHERE table_schema = '${schema}';`) @@ -237,6 +236,7 @@ export const dbTestUtilFactory = (): any => ({ const mainPartitionTables = ["index_data", "index_relation"] let hasIndexTables = false + const tablesToTruncate: string[] = [] for (const { table_name } of tableNames) { if (mainPartitionTables.includes(table_name)) { hasIndexTables = true @@ -249,15 +249,17 @@ export const dbTestUtilFactory = (): any => ({ continue } - await runRawQuery(`DELETE FROM ${schema}."${table_name}";`) + tablesToTruncate.push(`${schema}."${table_name}"`) + } + if (tablesToTruncate.length > 0) { + await runRawQuery(`TRUNCATE ${tablesToTruncate.join(", ")};`) } if (hasIndexTables) { - await runRawQuery(`TRUNCATE TABLE ${schema}.index_data;`) - await runRawQuery(`TRUNCATE TABLE ${schema}.index_relation;`) + await runRawQuery( + `TRUNCATE ${schema}.index_data, ${schema}.index_relation;` + ) } - - await runRawQuery(`SET session_replication_role = 'origin';`) } catch (error) { logger.error("Error during database teardown:", error) throw error