Migrates the `db-mongodb` package to use `strict: true` and `noUncheckedIndexedAccess: true` TSConfig properties. This greatly improves code quality and prevents some runtime errors or gives better error messages.
77 lines
1.9 KiB
TypeScript
77 lines
1.9 KiB
TypeScript
import { commitTransaction, initTransaction, killTransaction, readMigrationFiles } from 'payload'
|
|
import prompts from 'prompts'
|
|
|
|
import type { MongooseAdapter } from './index.js'
|
|
|
|
/**
|
|
* Drop the current database and run all migrate up functions
|
|
*/
|
|
export async function migrateFresh(
|
|
this: MongooseAdapter,
|
|
{ forceAcceptWarning = false }: { forceAcceptWarning?: boolean },
|
|
): Promise<void> {
|
|
const { payload } = this
|
|
|
|
if (!forceAcceptWarning) {
|
|
const { confirm: acceptWarning } = await prompts(
|
|
{
|
|
name: 'confirm',
|
|
type: 'confirm',
|
|
initial: false,
|
|
message: `WARNING: This will drop your database and run all migrations. Are you sure you want to proceed?`,
|
|
},
|
|
{
|
|
onCancel: () => {
|
|
process.exit(0)
|
|
},
|
|
},
|
|
)
|
|
|
|
if (!acceptWarning) {
|
|
process.exit(0)
|
|
}
|
|
}
|
|
|
|
payload.logger.info({
|
|
msg: `Dropping database.`,
|
|
})
|
|
|
|
await this.connection.dropDatabase()
|
|
|
|
const migrationFiles = await readMigrationFiles({ payload })
|
|
payload.logger.debug({
|
|
msg: `Found ${migrationFiles.length} migration files.`,
|
|
})
|
|
|
|
const req = { payload }
|
|
|
|
// Run all migrate up
|
|
for (const migration of migrationFiles) {
|
|
payload.logger.info({ msg: `Migrating: ${migration.name}` })
|
|
try {
|
|
const start = Date.now()
|
|
await initTransaction(req)
|
|
await migration.up({ payload, req })
|
|
await payload.create({
|
|
collection: 'payload-migrations',
|
|
data: {
|
|
name: migration.name,
|
|
batch: 1,
|
|
},
|
|
req,
|
|
})
|
|
|
|
await commitTransaction(req)
|
|
|
|
payload.logger.info({ msg: `Migrated: ${migration.name} (${Date.now() - start}ms)` })
|
|
} catch (err: unknown) {
|
|
await killTransaction(req)
|
|
payload.logger.error({
|
|
err,
|
|
msg: `Error running migration ${migration.name}. Rolling back.`,
|
|
})
|
|
throw err
|
|
}
|
|
}
|
|
}
|