fix(db-postgres): #7492 migrate snapshots (#7540)

## Description

Fixes #7492 

In order to run createMigration, we need to read in the previous
snapshot file if one exists. When that snapshot was generated from an
older version of drizzle-kit, we have to first migrate it up match the
latest version for drizzle to generate the new migration. This change
adds in the call to check the version and migrate the snapshot if
needed.
This commit is contained in:
Dan Ribbens
2024-08-07 13:49:08 -04:00
committed by GitHub
parent b7e2c59622
commit 46f70d9df4
4 changed files with 14 additions and 12 deletions

View File

@@ -47,7 +47,7 @@
"dependencies": { "dependencies": {
"@payloadcms/drizzle": "workspace:*", "@payloadcms/drizzle": "workspace:*",
"console-table-printer": "2.11.2", "console-table-printer": "2.11.2",
"drizzle-kit": "0.23.2", "drizzle-kit": "0.23.2-df9e596",
"drizzle-orm": "0.32.1", "drizzle-orm": "0.32.1",
"pg": "8.11.3", "pg": "8.11.3",
"prompts": "2.4.2", "prompts": "2.4.2",

View File

@@ -25,7 +25,7 @@ export const createMigration: CreateMigration = async function createMigration(
if (!fs.existsSync(dir)) { if (!fs.existsSync(dir)) {
fs.mkdirSync(dir) fs.mkdirSync(dir)
} }
const { generateDrizzleJson, generateMigration } = require('drizzle-kit/api') const { generateDrizzleJson, generateMigration, upPgSnapshot } = require('drizzle-kit/api')
const drizzleJsonAfter = generateDrizzleJson(this.schema) const drizzleJsonAfter = generateDrizzleJson(this.schema)
const [yyymmdd, hhmmss] = new Date().toISOString().split('T') const [yyymmdd, hhmmss] = new Date().toISOString().split('T')
const formattedDate = yyymmdd.replace(/\D/g, '') const formattedDate = yyymmdd.replace(/\D/g, '')
@@ -64,9 +64,11 @@ export const createMigration: CreateMigration = async function createMigration(
.reverse()?.[0] .reverse()?.[0]
if (latestSnapshot) { if (latestSnapshot) {
drizzleJsonBefore = JSON.parse( drizzleJsonBefore = JSON.parse(fs.readFileSync(`${dir}/${latestSnapshot}`, 'utf8'))
fs.readFileSync(`${dir}/${latestSnapshot}`, 'utf8'),
) as DrizzleSnapshotJSON if (drizzleJsonBefore.version < drizzleJsonAfter.version) {
drizzleJsonBefore = upPgSnapshot(drizzleJsonBefore)
}
} }
const sqlStatementsUp = await generateMigration(drizzleJsonBefore, drizzleJsonAfter) const sqlStatementsUp = await generateMigration(drizzleJsonBefore, drizzleJsonAfter)

View File

@@ -46,7 +46,7 @@
"@libsql/client": "^0.6.2", "@libsql/client": "^0.6.2",
"@payloadcms/drizzle": "workspace:*", "@payloadcms/drizzle": "workspace:*",
"console-table-printer": "2.11.2", "console-table-printer": "2.11.2",
"drizzle-kit": "0.23.2", "drizzle-kit": "0.23.2-df9e596",
"drizzle-orm": "0.32.1", "drizzle-orm": "0.32.1",
"prompts": "2.4.2", "prompts": "2.4.2",
"to-snake-case": "1.0.0", "to-snake-case": "1.0.0",

12
pnpm-lock.yaml generated
View File

@@ -302,8 +302,8 @@ importers:
specifier: 2.11.2 specifier: 2.11.2
version: 2.11.2 version: 2.11.2
drizzle-kit: drizzle-kit:
specifier: 0.23.2 specifier: 0.23.2-df9e596
version: 0.23.2 version: 0.23.2-df9e596
drizzle-orm: drizzle-orm:
specifier: 0.32.1 specifier: 0.32.1
version: 0.32.1(@libsql/client@0.6.2)(@types/pg@8.10.2)(pg@8.11.3)(react@19.0.0-rc-fb9a90fa48-20240614)(types-react@19.0.0-rc.0) version: 0.32.1(@libsql/client@0.6.2)(@types/pg@8.10.2)(pg@8.11.3)(react@19.0.0-rc-fb9a90fa48-20240614)(types-react@19.0.0-rc.0)
@@ -351,8 +351,8 @@ importers:
specifier: 2.11.2 specifier: 2.11.2
version: 2.11.2 version: 2.11.2
drizzle-kit: drizzle-kit:
specifier: 0.23.2 specifier: 0.23.2-df9e596
version: 0.23.2 version: 0.23.2-df9e596
drizzle-orm: drizzle-orm:
specifier: 0.32.1 specifier: 0.32.1
version: 0.32.1(@libsql/client@0.6.2)(@types/pg@8.10.2)(pg@8.11.3)(react@19.0.0-rc-fb9a90fa48-20240614)(types-react@19.0.0-rc.0) version: 0.32.1(@libsql/client@0.6.2)(@types/pg@8.10.2)(pg@8.11.3)(react@19.0.0-rc-fb9a90fa48-20240614)(types-react@19.0.0-rc.0)
@@ -9063,8 +9063,8 @@ packages:
engines: {node: '>=12'} engines: {node: '>=12'}
dev: true dev: true
/drizzle-kit@0.23.2: /drizzle-kit@0.23.2-df9e596:
resolution: {integrity: sha512-NWkQ7GD2OTbQ7HzcjsaCOf3n0tlFPSEAF38fvDpwDj8jRbGWGFtN2cD8I8wp4lU+5Os/oyP2xycTKGLHdPipUw==} resolution: {integrity: sha512-MOvoeDE1c05XGevXLqhRVu/TNHwueibPZqMUH/kG1v6DOHD5pB2poRpjphr4sjae2QDS6h+PU5pCfzqEa6le6Q==}
hasBin: true hasBin: true
dependencies: dependencies:
'@drizzle-team/brocli': 0.8.2 '@drizzle-team/brocli': 0.8.2