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
63 lines
1.4 KiB
TypeScript
63 lines
1.4 KiB
TypeScript
import * as qs from 'qs-esm'
|
|
|
|
export const path = '/re-initialize'
|
|
|
|
export const reInitializeDB = async ({
|
|
serverURL,
|
|
snapshotKey,
|
|
uploadsDir,
|
|
deleteOnly,
|
|
}: {
|
|
deleteOnly?: boolean
|
|
serverURL: string
|
|
snapshotKey: string
|
|
uploadsDir?: string | string[]
|
|
}) => {
|
|
const maxAttempts = 50
|
|
let attempt = 1
|
|
const startTime = Date.now()
|
|
|
|
while (attempt <= maxAttempts) {
|
|
try {
|
|
console.log(`Attempting to reinitialize DB (attempt ${attempt}/${maxAttempts})...`)
|
|
|
|
const queryParams = qs.stringify(
|
|
{
|
|
snapshotKey,
|
|
uploadsDir,
|
|
deleteOnly,
|
|
},
|
|
{
|
|
addQueryPrefix: true,
|
|
},
|
|
)
|
|
|
|
const response = await fetch(`${serverURL}/api${path}${queryParams}`, {
|
|
method: 'get',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`HTTP error! status: ${response.status}`)
|
|
}
|
|
|
|
const timeTaken = Date.now() - startTime
|
|
console.log(`Successfully reinitialized DB (took ${timeTaken}ms)`)
|
|
return
|
|
} catch (error) {
|
|
console.error(`Failed to reinitialize DB`, error)
|
|
|
|
if (attempt === maxAttempts) {
|
|
console.error('Max retry attempts reached. Giving up.')
|
|
throw error
|
|
}
|
|
|
|
console.log('Retrying in 3 seconds...')
|
|
await new Promise((resolve) => setTimeout(resolve, 3000))
|
|
attempt++
|
|
}
|
|
}
|
|
}
|