Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
54 lines
1.9 KiB
Plaintext
54 lines
1.9 KiB
Plaintext
import { toSnakeCase } from '../shared.js';
|
|
/**
|
|
* Migrates main global document from _status to per-locale status object
|
|
*/ export async function migrateMainGlobalStatus({ db, globalSlug, locales, payload, sql, versionsTable }) {
|
|
const globalTable = toSnakeCase(globalSlug);
|
|
const globalLocalesTable = `${globalTable}_locales`;
|
|
payload.logger.info({
|
|
msg: `Migrating main global locales for: ${globalLocalesTable}`
|
|
});
|
|
// For each locale, get the latest version status
|
|
for (const locale of locales){
|
|
const latestVersionStatus = await db.execute({
|
|
drizzle: db.drizzle,
|
|
sql: sql`
|
|
SELECT l.version__status as _status
|
|
FROM ${sql.identifier(versionsTable)} v
|
|
JOIN ${sql.raw(`${versionsTable}_locales`)} l ON l._parent_id = v.id
|
|
WHERE l._locale = ${locale}
|
|
ORDER BY v.created_at DESC
|
|
LIMIT 1
|
|
`
|
|
});
|
|
const status = latestVersionStatus.rows[0]?._status || 'draft';
|
|
// Get the global document ID from the globals table
|
|
const globalDoc = await db.execute({
|
|
drizzle: db.drizzle,
|
|
sql: sql`
|
|
SELECT id FROM ${sql.identifier(globalTable)} LIMIT 1
|
|
`
|
|
});
|
|
if (globalDoc.rows.length === 0) {
|
|
payload.logger.warn({
|
|
msg: `No global document found for ${globalSlug}, skipping`
|
|
});
|
|
continue;
|
|
}
|
|
const globalId = globalDoc.rows[0].id;
|
|
// Update the global's locales table with this status
|
|
await db.execute({
|
|
drizzle: db.drizzle,
|
|
sql: sql`
|
|
UPDATE ${sql.identifier(globalLocalesTable)}
|
|
SET _status = ${status}
|
|
WHERE _parent_id = ${globalId}
|
|
AND _locale = ${locale}
|
|
`
|
|
});
|
|
}
|
|
payload.logger.info({
|
|
msg: 'Migrated global document'
|
|
});
|
|
}
|
|
|
|
//# sourceMappingURL=migrateMainGlobal.js.map |