Files
payloadcms/test/helpers/reset.ts
Alessio Gravili 17f7b94555 chore: improve test suites, upgrade jest and playwright, add debug utilities for lexical (#4011)
* feat(richtext-lexical): 'bottom' position value for plugins

* feat: TestRecorderFeature

* chore: restructuring to seed and clear db before each test

* chore: make sure all tests pass

* chore: make sure indexes are created in seed.ts - this fixes one erroring test

* chore: speed up test runs through db snapshots

* chore: support drizzle when resetting db

* chore: simplify seeding process, by moving boilerplate db reset / snapshot logic into a wrapper function

* chore: add new seeding process to admin test suite

* chore(deps): upgrade jest and playwright

* chore: make sure mongoose-specific tests are not skipped

* chore: fix point test, which was depending on another test (that's bad!)

* chore: fix incorrect import

* chore: remove unnecessary comments

* chore: clearly label lexicalE2E test file as todo

* chore: simplify seed logic

* chore: move versions test suite to new seed system
2023-11-06 16:38:40 +01: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 ${table.dbName}`)
})
await db.drizzle.transaction(async (trx) => {
await Promise.all(
queries.map(async (query) => {
if (query) {
await trx.execute(query)
}
}),
)
})
}
}