Skip to content

Commit 05f4000

Browse files
fix: localizeStatus PG migration db.execute signature (#16889)
Requires #19071 for clean-cache browser setup on the `3.x` CI runners. ## What Fixes the broken `localizeStatus` migration for PostgreSQL databases. ## Why All `db.execute` calls in the 4 SQL migration files used the wrong signature: ```ts // WRONG - db has no .drizzle sub-property await db.execute({ drizzle: db.drizzle, sql: sql`...` }) ``` The `db` parameter passed into migration functions IS the raw Drizzle `PostgresDB` instance — it has no `.drizzle` sub-property. The correct call pattern (used throughout the rest of the drizzle codebase) is: ```ts // CORRECT await db.execute(sql`...`) ``` Also fixes `inserted.length` → `inserted.rows.length` in `up.ts` (the result object has a `.rows` array, not a top-level length). ## Files changed - `sql/up.ts` - `sql/down.ts` - `sql/migrateMainCollection.ts` - `sql/migrateMainGlobal.ts` These are pure bug fixes with no behaviour changes — the migration logic is identical, only the call signature is corrected. Created from comments on this PR #14667 --------- Co-authored-by: Copilot <[email protected]>
1 parent 12f1d80 commit 05f4000

5 files changed

Lines changed: 167 additions & 272 deletions

File tree

‎packages/payload/src/versions/migrations/localizeStatus/sql/down.ts‎

Lines changed: 75 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -60,140 +60,107 @@ export async function down(args: LocalizeStatusArgs): Promise<void> {
6060
// 1. Restore version__status column to main table
6161
payload.logger.info({ msg: `Restoring version__status column to ${versionsTable}` })
6262

63-
await db.execute({
64-
drizzle: db.drizzle,
65-
sql: sql`
66-
ALTER TABLE ${sql.identifier(versionsTable)} ADD COLUMN version__status VARCHAR
67-
`,
68-
})
63+
await db.execute(sql`
64+
ALTER TABLE ${sql.identifier(versionsTable)} ADD COLUMN version__status VARCHAR
65+
`)
6966

7067
// 2. Copy status from default locale back to main table
7168
payload.logger.info({
7269
msg: `Copying status from default locale (${defaultLocale}) back to main table`,
7370
})
7471

75-
await db.execute({
76-
drizzle: db.drizzle,
77-
sql: sql`
78-
UPDATE ${sql.identifier(versionsTable)} pv
79-
SET version__status = pl.version__status
80-
FROM ${sql.identifier(localesTable)} pl
81-
WHERE pv.id = pl._parent_id
82-
AND pl._locale = ${defaultLocale}
83-
`,
84-
})
72+
await db.execute(sql`
73+
UPDATE ${sql.identifier(versionsTable)} pv
74+
SET version__status = pl.version__status
75+
FROM ${sql.identifier(localesTable)} pl
76+
WHERE pv.id = pl._parent_id
77+
AND pl._locale = ${defaultLocale}
78+
`)
8579

8680
// 3. Check if there are other localized fields besides version__status
87-
const columnCheckResult = await db.execute({
88-
drizzle: db.drizzle,
89-
sql: sql`
90-
SELECT COUNT(*) as count
91-
FROM information_schema.columns
92-
WHERE table_schema = 'public'
93-
AND table_name = ${localesTable}
94-
AND column_name NOT IN ('id', '_locale', '_parent_id', 'version__status')
95-
`,
96-
})
81+
const columnCheckResult = await db.execute(sql`
82+
SELECT COUNT(*) as count
83+
FROM information_schema.columns
84+
WHERE table_schema = 'public'
85+
AND table_name = ${localesTable}
86+
AND column_name NOT IN ('id', '_locale', '_parent_id', 'version__status')
87+
`)
9788

9889
const hasOtherLocalizedFields = Number(columnCheckResult.rows[0]?.count) > 0
9990

10091
if (!hasOtherLocalizedFields) {
10192
// SCENARIO 1 ROLLBACK: No other localized fields, drop entire table
10293
payload.logger.info({ msg: `Dropping entire locales table: ${localesTable}` })
10394

104-
await db.execute({
105-
drizzle: db.drizzle,
106-
sql: sql`DROP TABLE ${sql.identifier(localesTable)} CASCADE`,
107-
})
95+
await db.execute(sql`DROP TABLE ${sql.identifier(localesTable)} CASCADE`)
10896
} else {
10997
// SCENARIO 2 ROLLBACK: Other localized fields exist, just drop version__status column
11098
payload.logger.info({ msg: `Dropping version__status column from ${localesTable}` })
11199

112-
await db.execute({
113-
drizzle: db.drizzle,
114-
sql: sql`
115-
ALTER TABLE ${sql.identifier(localesTable)} DROP COLUMN version__status
116-
`,
117-
})
100+
await db.execute(sql`
101+
ALTER TABLE ${sql.identifier(localesTable)} DROP COLUMN version__status
102+
`)
118103
}
119104

120105
// 4. Restore _status to main collection/global table if it was dropped
121106
const mainTable = collectionSlug ? toSnakeCase(collectionSlug) : toSnakeCase(globalSlug!)
122107
const mainLocalesTable = `${mainTable}_locales`
123108

124109
// Check if _status exists in the main table
125-
const statusInMainTableCheck = await db.execute({
126-
drizzle: db.drizzle,
127-
sql: sql`
128-
SELECT EXISTS (
129-
SELECT FROM information_schema.columns
130-
WHERE table_schema = 'public'
131-
AND table_name = ${mainTable}
132-
AND column_name = '_status'
133-
) as exists
134-
`,
135-
})
110+
const statusInMainTableCheck = await db.execute(sql`
111+
SELECT EXISTS (
112+
SELECT FROM information_schema.columns
113+
WHERE table_schema = 'public'
114+
AND table_name = ${mainTable}
115+
AND column_name = '_status'
116+
) as exists
117+
`)
136118

137119
if (!statusInMainTableCheck.rows[0]?.exists) {
138120
// _status column doesn't exist in main table, need to restore it
139121
// Check if main collection/global has a locales table
140-
const mainLocalesTableCheck = await db.execute({
141-
drizzle: db.drizzle,
142-
sql: sql`
122+
const mainLocalesTableCheck = await db.execute(sql`
123+
SELECT EXISTS (
124+
SELECT FROM information_schema.tables
125+
WHERE table_schema = 'public'
126+
AND table_name = ${mainLocalesTable}
127+
) as exists
128+
`)
129+
130+
if (mainLocalesTableCheck.rows[0]?.exists) {
131+
// Locales table exists - check if _status is there
132+
const statusInLocalesCheck = await db.execute(sql`
143133
SELECT EXISTS (
144-
SELECT FROM information_schema.tables
134+
SELECT FROM information_schema.columns
145135
WHERE table_schema = 'public'
146136
AND table_name = ${mainLocalesTable}
137+
AND column_name = '_status'
147138
) as exists
148-
`,
149-
})
150-
151-
if (mainLocalesTableCheck.rows[0]?.exists) {
152-
// Locales table exists - check if _status is there
153-
const statusInLocalesCheck = await db.execute({
154-
drizzle: db.drizzle,
155-
sql: sql`
156-
SELECT EXISTS (
157-
SELECT FROM information_schema.columns
158-
WHERE table_schema = 'public'
159-
AND table_name = ${mainLocalesTable}
160-
AND column_name = '_status'
161-
) as exists
162-
`,
163-
})
139+
`)
164140

165141
if (statusInLocalesCheck.rows[0]?.exists) {
166142
// Add _status back to main table
167143
payload.logger.info({ msg: `Restoring _status column to ${mainTable}` })
168144

169-
await db.execute({
170-
drizzle: db.drizzle,
171-
sql: sql`
172-
ALTER TABLE ${sql.identifier(mainTable)} ADD COLUMN _status VARCHAR
173-
`,
174-
})
145+
await db.execute(sql`
146+
ALTER TABLE ${sql.identifier(mainTable)} ADD COLUMN _status VARCHAR
147+
`)
175148

176149
// Copy status from default locale back to main table
177-
await db.execute({
178-
drizzle: db.drizzle,
179-
sql: sql`
180-
UPDATE ${sql.identifier(mainTable)} m
181-
SET _status = l._status
182-
FROM ${sql.identifier(mainLocalesTable)} l
183-
WHERE m.id = l._parent_id
184-
AND l._locale = ${defaultLocale}
185-
`,
186-
})
150+
await db.execute(sql`
151+
UPDATE ${sql.identifier(mainTable)} m
152+
SET _status = l._status
153+
FROM ${sql.identifier(mainLocalesTable)} l
154+
WHERE m.id = l._parent_id
155+
AND l._locale = ${defaultLocale}
156+
`)
187157

188158
// Drop _status from locales table
189159
payload.logger.info({ msg: `Dropping _status column from ${mainLocalesTable}` })
190160

191-
await db.execute({
192-
drizzle: db.drizzle,
193-
sql: sql`
194-
ALTER TABLE ${sql.identifier(mainLocalesTable)} DROP COLUMN _status
195-
`,
196-
})
161+
await db.execute(sql`
162+
ALTER TABLE ${sql.identifier(mainLocalesTable)} DROP COLUMN _status
163+
`)
197164
}
198165
} else {
199166
// No locales table exists - this means collection/global has no localized fields
@@ -202,46 +169,34 @@ export async function down(args: LocalizeStatusArgs): Promise<void> {
202169
msg: `Restoring _status column to ${mainTable} (no locales table exists)`,
203170
})
204171

205-
await db.execute({
206-
drizzle: db.drizzle,
207-
sql: sql`
208-
ALTER TABLE ${sql.identifier(mainTable)} ADD COLUMN _status VARCHAR
209-
`,
210-
})
172+
await db.execute(sql`
173+
ALTER TABLE ${sql.identifier(mainTable)} ADD COLUMN _status VARCHAR
174+
`)
211175

212176
// Set default status based on latest version status for each document
213177
// Get all documents in the collection
214-
const documents = await db.execute({
215-
drizzle: db.drizzle,
216-
sql: sql`
217-
SELECT DISTINCT id
218-
FROM ${sql.identifier(mainTable)}
219-
`,
220-
})
178+
const documents = await db.execute(sql`
179+
SELECT DISTINCT id
180+
FROM ${sql.identifier(mainTable)}
181+
`)
221182

222183
for (const doc of documents.rows) {
223184
// Get latest version status for this document
224-
const latestVersionStatus = await db.execute({
225-
drizzle: db.drizzle,
226-
sql: sql`
227-
SELECT version__status
228-
FROM ${sql.identifier(versionsTable)}
229-
WHERE parent_id = ${doc.id}
230-
ORDER BY created_at DESC
231-
LIMIT 1
232-
`,
233-
})
185+
const latestVersionStatus = await db.execute(sql`
186+
SELECT version__status
187+
FROM ${sql.identifier(versionsTable)}
188+
WHERE parent_id = ${doc.id}
189+
ORDER BY created_at DESC
190+
LIMIT 1
191+
`)
234192

235193
const status = latestVersionStatus.rows[0]?.version__status || 'draft'
236194

237-
await db.execute({
238-
drizzle: db.drizzle,
239-
sql: sql`
240-
UPDATE ${sql.identifier(mainTable)}
241-
SET _status = ${status}
242-
WHERE id = ${doc.id}
243-
`,
244-
})
195+
await db.execute(sql`
196+
UPDATE ${sql.identifier(mainTable)}
197+
SET _status = ${status}
198+
WHERE id = ${doc.id}
199+
`)
245200
}
246201

247202
payload.logger.info({ msg: `Restored _status for ${documents.rows.length} documents` })

‎packages/payload/src/versions/migrations/localizeStatus/sql/migrateMainCollection.ts‎

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,33 @@ export async function migrateMainCollectionStatus({
2626
payload.logger.info({ msg: `Migrating main collection locales for: ${mainLocalesTable}` })
2727

2828
// Get all documents
29-
const documents = await db.execute({
30-
drizzle: db.drizzle,
31-
sql: sql`
32-
SELECT DISTINCT id
33-
FROM ${sql.identifier(mainTable)}
34-
`,
35-
})
29+
const documents = await db.execute(sql`
30+
SELECT DISTINCT id
31+
FROM ${sql.identifier(mainTable)}
32+
`)
3633

3734
for (const doc of documents.rows) {
3835
// For each locale, get the latest version status
3936
for (const locale of locales) {
40-
const latestVersionStatus = await db.execute({
41-
drizzle: db.drizzle,
42-
sql: sql`
43-
SELECT l.version__status as _status
44-
FROM ${sql.identifier(versionsTable)} v
45-
JOIN ${sql.raw(`${versionsTable}_locales`)} l ON l._parent_id = v.id
46-
WHERE v.parent_id = ${doc.id}
47-
AND l._locale = ${locale}
48-
ORDER BY v.created_at DESC
49-
LIMIT 1
50-
`,
51-
})
37+
const latestVersionStatus = await db.execute(sql`
38+
SELECT l.version__status as _status
39+
FROM ${sql.identifier(versionsTable)} v
40+
JOIN ${sql.raw(`${versionsTable}_locales`)} l ON l._parent_id = v.id
41+
WHERE v.parent_id = ${doc.id}
42+
AND l._locale = ${locale}
43+
ORDER BY v.created_at DESC
44+
LIMIT 1
45+
`)
5246

5347
const status = latestVersionStatus.rows[0]?._status || 'draft'
5448

5549
// Update the main collection's locales table with this status
56-
await db.execute({
57-
drizzle: db.drizzle,
58-
sql: sql`
59-
UPDATE ${sql.identifier(mainLocalesTable)}
60-
SET _status = ${status}
61-
WHERE _parent_id = ${doc.id}
62-
AND _locale = ${locale}
63-
`,
64-
})
50+
await db.execute(sql`
51+
UPDATE ${sql.identifier(mainLocalesTable)}
52+
SET _status = ${status}
53+
WHERE _parent_id = ${doc.id}
54+
AND _locale = ${locale}
55+
`)
6556
}
6657
}
6758

‎packages/payload/src/versions/migrations/localizeStatus/sql/migrateMainGlobal.ts‎

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,21 @@ export async function migrateMainGlobalStatus({
2727

2828
// For each locale, get the latest version status
2929
for (const locale of locales) {
30-
const latestVersionStatus = await db.execute({
31-
drizzle: db.drizzle,
32-
sql: sql`
33-
SELECT l.version__status as _status
34-
FROM ${sql.identifier(versionsTable)} v
35-
JOIN ${sql.raw(`${versionsTable}_locales`)} l ON l._parent_id = v.id
36-
WHERE l._locale = ${locale}
37-
ORDER BY v.created_at DESC
38-
LIMIT 1
39-
`,
40-
})
30+
const latestVersionStatus = await db.execute(sql`
31+
SELECT l.version__status as _status
32+
FROM ${sql.identifier(versionsTable)} v
33+
JOIN ${sql.raw(`${versionsTable}_locales`)} l ON l._parent_id = v.id
34+
WHERE l._locale = ${locale}
35+
ORDER BY v.created_at DESC
36+
LIMIT 1
37+
`)
4138

4239
const status = latestVersionStatus.rows[0]?._status || 'draft'
4340

4441
// Get the global document ID from the globals table
45-
const globalDoc = await db.execute({
46-
drizzle: db.drizzle,
47-
sql: sql`
48-
SELECT id FROM ${sql.identifier(globalTable)} LIMIT 1
49-
`,
50-
})
42+
const globalDoc = await db.execute(sql`
43+
SELECT id FROM ${sql.identifier(globalTable)} LIMIT 1
44+
`)
5145

5246
if (globalDoc.rows.length === 0) {
5347
payload.logger.warn({ msg: `No global document found for ${globalSlug}, skipping` })
@@ -57,15 +51,12 @@ export async function migrateMainGlobalStatus({
5751
const globalId = globalDoc.rows[0].id
5852

5953
// Update the global's locales table with this status
60-
await db.execute({
61-
drizzle: db.drizzle,
62-
sql: sql`
63-
UPDATE ${sql.identifier(globalLocalesTable)}
64-
SET _status = ${status}
65-
WHERE _parent_id = ${globalId}
66-
AND _locale = ${locale}
67-
`,
68-
})
54+
await db.execute(sql`
55+
UPDATE ${sql.identifier(globalLocalesTable)}
56+
SET _status = ${status}
57+
WHERE _parent_id = ${globalId}
58+
AND _locale = ${locale}
59+
`)
6960
}
7061

7162
payload.logger.info({ msg: 'Migrated global document' })

0 commit comments

Comments
 (0)