Skip to content

Commit f310c90

Browse files
authored
fix(db-postgres): down migration fails because migrationTableExists doesn't check in the current transaction (#11910)
Fixes #11882 Previously, down migration that dropped the `payload_migrations` table was failing because `migrationTableExists` doesn't check the current transaction, only in which you can get a `false` value result.
1 parent dc793d1 commit f310c90

File tree

8 files changed

+664
-6
lines changed

8 files changed

+664
-6
lines changed

packages/drizzle/src/migrateDown.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ export async function migrateDown(this: DrizzleAdapter): Promise<void> {
5050
msg: `Migrated down: ${migrationFile.name} (${Date.now() - start}ms)`,
5151
})
5252

53-
const tableExists = await migrationTableExists(this)
53+
const tableExists = await migrationTableExists(this, db)
54+
5455
if (tableExists) {
5556
await payload.delete({
5657
id: migration.id,

packages/drizzle/src/migrateRefresh.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export async function migrateRefresh(this: DrizzleAdapter) {
5454
msg: `Migrated down: ${migration.name} (${Date.now() - start}ms)`,
5555
})
5656

57-
const tableExists = await migrationTableExists(this)
57+
const tableExists = await migrationTableExists(this, db)
5858
if (tableExists) {
5959
await payload.delete({
6060
collection: 'payload-migrations',

packages/drizzle/src/migrateReset.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export async function migrateReset(this: DrizzleAdapter): Promise<void> {
4545
msg: `Migrated down: ${migrationFile.name} (${Date.now() - start}ms)`,
4646
})
4747

48-
const tableExists = await migrationTableExists(this)
48+
const tableExists = await migrationTableExists(this, db)
4949
if (tableExists) {
5050
await payload.delete({
5151
id: migration.id,

packages/drizzle/src/utilities/migrationTableExists.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import type { DrizzleAdapter } from '../types.js'
1+
import type { LibSQLDatabase } from 'drizzle-orm/libsql'
22

3-
export const migrationTableExists = async (adapter: DrizzleAdapter): Promise<boolean> => {
3+
import type { DrizzleAdapter, PostgresDB } from '../types.js'
4+
5+
export const migrationTableExists = async (
6+
adapter: DrizzleAdapter,
7+
db?: LibSQLDatabase | PostgresDB,
8+
): Promise<boolean> => {
49
let statement
510

611
if (adapter.name === 'postgres') {
@@ -20,7 +25,7 @@ export const migrationTableExists = async (adapter: DrizzleAdapter): Promise<boo
2025
}
2126

2227
const result = await adapter.execute({
23-
drizzle: adapter.drizzle,
28+
drizzle: db ?? adapter.drizzle,
2429
raw: statement,
2530
})
2631

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* eslint-disable jest/require-top-level-describe */
2+
import { existsSync, rmdirSync, rmSync } from 'fs'
3+
import path from 'path'
4+
import { buildConfig, getPayload } from 'payload'
5+
import { fileURLToPath } from 'url'
6+
7+
const filename = fileURLToPath(import.meta.url)
8+
const dirname = path.dirname(filename)
9+
10+
const describe =
11+
process.env.PAYLOAD_DATABASE === 'postgres' ? global.describe : global.describe.skip
12+
13+
const clearMigrations = () => {
14+
if (existsSync(path.resolve(dirname, 'migrations'))) {
15+
rmSync(path.resolve(dirname, 'migrations'), { force: true, recursive: true })
16+
}
17+
}
18+
19+
describe('SQL migrations', () => {
20+
// If something fails - an error will be thrown.
21+
// eslint-disable-next-line jest/expect-expect
22+
it('should up and down migration successfully', async () => {
23+
clearMigrations()
24+
25+
const { databaseAdapter } = await import(path.resolve(dirname, '../../databaseAdapter.js'))
26+
27+
const init = databaseAdapter.init
28+
29+
// set options
30+
databaseAdapter.init = ({ payload }) => {
31+
const adapter = init({ payload })
32+
adapter.migrationDir = path.resolve(dirname, 'migrations')
33+
adapter.push = false
34+
return adapter
35+
}
36+
37+
const config = await buildConfig({
38+
db: databaseAdapter,
39+
secret: 'secret',
40+
collections: [
41+
{
42+
slug: 'users',
43+
auth: true,
44+
fields: [],
45+
},
46+
],
47+
})
48+
49+
const payload = await getPayload({ config })
50+
51+
await payload.db.createMigration({ payload })
52+
await payload.db.migrate()
53+
await payload.db.migrateDown()
54+
55+
await payload.db.dropDatabase({ adapter: payload.db as any })
56+
await payload.db.destroy?.()
57+
})
58+
})

0 commit comments

Comments
 (0)