payload 3.92.0 → 3.92.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/operations/resetPassword.d.ts.map +1 -1
- package/dist/auth/operations/resetPassword.js +50 -1
- package/dist/auth/operations/resetPassword.js.map +1 -1
- package/dist/index.bundled.d.ts +9 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/uploads/cropImage.d.ts.map +1 -1
- package/dist/uploads/cropImage.js +4 -2
- package/dist/uploads/cropImage.js.map +1 -1
- package/dist/uploads/generateFileData.d.ts.map +1 -1
- package/dist/uploads/generateFileData.js +6 -1
- package/dist/uploads/generateFileData.js.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/down.d.ts.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/down.js +74 -119
- package/dist/versions/migrations/localizeStatus/sql/down.js.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/migrateMainCollection.d.ts.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/migrateMainCollection.js +19 -28
- package/dist/versions/migrations/localizeStatus/sql/migrateMainCollection.js.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/migrateMainGlobal.d.ts.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/migrateMainGlobal.js +17 -26
- package/dist/versions/migrations/localizeStatus/sql/migrateMainGlobal.js.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/up.d.ts.map +1 -1
- package/dist/versions/migrations/localizeStatus/sql/up.js +48 -90
- package/dist/versions/migrations/localizeStatus/sql/up.js.map +1 -1
- package/package.json +2 -2
|
@@ -9,25 +9,19 @@ import { toSnakeCase } from '../shared.js';
|
|
|
9
9
|
});
|
|
10
10
|
// For each locale, get the latest version status
|
|
11
11
|
for (const locale of locales){
|
|
12
|
-
const latestVersionStatus = await db.execute(
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
LIMIT 1
|
|
21
|
-
`
|
|
22
|
-
});
|
|
12
|
+
const latestVersionStatus = await db.execute(sql`
|
|
13
|
+
SELECT l.version__status as _status
|
|
14
|
+
FROM ${sql.identifier(versionsTable)} v
|
|
15
|
+
JOIN ${sql.raw(`${versionsTable}_locales`)} l ON l._parent_id = v.id
|
|
16
|
+
WHERE l._locale = ${locale}
|
|
17
|
+
ORDER BY v.created_at DESC
|
|
18
|
+
LIMIT 1
|
|
19
|
+
`);
|
|
23
20
|
const status = latestVersionStatus.rows[0]?._status || 'draft';
|
|
24
21
|
// Get the global document ID from the globals table
|
|
25
|
-
const globalDoc = await db.execute(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
SELECT id FROM ${sql.identifier(globalTable)} LIMIT 1
|
|
29
|
-
`
|
|
30
|
-
});
|
|
22
|
+
const globalDoc = await db.execute(sql`
|
|
23
|
+
SELECT id FROM ${sql.identifier(globalTable)} LIMIT 1
|
|
24
|
+
`);
|
|
31
25
|
if (globalDoc.rows.length === 0) {
|
|
32
26
|
payload.logger.warn({
|
|
33
27
|
msg: `No global document found for ${globalSlug}, skipping`
|
|
@@ -36,15 +30,12 @@ import { toSnakeCase } from '../shared.js';
|
|
|
36
30
|
}
|
|
37
31
|
const globalId = globalDoc.rows[0].id;
|
|
38
32
|
// Update the global's locales table with this status
|
|
39
|
-
await db.execute(
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
AND _locale = ${locale}
|
|
46
|
-
`
|
|
47
|
-
});
|
|
33
|
+
await db.execute(sql`
|
|
34
|
+
UPDATE ${sql.identifier(globalLocalesTable)}
|
|
35
|
+
SET _status = ${status}
|
|
36
|
+
WHERE _parent_id = ${globalId}
|
|
37
|
+
AND _locale = ${locale}
|
|
38
|
+
`);
|
|
48
39
|
}
|
|
49
40
|
payload.logger.info({
|
|
50
41
|
msg: 'Migrated global document'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/versions/migrations/localizeStatus/sql/migrateMainGlobal.ts"],"sourcesContent":["import type { Payload } from '../../../../types/index.js'\n\nimport { toSnakeCase } from '../shared.js'\n\n/**\n * Migrates main global document from _status to per-locale status object\n */\nexport async function migrateMainGlobalStatus({\n db,\n globalSlug,\n locales,\n payload,\n sql,\n versionsTable,\n}: {\n db: any\n globalSlug: string\n locales: string[]\n payload: Payload\n sql: any\n versionsTable: string\n}): Promise<void> {\n const globalTable = toSnakeCase(globalSlug)\n const globalLocalesTable = `${globalTable}_locales`\n\n payload.logger.info({ msg: `Migrating main global locales for: ${globalLocalesTable}` })\n\n // For each locale, get the latest version status\n for (const locale of locales) {\n const latestVersionStatus = await db.execute(
|
|
1
|
+
{"version":3,"sources":["../../../../../src/versions/migrations/localizeStatus/sql/migrateMainGlobal.ts"],"sourcesContent":["import type { Payload } from '../../../../types/index.js'\n\nimport { toSnakeCase } from '../shared.js'\n\n/**\n * Migrates main global document from _status to per-locale status object\n */\nexport async function migrateMainGlobalStatus({\n db,\n globalSlug,\n locales,\n payload,\n sql,\n versionsTable,\n}: {\n db: any\n globalSlug: string\n locales: string[]\n payload: Payload\n sql: any\n versionsTable: string\n}): Promise<void> {\n const globalTable = toSnakeCase(globalSlug)\n const globalLocalesTable = `${globalTable}_locales`\n\n payload.logger.info({ msg: `Migrating main global locales for: ${globalLocalesTable}` })\n\n // For each locale, get the latest version status\n for (const locale of locales) {\n const latestVersionStatus = await db.execute(sql`\n SELECT l.version__status as _status\n FROM ${sql.identifier(versionsTable)} v\n JOIN ${sql.raw(`${versionsTable}_locales`)} l ON l._parent_id = v.id\n WHERE l._locale = ${locale}\n ORDER BY v.created_at DESC\n LIMIT 1\n `)\n\n const status = latestVersionStatus.rows[0]?._status || 'draft'\n\n // Get the global document ID from the globals table\n const globalDoc = await db.execute(sql`\n SELECT id FROM ${sql.identifier(globalTable)} LIMIT 1\n `)\n\n if (globalDoc.rows.length === 0) {\n payload.logger.warn({ msg: `No global document found for ${globalSlug}, skipping` })\n continue\n }\n\n const globalId = globalDoc.rows[0].id\n\n // Update the global's locales table with this status\n await db.execute(sql`\n UPDATE ${sql.identifier(globalLocalesTable)}\n SET _status = ${status}\n WHERE _parent_id = ${globalId}\n AND _locale = ${locale}\n `)\n }\n\n payload.logger.info({ msg: 'Migrated global document' })\n}\n"],"names":["toSnakeCase","migrateMainGlobalStatus","db","globalSlug","locales","payload","sql","versionsTable","globalTable","globalLocalesTable","logger","info","msg","locale","latestVersionStatus","execute","identifier","raw","status","rows","_status","globalDoc","length","warn","globalId","id"],"mappings":"AAEA,SAASA,WAAW,QAAQ,eAAc;AAE1C;;CAEC,GACD,OAAO,eAAeC,wBAAwB,EAC5CC,EAAE,EACFC,UAAU,EACVC,OAAO,EACPC,OAAO,EACPC,GAAG,EACHC,aAAa,EAQd;IACC,MAAMC,cAAcR,YAAYG;IAChC,MAAMM,qBAAqB,GAAGD,YAAY,QAAQ,CAAC;IAEnDH,QAAQK,MAAM,CAACC,IAAI,CAAC;QAAEC,KAAK,CAAC,mCAAmC,EAAEH,oBAAoB;IAAC;IAEtF,iDAAiD;IACjD,KAAK,MAAMI,UAAUT,QAAS;QAC5B,MAAMU,sBAAsB,MAAMZ,GAAGa,OAAO,CAACT,GAAG,CAAC;;WAE1C,EAAEA,IAAIU,UAAU,CAACT,eAAe;WAChC,EAAED,IAAIW,GAAG,CAAC,GAAGV,cAAc,QAAQ,CAAC,EAAE;wBACzB,EAAEM,OAAO;;;IAG7B,CAAC;QAED,MAAMK,SAASJ,oBAAoBK,IAAI,CAAC,EAAE,EAAEC,WAAW;QAEvD,oDAAoD;QACpD,MAAMC,YAAY,MAAMnB,GAAGa,OAAO,CAACT,GAAG,CAAC;qBACtB,EAAEA,IAAIU,UAAU,CAACR,aAAa;IAC/C,CAAC;QAED,IAAIa,UAAUF,IAAI,CAACG,MAAM,KAAK,GAAG;YAC/BjB,QAAQK,MAAM,CAACa,IAAI,CAAC;gBAAEX,KAAK,CAAC,6BAA6B,EAAET,WAAW,UAAU,CAAC;YAAC;YAClF;QACF;QAEA,MAAMqB,WAAWH,UAAUF,IAAI,CAAC,EAAE,CAACM,EAAE;QAErC,qDAAqD;QACrD,MAAMvB,GAAGa,OAAO,CAACT,GAAG,CAAC;aACZ,EAAEA,IAAIU,UAAU,CAACP,oBAAoB;oBAC9B,EAAES,OAAO;yBACJ,EAAEM,SAAS;oBAChB,EAAEX,OAAO;IACzB,CAAC;IACH;IAEAR,QAAQK,MAAM,CAACC,IAAI,CAAC;QAAEC,KAAK;IAA2B;AACxD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"up.d.ts","sourceRoot":"","sources":["../../../../../src/versions/migrations/localizeStatus/sql/up.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAMzD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,EAAE,EAAE,GAAG,CAAA;IACP,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,CAAC,EAAE,GAAG,CAAA;IACT,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,wBAAsB,EAAE,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,
|
|
1
|
+
{"version":3,"file":"up.d.ts","sourceRoot":"","sources":["../../../../../src/versions/migrations/localizeStatus/sql/up.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAA;AAMzD,MAAM,MAAM,kBAAkB,GAAG;IAC/B,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,EAAE,EAAE,GAAG,CAAA;IACP,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,CAAC,EAAE,GAAG,CAAA;IACT,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAED,wBAAsB,EAAE,CAAC,IAAI,EAAE,kBAAkB,GAAG,OAAO,CAAC,IAAI,CAAC,CA6PhE"}
|
|
@@ -55,40 +55,32 @@ export async function up(args) {
|
|
|
55
55
|
return;
|
|
56
56
|
}
|
|
57
57
|
// Validate that version__status column exists before proceeding
|
|
58
|
-
const columnCheckResult = await db.execute(
|
|
59
|
-
drizzle: db.drizzle,
|
|
60
|
-
sql: sql`
|
|
58
|
+
const columnCheckResult = await db.execute(sql`
|
|
61
59
|
SELECT EXISTS (
|
|
62
60
|
SELECT FROM information_schema.columns
|
|
63
61
|
WHERE table_schema = 'public'
|
|
64
62
|
AND table_name = ${versionsTable}
|
|
65
63
|
AND column_name = 'version__status'
|
|
66
64
|
) as exists
|
|
67
|
-
`
|
|
68
|
-
});
|
|
65
|
+
`);
|
|
69
66
|
if (!columnCheckResult.rows[0]?.exists) {
|
|
70
67
|
throw new Error(`Migration aborted: version__status column not found in ${versionsTable} table. ` + `This migration should only run on schemas that have NOT yet been migrated to per-locale status. ` + `If you've already run this migration, no action is needed.`);
|
|
71
68
|
}
|
|
72
69
|
// 1. Check if the locales table exists
|
|
73
|
-
const localesTableCheckResult = await db.execute(
|
|
74
|
-
drizzle: db.drizzle,
|
|
75
|
-
sql: sql`
|
|
70
|
+
const localesTableCheckResult = await db.execute(sql`
|
|
76
71
|
SELECT EXISTS (
|
|
77
72
|
SELECT FROM information_schema.tables
|
|
78
73
|
WHERE table_schema = 'public'
|
|
79
74
|
AND table_name = ${localesTable}
|
|
80
75
|
) as exists
|
|
81
|
-
`
|
|
82
|
-
});
|
|
76
|
+
`);
|
|
83
77
|
const localesTableExists = localesTableCheckResult.rows[0]?.exists;
|
|
84
78
|
if (!localesTableExists) {
|
|
85
79
|
// SCENARIO 1: Create the locales table (first localized field in versions)
|
|
86
80
|
payload.logger.info({
|
|
87
81
|
msg: `Creating new locales table: ${localesTable}`
|
|
88
82
|
});
|
|
89
|
-
await db.execute(
|
|
90
|
-
drizzle: db.drizzle,
|
|
91
|
-
sql: sql`
|
|
83
|
+
await db.execute(sql`
|
|
92
84
|
CREATE TABLE ${sql.identifier(localesTable)} (
|
|
93
85
|
id SERIAL PRIMARY KEY,
|
|
94
86
|
_locale VARCHAR NOT NULL,
|
|
@@ -97,22 +89,18 @@ export async function up(args) {
|
|
|
97
89
|
UNIQUE(_locale, _parent_id),
|
|
98
90
|
FOREIGN KEY (_parent_id) REFERENCES ${sql.identifier(versionsTable)}(id) ON DELETE CASCADE
|
|
99
91
|
)
|
|
100
|
-
`
|
|
101
|
-
});
|
|
92
|
+
`);
|
|
102
93
|
// Create one row per locale per version record
|
|
103
94
|
// Simple approach: copy the same status to all locales
|
|
104
95
|
for (const locale of locales){
|
|
105
|
-
const inserted = await db.execute(
|
|
106
|
-
drizzle: db.drizzle,
|
|
107
|
-
sql: sql`
|
|
96
|
+
const inserted = await db.execute(sql`
|
|
108
97
|
INSERT INTO ${sql.identifier(localesTable)} (_locale, _parent_id, version__status)
|
|
109
98
|
SELECT ${locale}, id, version__status
|
|
110
99
|
FROM ${sql.identifier(versionsTable)}
|
|
111
100
|
RETURNING id
|
|
112
|
-
`
|
|
113
|
-
});
|
|
101
|
+
`);
|
|
114
102
|
payload.logger.info({
|
|
115
|
-
msg: `Inserted ${inserted.length} rows for locale: ${locale}`
|
|
103
|
+
msg: `Inserted ${inserted.rows.length} rows for locale: ${locale}`
|
|
116
104
|
});
|
|
117
105
|
}
|
|
118
106
|
} else {
|
|
@@ -120,39 +108,30 @@ export async function up(args) {
|
|
|
120
108
|
payload.logger.info({
|
|
121
109
|
msg: `Adding version__status column to existing table: ${localesTable}`
|
|
122
110
|
});
|
|
123
|
-
await db.execute(
|
|
124
|
-
drizzle: db.drizzle,
|
|
125
|
-
sql: sql`
|
|
111
|
+
await db.execute(sql`
|
|
126
112
|
ALTER TABLE ${sql.identifier(localesTable)} ADD COLUMN version__status VARCHAR
|
|
127
|
-
`
|
|
128
|
-
});
|
|
113
|
+
`);
|
|
129
114
|
// INTELLIGENT DATA MIGRATION using historical publishedLocale data
|
|
130
115
|
payload.logger.info({
|
|
131
116
|
msg: 'Processing version history to determine status per locale...'
|
|
132
117
|
});
|
|
133
118
|
// First, get the list of locales that actually exist in the locales table
|
|
134
119
|
// This is important because the config may have more locales defined than what's in the OLD schema
|
|
135
|
-
const existingLocalesResult = await db.execute(
|
|
136
|
-
drizzle: db.drizzle,
|
|
137
|
-
sql: sql`
|
|
120
|
+
const existingLocalesResult = await db.execute(sql`
|
|
138
121
|
SELECT DISTINCT _locale
|
|
139
122
|
FROM ${sql.identifier(localesTable)}
|
|
140
123
|
ORDER BY _locale
|
|
141
|
-
`
|
|
142
|
-
});
|
|
124
|
+
`);
|
|
143
125
|
const existingLocales = existingLocalesResult.rows.map((row)=>row._locale);
|
|
144
126
|
payload.logger.info({
|
|
145
127
|
msg: `Found existing locales in table: ${existingLocales.join(', ')}`
|
|
146
128
|
});
|
|
147
129
|
// Get all version records grouped by parent document, ordered chronologically
|
|
148
|
-
const versionsResult = await db.execute(
|
|
149
|
-
drizzle: db.drizzle,
|
|
150
|
-
sql: sql`
|
|
130
|
+
const versionsResult = await db.execute(sql`
|
|
151
131
|
SELECT id, parent_id as parent, version__status as _status, published_locale, snapshot, created_at
|
|
152
132
|
FROM ${sql.identifier(versionsTable)}
|
|
153
133
|
ORDER BY parent_id, created_at ASC
|
|
154
|
-
`
|
|
155
|
-
});
|
|
134
|
+
`);
|
|
156
135
|
// Use shared function to calculate version locale statuses
|
|
157
136
|
// Only process locales that actually exist in the locales table
|
|
158
137
|
const versionLocaleStatus = calculateVersionLocaleStatuses(versionsResult.rows, existingLocales, payload);
|
|
@@ -163,15 +142,12 @@ export async function up(args) {
|
|
|
163
142
|
let updateCount = 0;
|
|
164
143
|
for (const [versionId, localeMap] of versionLocaleStatus.entries()){
|
|
165
144
|
for (const [locale, status] of localeMap.entries()){
|
|
166
|
-
await db.execute(
|
|
167
|
-
drizzle: db.drizzle,
|
|
168
|
-
sql: sql`
|
|
145
|
+
await db.execute(sql`
|
|
169
146
|
UPDATE ${sql.identifier(localesTable)}
|
|
170
147
|
SET version__status = ${status}
|
|
171
148
|
WHERE _parent_id = ${versionId}
|
|
172
149
|
AND _locale = ${locale}
|
|
173
|
-
`
|
|
174
|
-
});
|
|
150
|
+
`);
|
|
175
151
|
updateCount++;
|
|
176
152
|
}
|
|
177
153
|
}
|
|
@@ -180,49 +156,37 @@ export async function up(args) {
|
|
|
180
156
|
});
|
|
181
157
|
}
|
|
182
158
|
// 3. Drop the old version__status column from main versions table
|
|
183
|
-
await db.execute(
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
ALTER TABLE ${sql.identifier(versionsTable)} DROP COLUMN version__status
|
|
187
|
-
`
|
|
188
|
-
});
|
|
159
|
+
await db.execute(sql`
|
|
160
|
+
ALTER TABLE ${sql.identifier(versionsTable)} DROP COLUMN version__status
|
|
161
|
+
`);
|
|
189
162
|
// 4. Create and populate _status column in main collection/global locales table
|
|
190
163
|
// With localizeStatus enabled, _status is a localized field stored in the collection's locales table
|
|
191
164
|
// We need to create this column and populate it based on the latest version status per locale
|
|
192
165
|
const mainTable = collectionSlug ? toSnakeCase(collectionSlug) : toSnakeCase(globalSlug);
|
|
193
166
|
const mainLocalesTable = `${mainTable}_locales`;
|
|
194
|
-
const localesTableCheck = await db.execute(
|
|
195
|
-
|
|
196
|
-
|
|
167
|
+
const localesTableCheck = await db.execute(sql`
|
|
168
|
+
SELECT EXISTS (
|
|
169
|
+
SELECT FROM information_schema.tables
|
|
170
|
+
WHERE table_schema = 'public'
|
|
171
|
+
AND table_name = ${mainLocalesTable}
|
|
172
|
+
) as exists
|
|
173
|
+
`);
|
|
174
|
+
if (localesTableCheck.rows[0]?.exists) {
|
|
175
|
+
// Check if _status column already exists in the locales table
|
|
176
|
+
const statusColumnCheck = await db.execute(sql`
|
|
197
177
|
SELECT EXISTS (
|
|
198
|
-
SELECT FROM information_schema.
|
|
178
|
+
SELECT FROM information_schema.columns
|
|
199
179
|
WHERE table_schema = 'public'
|
|
200
180
|
AND table_name = ${mainLocalesTable}
|
|
181
|
+
AND column_name = '_status'
|
|
201
182
|
) as exists
|
|
202
|
-
`
|
|
203
|
-
});
|
|
204
|
-
if (localesTableCheck.rows[0]?.exists) {
|
|
205
|
-
// Check if _status column already exists in the locales table
|
|
206
|
-
const statusColumnCheck = await db.execute({
|
|
207
|
-
drizzle: db.drizzle,
|
|
208
|
-
sql: sql`
|
|
209
|
-
SELECT EXISTS (
|
|
210
|
-
SELECT FROM information_schema.columns
|
|
211
|
-
WHERE table_schema = 'public'
|
|
212
|
-
AND table_name = ${mainLocalesTable}
|
|
213
|
-
AND column_name = '_status'
|
|
214
|
-
) as exists
|
|
215
|
-
`
|
|
216
|
-
});
|
|
183
|
+
`);
|
|
217
184
|
if (!statusColumnCheck.rows[0]?.exists) {
|
|
218
185
|
// Add _status column to locales table
|
|
219
|
-
await db.execute(
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
ADD COLUMN _status VARCHAR DEFAULT 'draft'
|
|
224
|
-
`
|
|
225
|
-
});
|
|
186
|
+
await db.execute(sql`
|
|
187
|
+
ALTER TABLE ${sql.identifier(mainLocalesTable)}
|
|
188
|
+
ADD COLUMN _status VARCHAR DEFAULT 'draft'
|
|
189
|
+
`);
|
|
226
190
|
}
|
|
227
191
|
// Now populate the _status values from the latest version
|
|
228
192
|
if (collectionSlug) {
|
|
@@ -250,24 +214,18 @@ export async function up(args) {
|
|
|
250
214
|
});
|
|
251
215
|
}
|
|
252
216
|
// 5. Drop _status from main table if it exists (it will be in locales table now)
|
|
253
|
-
const mainTableStatusCheck = await db.execute(
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
) as exists
|
|
262
|
-
`
|
|
263
|
-
});
|
|
217
|
+
const mainTableStatusCheck = await db.execute(sql`
|
|
218
|
+
SELECT EXISTS (
|
|
219
|
+
SELECT FROM information_schema.columns
|
|
220
|
+
WHERE table_schema = 'public'
|
|
221
|
+
AND table_name = ${mainTable}
|
|
222
|
+
AND column_name = '_status'
|
|
223
|
+
) as exists
|
|
224
|
+
`);
|
|
264
225
|
if (mainTableStatusCheck.rows[0]?.exists) {
|
|
265
|
-
await db.execute(
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
ALTER TABLE ${sql.identifier(mainTable)} DROP COLUMN _status
|
|
269
|
-
`
|
|
270
|
-
});
|
|
226
|
+
await db.execute(sql`
|
|
227
|
+
ALTER TABLE ${sql.identifier(mainTable)} DROP COLUMN _status
|
|
228
|
+
`);
|
|
271
229
|
}
|
|
272
230
|
payload.logger.info({
|
|
273
231
|
msg: 'Migration completed successfully'
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../src/versions/migrations/localizeStatus/sql/up.ts"],"sourcesContent":["import type { Payload } from '../../../../types/index.js'\n\nimport { calculateVersionLocaleStatuses, toSnakeCase } from '../shared.js'\nimport { migrateMainCollectionStatus } from './migrateMainCollection.js'\nimport { migrateMainGlobalStatus } from './migrateMainGlobal.js'\n\nexport type LocalizeStatusArgs = {\n collectionSlug?: string\n db: any\n globalSlug?: string\n payload: Payload\n req?: any\n sql: any\n}\n\nexport async function up(args: LocalizeStatusArgs): Promise<void> {\n const { collectionSlug, db, globalSlug, payload, req, sql } = args\n\n if (!collectionSlug && !globalSlug) {\n throw new Error('Either collectionSlug or globalSlug must be provided')\n }\n\n if (collectionSlug && globalSlug) {\n throw new Error('Cannot provide both collectionSlug and globalSlug')\n }\n\n const entitySlug = collectionSlug || globalSlug\n // Convert camelCase slugs to snake_case and add version prefix/suffix\n const versionsTable = collectionSlug\n ? `_${toSnakeCase(collectionSlug)}_v`\n : `_${toSnakeCase(globalSlug!)}_v`\n const localesTable = `${versionsTable}_locales`\n\n if (!payload.config.localization) {\n throw new Error('Localization is not enabled in payload config')\n }\n\n // Check if versions are enabled on this collection/global\n let entityConfig\n if (collectionSlug) {\n const collection = payload.config.collections.find((c) => c.slug === collectionSlug)\n if (collection) {\n entityConfig = collection\n }\n } else if (globalSlug) {\n const global = payload.config.globals.find((g) => g.slug === globalSlug)\n if (global) {\n entityConfig = global\n }\n }\n\n if (!entityConfig) {\n throw new Error(\n `${collectionSlug ? 'Collection' : 'Global'} not found: ${collectionSlug || globalSlug}`,\n )\n }\n\n payload.logger.info({\n msg: `Starting _status localization migration for ${collectionSlug ? 'collection' : 'global'}: ${entitySlug}`,\n })\n\n // Get filtered locales if filterAvailableLocales is defined\n let locales = payload.config.localization.localeCodes\n if (typeof payload.config.localization.filterAvailableLocales === 'function') {\n const filteredLocaleObjects = await payload.config.localization.filterAvailableLocales({\n locales: payload.config.localization.locales,\n req,\n })\n locales = filteredLocaleObjects.map((locale) => locale.code)\n }\n payload.logger.info({ msg: `Locales: ${locales.join(', ')}` })\n\n // Check if versions are enabled in config (skip if not)\n if (!entityConfig.versions) {\n payload.logger.info({\n msg: `Skipping migration for ${collectionSlug ? 'collection' : 'global'}: ${entitySlug} - versions not enabled`,\n })\n return\n }\n\n // Validate that version__status column exists before proceeding\n const columnCheckResult = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n SELECT EXISTS (\n SELECT FROM information_schema.columns\n WHERE table_schema = 'public'\n AND table_name = ${versionsTable}\n AND column_name = 'version__status'\n ) as exists\n `,\n })\n\n if (!columnCheckResult.rows[0]?.exists) {\n throw new Error(\n `Migration aborted: version__status column not found in ${versionsTable} table. ` +\n `This migration should only run on schemas that have NOT yet been migrated to per-locale status. ` +\n `If you've already run this migration, no action is needed.`,\n )\n }\n\n // 1. Check if the locales table exists\n const localesTableCheckResult = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n SELECT EXISTS (\n SELECT FROM information_schema.tables\n WHERE table_schema = 'public'\n AND table_name = ${localesTable}\n ) as exists\n `,\n })\n\n const localesTableExists = localesTableCheckResult.rows[0]?.exists\n\n if (!localesTableExists) {\n // SCENARIO 1: Create the locales table (first localized field in versions)\n payload.logger.info({ msg: `Creating new locales table: ${localesTable}` })\n\n await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n CREATE TABLE ${sql.identifier(localesTable)} (\n id SERIAL PRIMARY KEY,\n _locale VARCHAR NOT NULL,\n _parent_id INTEGER NOT NULL,\n version__status VARCHAR,\n UNIQUE(_locale, _parent_id),\n FOREIGN KEY (_parent_id) REFERENCES ${sql.identifier(versionsTable)}(id) ON DELETE CASCADE\n )\n `,\n })\n\n // Create one row per locale per version record\n // Simple approach: copy the same status to all locales\n for (const locale of locales) {\n const inserted = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n INSERT INTO ${sql.identifier(localesTable)} (_locale, _parent_id, version__status)\n SELECT ${locale}, id, version__status\n FROM ${sql.identifier(versionsTable)}\n RETURNING id\n `,\n })\n payload.logger.info({\n msg: `Inserted ${inserted.length} rows for locale: ${locale}`,\n })\n }\n } else {\n // SCENARIO 2: Add version__status column to existing locales table\n payload.logger.info({ msg: `Adding version__status column to existing table: ${localesTable}` })\n\n await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n ALTER TABLE ${sql.identifier(localesTable)} ADD COLUMN version__status VARCHAR\n `,\n })\n\n // INTELLIGENT DATA MIGRATION using historical publishedLocale data\n payload.logger.info({ msg: 'Processing version history to determine status per locale...' })\n\n // First, get the list of locales that actually exist in the locales table\n // This is important because the config may have more locales defined than what's in the OLD schema\n const existingLocalesResult = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n SELECT DISTINCT _locale\n FROM ${sql.identifier(localesTable)}\n ORDER BY _locale\n `,\n })\n const existingLocales = existingLocalesResult.rows.map((row: any) => row._locale as string)\n payload.logger.info({\n msg: `Found existing locales in table: ${existingLocales.join(', ')}`,\n })\n\n // Get all version records grouped by parent document, ordered chronologically\n const versionsResult = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n SELECT id, parent_id as parent, version__status as _status, published_locale, snapshot, created_at\n FROM ${sql.identifier(versionsTable)}\n ORDER BY parent_id, created_at ASC\n `,\n })\n\n // Use shared function to calculate version locale statuses\n // Only process locales that actually exist in the locales table\n const versionLocaleStatus = calculateVersionLocaleStatuses(\n versionsResult.rows,\n existingLocales,\n payload,\n )\n\n // Now update the locales table with the calculated status for each version\n payload.logger.info({ msg: 'Updating locales table with calculated statuses...' })\n\n let updateCount = 0\n for (const [versionId, localeMap] of versionLocaleStatus.entries()) {\n for (const [locale, status] of localeMap.entries()) {\n await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n UPDATE ${sql.identifier(localesTable)}\n SET version__status = ${status}\n WHERE _parent_id = ${versionId}\n AND _locale = ${locale}\n `,\n })\n updateCount++\n }\n }\n\n payload.logger.info({ msg: `Updated ${updateCount} locale rows with status` })\n }\n\n // 3. Drop the old version__status column from main versions table\n await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n ALTER TABLE ${sql.identifier(versionsTable)} DROP COLUMN version__status\n `,\n })\n\n // 4. Create and populate _status column in main collection/global locales table\n // With localizeStatus enabled, _status is a localized field stored in the collection's locales table\n // We need to create this column and populate it based on the latest version status per locale\n const mainTable = collectionSlug ? toSnakeCase(collectionSlug) : toSnakeCase(globalSlug!)\n const mainLocalesTable = `${mainTable}_locales`\n\n const localesTableCheck = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n SELECT EXISTS (\n SELECT FROM information_schema.tables\n WHERE table_schema = 'public'\n AND table_name = ${mainLocalesTable}\n ) as exists\n `,\n })\n\n if (localesTableCheck.rows[0]?.exists) {\n // Check if _status column already exists in the locales table\n const statusColumnCheck = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n SELECT EXISTS (\n SELECT FROM information_schema.columns\n WHERE table_schema = 'public'\n AND table_name = ${mainLocalesTable}\n AND column_name = '_status'\n ) as exists\n `,\n })\n\n if (!statusColumnCheck.rows[0]?.exists) {\n // Add _status column to locales table\n await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n ALTER TABLE ${sql.identifier(mainLocalesTable)}\n ADD COLUMN _status VARCHAR DEFAULT 'draft'\n `,\n })\n }\n\n // Now populate the _status values from the latest version\n if (collectionSlug) {\n await migrateMainCollectionStatus({\n collectionSlug,\n db,\n locales,\n payload,\n sql,\n versionsTable,\n })\n } else if (globalSlug) {\n await migrateMainGlobalStatus({ db, globalSlug, locales, payload, sql, versionsTable })\n }\n } else {\n payload.logger.info({\n msg: `No locales table found: ${mainLocalesTable} (collection/global not localized)`,\n })\n }\n\n // 5. Drop _status from main table if it exists (it will be in locales table now)\n const mainTableStatusCheck = await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n SELECT EXISTS (\n SELECT FROM information_schema.columns\n WHERE table_schema = 'public'\n AND table_name = ${mainTable}\n AND column_name = '_status'\n ) as exists\n `,\n })\n\n if (mainTableStatusCheck.rows[0]?.exists) {\n await db.execute({\n drizzle: db.drizzle,\n sql: sql`\n ALTER TABLE ${sql.identifier(mainTable)} DROP COLUMN _status\n `,\n })\n }\n\n payload.logger.info({ msg: 'Migration completed successfully' })\n}\n"],"names":["calculateVersionLocaleStatuses","toSnakeCase","migrateMainCollectionStatus","migrateMainGlobalStatus","up","args","collectionSlug","db","globalSlug","payload","req","sql","Error","entitySlug","versionsTable","localesTable","config","localization","entityConfig","collection","collections","find","c","slug","global","globals","g","logger","info","msg","locales","localeCodes","filterAvailableLocales","filteredLocaleObjects","map","locale","code","join","versions","columnCheckResult","execute","drizzle","rows","exists","localesTableCheckResult","localesTableExists","identifier","inserted","length","existingLocalesResult","existingLocales","row","_locale","versionsResult","versionLocaleStatus","updateCount","versionId","localeMap","entries","status","mainTable","mainLocalesTable","localesTableCheck","statusColumnCheck","mainTableStatusCheck"],"mappings":"AAEA,SAASA,8BAA8B,EAAEC,WAAW,QAAQ,eAAc;AAC1E,SAASC,2BAA2B,QAAQ,6BAA4B;AACxE,SAASC,uBAAuB,QAAQ,yBAAwB;AAWhE,OAA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CAAC;IACH;IAEA,IAAII,qBAAqBtB,IAAI,CAAC,EAAE,EAAEC,QAAQ;QACxC,MAAMpC,GAAGiC,OAAO,CAAC;YACfC,SAASlC,GAAGkC,OAAO;YACnB9B,KAAKA,GAAG,CAAC;oBACK,EAAEA,IAAImC,UAAU,CAACc,WAAW;MAC1C,CAAC;QACH;IACF;IAEAnD,QAAQkB,MAAM,CAACC,IAAI,CAAC;QAAEC,KAAK;IAAmC;AAChE"}
|
|
1
|
+
{"version":3,"sources":["../../../../../src/versions/migrations/localizeStatus/sql/up.ts"],"sourcesContent":["import type { Payload } from '../../../../types/index.js'\n\nimport { calculateVersionLocaleStatuses, toSnakeCase } from '../shared.js'\nimport { migrateMainCollectionStatus } from './migrateMainCollection.js'\nimport { migrateMainGlobalStatus } from './migrateMainGlobal.js'\n\nexport type LocalizeStatusArgs = {\n collectionSlug?: string\n db: any\n globalSlug?: string\n payload: Payload\n req?: any\n sql: any\n}\n\nexport async function up(args: LocalizeStatusArgs): Promise<void> {\n const { collectionSlug, db, globalSlug, payload, req, sql } = args\n\n if (!collectionSlug && !globalSlug) {\n throw new Error('Either collectionSlug or globalSlug must be provided')\n }\n\n if (collectionSlug && globalSlug) {\n throw new Error('Cannot provide both collectionSlug and globalSlug')\n }\n\n const entitySlug = collectionSlug || globalSlug\n // Convert camelCase slugs to snake_case and add version prefix/suffix\n const versionsTable = collectionSlug\n ? `_${toSnakeCase(collectionSlug)}_v`\n : `_${toSnakeCase(globalSlug!)}_v`\n const localesTable = `${versionsTable}_locales`\n\n if (!payload.config.localization) {\n throw new Error('Localization is not enabled in payload config')\n }\n\n // Check if versions are enabled on this collection/global\n let entityConfig\n if (collectionSlug) {\n const collection = payload.config.collections.find((c) => c.slug === collectionSlug)\n if (collection) {\n entityConfig = collection\n }\n } else if (globalSlug) {\n const global = payload.config.globals.find((g) => g.slug === globalSlug)\n if (global) {\n entityConfig = global\n }\n }\n\n if (!entityConfig) {\n throw new Error(\n `${collectionSlug ? 'Collection' : 'Global'} not found: ${collectionSlug || globalSlug}`,\n )\n }\n\n payload.logger.info({\n msg: `Starting _status localization migration for ${collectionSlug ? 'collection' : 'global'}: ${entitySlug}`,\n })\n\n // Get filtered locales if filterAvailableLocales is defined\n let locales = payload.config.localization.localeCodes\n if (typeof payload.config.localization.filterAvailableLocales === 'function') {\n const filteredLocaleObjects = await payload.config.localization.filterAvailableLocales({\n locales: payload.config.localization.locales,\n req,\n })\n locales = filteredLocaleObjects.map((locale) => locale.code)\n }\n payload.logger.info({ msg: `Locales: ${locales.join(', ')}` })\n\n // Check if versions are enabled in config (skip if not)\n if (!entityConfig.versions) {\n payload.logger.info({\n msg: `Skipping migration for ${collectionSlug ? 'collection' : 'global'}: ${entitySlug} - versions not enabled`,\n })\n return\n }\n\n // Validate that version__status column exists before proceeding\n const columnCheckResult = await db.execute(sql`\n SELECT EXISTS (\n SELECT FROM information_schema.columns\n WHERE table_schema = 'public'\n AND table_name = ${versionsTable}\n AND column_name = 'version__status'\n ) as exists\n `)\n\n if (!columnCheckResult.rows[0]?.exists) {\n throw new Error(\n `Migration aborted: version__status column not found in ${versionsTable} table. ` +\n `This migration should only run on schemas that have NOT yet been migrated to per-locale status. ` +\n `If you've already run this migration, no action is needed.`,\n )\n }\n\n // 1. Check if the locales table exists\n const localesTableCheckResult = await db.execute(sql`\n SELECT EXISTS (\n SELECT FROM information_schema.tables\n WHERE table_schema = 'public'\n AND table_name = ${localesTable}\n ) as exists\n `)\n\n const localesTableExists = localesTableCheckResult.rows[0]?.exists\n\n if (!localesTableExists) {\n // SCENARIO 1: Create the locales table (first localized field in versions)\n payload.logger.info({ msg: `Creating new locales table: ${localesTable}` })\n\n await db.execute(sql`\n CREATE TABLE ${sql.identifier(localesTable)} (\n id SERIAL PRIMARY KEY,\n _locale VARCHAR NOT NULL,\n _parent_id INTEGER NOT NULL,\n version__status VARCHAR,\n UNIQUE(_locale, _parent_id),\n FOREIGN KEY (_parent_id) REFERENCES ${sql.identifier(versionsTable)}(id) ON DELETE CASCADE\n )\n `)\n\n // Create one row per locale per version record\n // Simple approach: copy the same status to all locales\n for (const locale of locales) {\n const inserted = await db.execute(sql`\n INSERT INTO ${sql.identifier(localesTable)} (_locale, _parent_id, version__status)\n SELECT ${locale}, id, version__status\n FROM ${sql.identifier(versionsTable)}\n RETURNING id\n `)\n payload.logger.info({\n msg: `Inserted ${inserted.rows.length} rows for locale: ${locale}`,\n })\n }\n } else {\n // SCENARIO 2: Add version__status column to existing locales table\n payload.logger.info({ msg: `Adding version__status column to existing table: ${localesTable}` })\n\n await db.execute(sql`\n ALTER TABLE ${sql.identifier(localesTable)} ADD COLUMN version__status VARCHAR\n `)\n\n // INTELLIGENT DATA MIGRATION using historical publishedLocale data\n payload.logger.info({ msg: 'Processing version history to determine status per locale...' })\n\n // First, get the list of locales that actually exist in the locales table\n // This is important because the config may have more locales defined than what's in the OLD schema\n const existingLocalesResult = await db.execute(sql`\n SELECT DISTINCT _locale\n FROM ${sql.identifier(localesTable)}\n ORDER BY _locale\n `)\n const existingLocales = existingLocalesResult.rows.map((row: any) => row._locale as string)\n payload.logger.info({\n msg: `Found existing locales in table: ${existingLocales.join(', ')}`,\n })\n\n // Get all version records grouped by parent document, ordered chronologically\n const versionsResult = await db.execute(sql`\n SELECT id, parent_id as parent, version__status as _status, published_locale, snapshot, created_at\n FROM ${sql.identifier(versionsTable)}\n ORDER BY parent_id, created_at ASC\n `)\n\n // Use shared function to calculate version locale statuses\n // Only process locales that actually exist in the locales table\n const versionLocaleStatus = calculateVersionLocaleStatuses(\n versionsResult.rows,\n existingLocales,\n payload,\n )\n\n // Now update the locales table with the calculated status for each version\n payload.logger.info({ msg: 'Updating locales table with calculated statuses...' })\n\n let updateCount = 0\n for (const [versionId, localeMap] of versionLocaleStatus.entries()) {\n for (const [locale, status] of localeMap.entries()) {\n await db.execute(sql`\n UPDATE ${sql.identifier(localesTable)}\n SET version__status = ${status}\n WHERE _parent_id = ${versionId}\n AND _locale = ${locale}\n `)\n updateCount++\n }\n }\n\n payload.logger.info({ msg: `Updated ${updateCount} locale rows with status` })\n }\n\n // 3. Drop the old version__status column from main versions table\n await db.execute(sql`\n ALTER TABLE ${sql.identifier(versionsTable)} DROP COLUMN version__status\n `)\n\n // 4. Create and populate _status column in main collection/global locales table\n // With localizeStatus enabled, _status is a localized field stored in the collection's locales table\n // We need to create this column and populate it based on the latest version status per locale\n const mainTable = collectionSlug ? toSnakeCase(collectionSlug) : toSnakeCase(globalSlug!)\n const mainLocalesTable = `${mainTable}_locales`\n\n const localesTableCheck = await db.execute(sql`\n SELECT EXISTS (\n SELECT FROM information_schema.tables\n WHERE table_schema = 'public'\n AND table_name = ${mainLocalesTable}\n ) as exists\n `)\n\n if (localesTableCheck.rows[0]?.exists) {\n // Check if _status column already exists in the locales table\n const statusColumnCheck = await db.execute(sql`\n SELECT EXISTS (\n SELECT FROM information_schema.columns\n WHERE table_schema = 'public'\n AND table_name = ${mainLocalesTable}\n AND column_name = '_status'\n ) as exists\n `)\n\n if (!statusColumnCheck.rows[0]?.exists) {\n // Add _status column to locales table\n await db.execute(sql`\n ALTER TABLE ${sql.identifier(mainLocalesTable)}\n ADD COLUMN _status VARCHAR DEFAULT 'draft'\n `)\n }\n\n // Now populate the _status values from the latest version\n if (collectionSlug) {\n await migrateMainCollectionStatus({\n collectionSlug,\n db,\n locales,\n payload,\n sql,\n versionsTable,\n })\n } else if (globalSlug) {\n await migrateMainGlobalStatus({ db, globalSlug, locales, payload, sql, versionsTable })\n }\n } else {\n payload.logger.info({\n msg: `No locales table found: ${mainLocalesTable} (collection/global not localized)`,\n })\n }\n\n // 5. Drop _status from main table if it exists (it will be in locales table now)\n const mainTableStatusCheck = await db.execute(sql`\n SELECT EXISTS (\n SELECT FROM information_schema.columns\n WHERE table_schema = 'public'\n AND table_name = ${mainTable}\n AND column_name = '_status'\n ) as exists\n `)\n\n if (mainTableStatusCheck.rows[0]?.exists) {\n await db.execute(sql`\n ALTER TABLE ${sql.identifier(mainTable)} DROP COLUMN _status\n `)\n }\n\n payload.logger.info({ msg: 'Migration completed successfully' })\n}\n"],"names":["calculateVersionLocaleStatuses","toSnakeCase","migrateMainCollectionStatus","migrateMainGlobalStatus","up","args","collectionSlug","db","globalSlug","payload","req","sql","Error","entitySlug","versionsTable","localesTable","config","localization","entityConfig","collection","collections","find","c","slug","global","globals","g","logger","info","msg","locales","localeCodes","filterAvailableLocales","filteredLocaleObjects","map","locale","code","join","versions","columnCheckResult","execute","rows","exists","localesTableCheckResult","localesTableExists","identifier","inserted","length","existingLocalesResult","existingLocales","row","_locale","versionsResult","versionLocaleStatus","updateCount","versionId","localeMap","entries","status","mainTable","mainLocalesTable","localesTableCheck","statusColumnCheck","mainTableStatusCheck"],"mappings":"AAEA,SAASA,8BAA8B,EAAEC,WAAW,QAAQ,eAAc;AAC1E,SAASC,2BAA2B,QAAQ,6BAA4B;AACxE,SAASC,uBAAuB,QAAQ,yBAAwB;AAWhE,OAAO,eAAeC,GAAGC,IAAwB;IAC/C,MAAM,EAAEC,cAAc,EAAEC,EAAE,EAAEC,UAAU,EAAEC,OAAO,EAAEC,GAAG,EAAEC,GAAG,EAAE,GAAGN;IAE9D,IAAI,CAACC,kBAAkB,CAACE,YAAY;QAClC,MAAM,IAAII,MAAM;IAClB;IAEA,IAAIN,kBAAkBE,YAAY;QAChC,MAAM,IAAII,MAAM;IAClB;IAEA,MAAMC,aAAaP,kBAAkBE;IACrC,sEAAsE;IACtE,MAAMM,gBAAgBR,iBAClB,CAAC,CAAC,EAAEL,YAAYK,gBAAgB,EAAE,CAAC,GACnC,CAAC,CAAC,EAAEL,YAAYO,YAAa,EAAE,CAAC;IACpC,MAAMO,eAAe,GAAGD,cAAc,QAAQ,CAAC;IAE/C,IAAI,CAACL,QAAQO,MAAM,CAACC,YAAY,EAAE;QAChC,MAAM,IAAIL,MAAM;IAClB;IAEA,0DAA0D;IAC1D,IAAIM;IACJ,IAAIZ,gBAAgB;QAClB,MAAMa,aAAaV,QAAQO,MAAM,C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U;;;EAGjC,CAAC;IAED,IAAII,qBAAqBtB,IAAI,CAAC,EAAE,EAAEC,QAAQ;QACxC,MAAMnC,GAAGiC,OAAO,CAAC7B,GAAG,CAAC;kBACP,EAAEA,IAAIkC,UAAU,CAACc,WAAW;IAC1C,CAAC;IACH;IAEAlD,QAAQkB,MAAM,CAACC,IAAI,CAAC;QAAEC,KAAK;IAAmC;AAChE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payload",
|
|
3
|
-
"version": "3.92.
|
|
3
|
+
"version": "3.92.1",
|
|
4
4
|
"description": "Node, React, Headless CMS and Application Framework built on Next.js",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"admin panel",
|
|
@@ -87,7 +87,7 @@
|
|
|
87
87
|
],
|
|
88
88
|
"dependencies": {
|
|
89
89
|
"@next/env": "^15.1.5",
|
|
90
|
-
"@payloadcms/translations": "3.92.
|
|
90
|
+
"@payloadcms/translations": "3.92.1",
|
|
91
91
|
"@types/busboy": "1.5.4",
|
|
92
92
|
"ajv": "8.18.0",
|
|
93
93
|
"bson-objectid": "2.0.4",
|