Skip to content

Commit 07be617

Browse files
authored
fix(db-postgres): relationships v2-v3 migration errors when migrating from v2 to stable v3 (#10080)
When migrating from stable v2 to v3, the provided relationships migration fails because of not having `payload_locked_documents` and `payload_locked_documents_rels` tables in the migration generated in v2. ![image](https://github.com/user-attachments/assets/0dc57e6a-5e6e-4b74-bcab-70e660f4e939)
1 parent d8c106c commit 07be617

File tree

8 files changed

+902
-2
lines changed

8 files changed

+902
-2
lines changed

packages/drizzle/src/postgres/predefinedMigrations/v2-v3/index.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,16 @@ export const migratePostgresV2toV3 = async ({ debug, payload, req }: Args) => {
4141

4242
// get the drizzle migrateUpSQL from drizzle using the last schema
4343
const { generateDrizzleJson, generateMigration, upSnapshot } = adapter.requireDrizzleKit()
44-
const drizzleJsonAfter = generateDrizzleJson(adapter.schema) as DrizzleSnapshotJSON
44+
45+
const toSnapshot: Record<string, unknown> = {}
46+
47+
for (const key of Object.keys(adapter.schema).filter(
48+
(key) => !key.startsWith('payload_locked_documents'),
49+
)) {
50+
toSnapshot[key] = adapter.schema[key]
51+
}
52+
53+
const drizzleJsonAfter = generateDrizzleJson(toSnapshot) as DrizzleSnapshotJSON
4554

4655
// Get the previous migration snapshot
4756
const previousSnapshot = fs

test/database/.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
migrations
1+
/migrations
22
*.generated-schema.ts
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* eslint-disable jest/require-top-level-describe */
2+
import path from 'path'
3+
import { buildConfig, getPayload } from 'payload'
4+
import { fileURLToPath } from 'url'
5+
6+
const filename = fileURLToPath(import.meta.url)
7+
const dirname = path.dirname(filename)
8+
9+
const describe =
10+
process.env.PAYLOAD_DATABASE === 'postgres' ? global.describe : global.describe.skip
11+
12+
describe('Postgres relationships v2-v3 migration', () => {
13+
it('should execute relationships v2-v3 migration', async () => {
14+
const { databaseAdapter } = await import(path.resolve(dirname, '../../databaseAdapter.js'))
15+
16+
const init = databaseAdapter.init
17+
18+
// set options
19+
databaseAdapter.init = ({ payload }) => {
20+
const adapter = init({ payload })
21+
adapter.migrationDir = path.resolve(dirname, 'migrations')
22+
adapter.push = false
23+
return adapter
24+
}
25+
26+
const config = await buildConfig({
27+
db: databaseAdapter,
28+
secret: 'secret',
29+
collections: [
30+
{
31+
slug: 'users',
32+
auth: true,
33+
fields: [],
34+
},
35+
],
36+
})
37+
38+
const payload = await getPayload({ config })
39+
40+
let hasErr = false
41+
42+
await payload.db.migrate().catch(() => {
43+
hasErr = true
44+
})
45+
46+
expect(hasErr).toBeFalsy()
47+
48+
await payload.db.dropDatabase({ adapter: payload.db as any })
49+
await payload.db.destroy()
50+
})
51+
})

0 commit comments

Comments
 (0)