Removes PayloadRequestWithData in favour of just PayloadRequest with
optional types for `data` and `locale`
`addDataAndFileToRequest` and `addLocalesToRequestFromData` now takes in
a single argument instead of an object
```ts
// before
await addDataAndFileToRequest({ request: req })
addLocalesToRequestFromData({ request: req })
// current
await addDataAndFileToRequest(req)
addLocalesToRequestFromData(req)
```
---------
Co-authored-by: Paul Popus <paul@nouance.io>
74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
/* eslint-disable no-restricted-syntax, no-await-in-loop */
|
|
import type { PayloadRequest } from 'payload'
|
|
|
|
import {
|
|
commitTransaction,
|
|
getMigrations,
|
|
initTransaction,
|
|
killTransaction,
|
|
readMigrationFiles,
|
|
} from 'payload'
|
|
|
|
import type { PostgresAdapter } from './types.js'
|
|
|
|
import { migrationTableExists } from './utilities/migrationTableExists.js'
|
|
import { parseError } from './utilities/parseError.js'
|
|
|
|
export async function migrateDown(this: PostgresAdapter): Promise<void> {
|
|
const { payload } = this
|
|
const migrationFiles = await readMigrationFiles({ payload })
|
|
|
|
const { existingMigrations, latestBatch } = await getMigrations({
|
|
payload,
|
|
})
|
|
|
|
if (!existingMigrations?.length) {
|
|
payload.logger.info({ msg: 'No migrations to rollback.' })
|
|
return
|
|
}
|
|
|
|
payload.logger.info({
|
|
msg: `Rolling back batch ${latestBatch} consisting of ${existingMigrations.length} migration(s).`,
|
|
})
|
|
|
|
const latestBatchMigrations = existingMigrations.filter(({ batch }) => batch === latestBatch)
|
|
|
|
for (const migration of latestBatchMigrations) {
|
|
const migrationFile = migrationFiles.find((m) => m.name === migration.name)
|
|
if (!migrationFile) {
|
|
throw new Error(`Migration ${migration.name} not found locally.`)
|
|
}
|
|
|
|
const start = Date.now()
|
|
const req = { payload } as PayloadRequest
|
|
|
|
try {
|
|
payload.logger.info({ msg: `Migrating down: ${migrationFile.name}` })
|
|
await initTransaction(req)
|
|
await migrationFile.down({ payload, req })
|
|
payload.logger.info({
|
|
msg: `Migrated down: ${migrationFile.name} (${Date.now() - start}ms)`,
|
|
})
|
|
|
|
const tableExists = await migrationTableExists(this.drizzle)
|
|
if (tableExists) {
|
|
await payload.delete({
|
|
id: migration.id,
|
|
collection: 'payload-migrations',
|
|
req,
|
|
})
|
|
}
|
|
|
|
await commitTransaction(req)
|
|
} catch (err: unknown) {
|
|
await killTransaction(req)
|
|
|
|
payload.logger.error({
|
|
err,
|
|
msg: parseError(err, `Error migrating down ${migrationFile.name}. Rolling back.`),
|
|
})
|
|
process.exit(1)
|
|
}
|
|
}
|
|
}
|