Files
payload/packages/drizzle/src/migrate.ts
Sasha 0e5bda9a74 feat: make req partial and optional in DB / Local API operations (#9935)
### What?
Previously, the `req` argument:
In database operations (e.g `payload.db`) was required and you needed to
pass the whole `req` with all the properties. This is confusing because
in database operations we never use its properties outside of
`req.transactionID` and `req.t`, both of which should be optional as
well.

Now, you don't have to do that cast:
```ts
payload.db.findOne({
  collection: 'posts',
  req: {} as PayloadRequest,
  where: {
    id: {
      equals: 1,
    },
  },
})
```
Becomes:
```ts
payload.db.findOne({
  collection: 'posts',
  where: {
    id: {
      equals: 1,
    },
  },
})
```

If you need to use transactions, you're not required to do the `as` cast
as well now, as the `req` not only optional but also partial -
`Partial<PayloadRequest>`.
`initTransaction`, `commitTransaction`, `killTransaction` utilities are
typed better now as well. They do not require to you pass all the
properties of `req`, but only `payload` -
`MarkRequired<Partial<PayloadRequest>, 'payload'>`
```ts
const req = { payload }
await initTransaction(req)
await payload.db.create({
  collection: "posts",
  data: {},
  req
})
await commitTransaction(req)
```

The same for the Local API. Internal operations (for example
`packages/payload/src/collections/operations/find.ts`) still accept the
whole `req`, but local ones
(`packages/payload/src/collections/operations/local/find.ts`) which are
used through `payload.` now accept `Partial<PayloadRequest>`, as they
pass it through to internal operations with `createLocalReq`.

So now, this is also valid, while previously you had to do `as` cast for
`req`.
```ts
const req = { payload }
await initTransaction(req)
await payload.create({
  collection: "posts",
  data: {},
  req
})
await commitTransaction(req)
```


Marked as deprecated `PayloadRequest['transactionIDPromise']` to remove
in the next major version. It was never used anywhere.
Refactored `withSession` that returns an object to `getSession` that
returns just `ClientSession`. Better type safety for arguments
Deduplicated in all drizzle operations to `getTransaction(this, req)`
utility:
```ts
const db = this.sessions[await req?.transactionID]?.db || this.drizzle
```
Added fallback for throwing unique validation errors in database
operations when `req.t` is not available.

In migration `up` and `down` functions our `req` is not partial, while
we used to passed `req` with only 2 properties - `payload` and
`transactionID`. This is misleading and you can't access for example
`req.t`.
Now, to achieve "real" full `req` - we generate it with `createLocalReq`
in all migration functions.

This all is backwards compatible. In all public API places where you
expect the full `req` (like hooks) you still have it.

### Why?
Better DX, more expected types, less errors because of types casting.
2024-12-18 22:43:37 -05:00

116 lines
3.0 KiB
TypeScript

import type { Payload } from 'payload'
import {
commitTransaction,
createLocalReq,
initTransaction,
killTransaction,
readMigrationFiles,
} from 'payload'
import prompts from 'prompts'
import type { DrizzleAdapter, Migration } from './types.js'
import { getTransaction } from './utilities/getTransaction.js'
import { migrationTableExists } from './utilities/migrationTableExists.js'
import { parseError } from './utilities/parseError.js'
export const migrate: DrizzleAdapter['migrate'] = async function migrate(
this: DrizzleAdapter,
args,
): Promise<void> {
const { payload } = this
const migrationFiles = args?.migrations || (await readMigrationFiles({ payload }))
if (!migrationFiles.length) {
payload.logger.info({ msg: 'No migrations to run.' })
return
}
if ('createExtensions' in this && typeof this.createExtensions === 'function') {
await this.createExtensions()
}
let latestBatch = 0
let migrationsInDB = []
const hasMigrationTable = await migrationTableExists(this)
if (hasMigrationTable) {
;({ docs: migrationsInDB } = await payload.find({
collection: 'payload-migrations',
limit: 0,
sort: '-name',
}))
if (Number(migrationsInDB?.[0]?.batch) > 0) {
latestBatch = Number(migrationsInDB[0]?.batch)
}
}
if (migrationsInDB.find((m) => m.batch === -1)) {
const { confirm: runMigrations } = await prompts(
{
name: 'confirm',
type: 'confirm',
initial: false,
message:
"It looks like you've run Payload in dev mode, meaning you've dynamically pushed changes to your database.\n\n" +
"If you'd like to run migrations, data loss will occur. Would you like to proceed?",
},
{
onCancel: () => {
process.exit(0)
},
},
)
if (!runMigrations) {
process.exit(0)
}
}
const newBatch = latestBatch + 1
// Execute 'up' function for each migration sequentially
for (const migration of migrationFiles) {
const alreadyRan = migrationsInDB.find((existing) => existing.name === migration.name)
// If already ran, skip
if (alreadyRan) {
continue
}
await runMigrationFile(payload, migration, newBatch)
}
}
async function runMigrationFile(payload: Payload, migration: Migration, batch: number) {
const start = Date.now()
const req = await createLocalReq({}, payload)
payload.logger.info({ msg: `Migrating: ${migration.name}` })
try {
await initTransaction(req)
const db = await getTransaction(payload.db as DrizzleAdapter, req)
await migration.up({ db, payload, req })
payload.logger.info({ msg: `Migrated: ${migration.name} (${Date.now() - start}ms)` })
await payload.create({
collection: 'payload-migrations',
data: {
name: migration.name,
batch,
},
req,
})
await commitTransaction(req)
} catch (err: unknown) {
await killTransaction(req)
payload.logger.error({
err,
msg: parseError(err, `Error running migration ${migration.name}`),
})
process.exit(1)
}
}