I noticed a few issues when running e2e tests that will be resolved by this PR: - Most important: for some test suites (fields, fields-relationship, versions, queues, lexical), the database was cleared and seeded **twice** in between each test run. This is because the onInit function was running the clear and seed script, when it should only have been running the seed script. Clearing the database / the snapshot workflow is being done by the reInit endpoint, which then calls onInit to seed the actual data. - The slowest part of `clearAndSeedEverything` is recreating indexes on mongodb. This PR slightly improves performance here by: - Skipping this process for the built-in `['payload-migrations', 'payload-preferences', 'payload-locked-documents']` collections - Previously we were calling both `createIndexes` and `ensureIndexes`. This was unnecessary - `ensureIndexes` is a deprecated alias of `createIndexes`. This PR changes it to only call `createIndexes` - Makes the reinit endpoint accept GET requests instead of POST requests - this makes it easier to debug right in the browser - Some typescript fixes - Adds a `dev:memorydb` script to the package.json. For some reason, `dev` is super unreliable on mongodb locally when running e2e tests - it frequently fails during index creation. Using the memorydb fixes this issue, with the bonus of more closely resembling the CI environment - Previously, you were unable to run test suites using turbopack + postgres. This fixes it, by explicitly installing `pg` as devDependency in our monorepo - Fixes jest open handles warning
38 lines
1.3 KiB
TypeScript
38 lines
1.3 KiB
TypeScript
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
|
|
import type { Payload } from 'payload'
|
|
|
|
import { isMongoose } from './isMongoose.js'
|
|
|
|
export async function resetDB(_payload: Payload, collectionSlugs: string[]) {
|
|
if (isMongoose(_payload) && 'collections' in _payload.db && collectionSlugs.length > 0) {
|
|
const firstCollectionSlug = collectionSlugs?.[0]
|
|
|
|
if (!firstCollectionSlug?.length) {
|
|
throw new Error('No collection slugs provided to reset the database.')
|
|
}
|
|
await _payload.db.collections[firstCollectionSlug]?.db.dropDatabase()
|
|
} else if ('drizzle' in _payload.db) {
|
|
const db = _payload.db as unknown as DrizzleAdapter
|
|
|
|
// 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 `DELETE FROM ${db.schemaName ? db.schemaName + '.' : ''}${table.dbName};`
|
|
})
|
|
.join('')
|
|
|
|
await db.execute({
|
|
drizzle: db.drizzle,
|
|
raw: queries,
|
|
})
|
|
}
|
|
}
|