Skip to content

Commit cf1001f

Browse files
authored
use truncate in db teardown (medusajs#13875)
* use truncate in db teardown * fix empty table list condition
1 parent 39f2893 commit cf1001f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

packages/medusa-test-utils/src/database.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ export const dbTestUtilFactory = (): any => ({
228228
const runRawQuery = this.pgConnection_.raw.bind(this.pgConnection_)
229229
schema ??= "public"
230230

231-
await runRawQuery(`SET session_replication_role = 'replica';`)
232231
const { rows: tableNames } = await runRawQuery(`SELECT table_name
233232
FROM information_schema.tables
234233
WHERE table_schema = '${schema}';`)
@@ -237,6 +236,7 @@ export const dbTestUtilFactory = (): any => ({
237236
const mainPartitionTables = ["index_data", "index_relation"]
238237
let hasIndexTables = false
239238

239+
const tablesToTruncate: string[] = []
240240
for (const { table_name } of tableNames) {
241241
if (mainPartitionTables.includes(table_name)) {
242242
hasIndexTables = true
@@ -249,15 +249,17 @@ export const dbTestUtilFactory = (): any => ({
249249
continue
250250
}
251251

252-
await runRawQuery(`DELETE FROM ${schema}."${table_name}";`)
252+
tablesToTruncate.push(`${schema}."${table_name}"`)
253+
}
254+
if (tablesToTruncate.length > 0) {
255+
await runRawQuery(`TRUNCATE ${tablesToTruncate.join(", ")};`)
253256
}
254257

255258
if (hasIndexTables) {
256-
await runRawQuery(`TRUNCATE TABLE ${schema}.index_data;`)
257-
await runRawQuery(`TRUNCATE TABLE ${schema}.index_relation;`)
259+
await runRawQuery(
260+
`TRUNCATE ${schema}.index_data, ${schema}.index_relation;`
261+
)
258262
}
259-
260-
await runRawQuery(`SET session_replication_role = 'origin';`)
261263
} catch (error) {
262264
logger.error("Error during database teardown:", error)
263265
throw error

0 commit comments

Comments
 (0)