Skip to content

Commit

Permalink
fix: upgrade pg snapshot during v3 upgrade if needed (#9837)
Browse files Browse the repository at this point in the history
### What?

Upgrade PG snapshot if needed during v3 migration

### Why?

I'm unable to run the v3 migration as my PG snapshot is on version 5
instead the required 7

### How?

Copy code from typical migrations to upgrade PG snapshot as needed:
https://github.com/payloadcms/payload/blob/main/packages/drizzle/src/postgres/createMigration.ts#L64

Co-authored-by: Jayce Pulsipher <jpulsipher@nav.com>
  • Loading branch information
jaycetde and Jayce Pulsipher authored Dec 9, 2024
1 parent e095222 commit 1e5364f
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/db-postgres/src/predefinedMigrations/v2-v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const migratePostgresV2toV3 = async ({ debug, payload, req }: Args) => {
const dir = payload.db.migrationDir

// get the drizzle migrateUpSQL from drizzle using the last schema
const { generateDrizzleJson, generateMigration } = require('drizzle-kit/api')
const { generateDrizzleJson, generateMigration, upPgSnapshot } = require('drizzle-kit/api')
const drizzleJsonAfter = generateDrizzleJson(adapter.schema)

// Get the previous migration snapshot
Expand All @@ -59,10 +59,14 @@ export const migratePostgresV2toV3 = async ({ debug, payload, req }: Args) => {
)
}

const drizzleJsonBefore = JSON.parse(
let drizzleJsonBefore = JSON.parse(
fs.readFileSync(`${dir}/${previousSnapshot}`, 'utf8'),
) as DrizzleSnapshotJSON

if (drizzleJsonBefore.version < drizzleJsonAfter.version) {
drizzleJsonBefore = upPgSnapshot(drizzleJsonBefore)
}

const generatedSQL = await generateMigration(drizzleJsonBefore, drizzleJsonAfter)

if (!generatedSQL.length) {
Expand Down

0 comments on commit 1e5364f

Please sign in to comment.