Files
payload/test/helpers/reset.ts
Dan Ribbens e8f2ca484e feat(db-postgres): configurable custom schema to use (#5047)
* feat(db-postgres): configurable custom schema to use

* test(db-postgres): use public schema

* chore(db-postgres): simplify drop schema

* chore: add postgres-custom-schema test to ci

* chore: add custom schema to postgres ci

* chore(db-postgres): custom schema in migrate

* chore: ci postgres wait condition
2024-02-23 12:48:06 -05:00

37 lines
1.2 KiB
TypeScript

import { sql } from 'drizzle-orm'
import type { PostgresAdapter } from '../../packages/db-postgres/src/types'
import type { Payload } from '../../packages/payload/src'
import { isMongoose } from './isMongoose'
export async function resetDB(_payload: Payload, collectionSlugs: string[]) {
if (isMongoose(_payload)) {
await _payload.db.collections[collectionSlugs[0]].db.dropDatabase()
} else {
const db: PostgresAdapter = _payload.db as unknown as PostgresAdapter
// Alternative to: await db.drizzle.execute(sql`drop schema public cascade; create schema public;`)
// Deleting the schema causes issues when restoring the database from a snapshot later on. That's why we only delete the table data here,
// To avoid having to re-create any table schemas / indexes / whatever
const schema = db.drizzle._.schema
if (!schema) {
return
}
const queries = Object.values(schema).map((table: any) => {
return sql.raw(`DELETE FROM ${db.schemaName ? db.schemaName + '.' : ''}${table.dbName}`)
})
await db.drizzle.transaction(async (trx) => {
await Promise.all(
queries.map(async (query) => {
if (query) {
await trx.execute(query)
}
}),
)
})
}
}