-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #691 from CodeForAfrica/feature/upgrade-codeforafr…
…ic-to-payload2 Migrate codeforafrica to payload v 2
- Loading branch information
Showing
16 changed files
with
1,094 additions
and
3,006 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
apps/codeforafrica/migrations/20240508_113513_versions_v1_v2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { MigrateUpArgs, MigrateDownArgs } from "@payloadcms/db-mongodb"; | ||
|
||
export async function up({ payload }: MigrateUpArgs): Promise<void> { | ||
async function migrateCollectionDocs(slug: string, docsAtATime = 100) { | ||
const VersionsModel = payload.db.versions[slug]; | ||
const remainingDocs = await VersionsModel.aggregate( | ||
[ | ||
// Sort so that newest are first | ||
{ | ||
$sort: { | ||
updatedAt: -1, | ||
}, | ||
}, | ||
// Group by parent ID | ||
// take the $first of each | ||
{ | ||
$group: { | ||
_id: "$parent", | ||
_versionID: { $first: "$_id" }, | ||
createdAt: { $first: "$createdAt" }, | ||
latest: { $first: "$latest" }, | ||
updatedAt: { $first: "$updatedAt" }, | ||
version: { $first: "$version" }, | ||
}, | ||
}, | ||
{ | ||
$match: { | ||
latest: { $eq: null }, | ||
}, | ||
}, | ||
{ | ||
$limit: docsAtATime, | ||
}, | ||
], | ||
{ | ||
allowDiskUse: true, | ||
}, | ||
).exec(); | ||
|
||
if (!remainingDocs || remainingDocs.length === 0) { | ||
const newVersions = await VersionsModel.find({ | ||
latest: { | ||
$eq: true, | ||
}, | ||
}); | ||
|
||
if (newVersions?.length) { | ||
payload.logger.info( | ||
`Migrated ${newVersions.length} documents in the "${slug}" versions collection.`, | ||
); | ||
} | ||
|
||
return; | ||
} | ||
|
||
const remainingDocIds = remainingDocs.map((doc) => doc._versionID); | ||
|
||
await VersionsModel.updateMany( | ||
{ | ||
_id: { | ||
$in: remainingDocIds, | ||
}, | ||
}, | ||
{ | ||
latest: true, | ||
}, | ||
); | ||
|
||
await migrateCollectionDocs(slug); | ||
} | ||
|
||
// For each collection | ||
await Promise.all( | ||
payload.config.collections.map(async ({ slug, versions }) => { | ||
if (versions?.drafts) { | ||
return migrateCollectionDocs(slug); | ||
} | ||
}), | ||
); | ||
|
||
// For each global | ||
await Promise.all( | ||
payload.config.globals.map(async ({ slug, versions }) => { | ||
if (versions) { | ||
const VersionsModel = payload.db.versions[slug]; | ||
|
||
await VersionsModel.findOneAndUpdate( | ||
{}, | ||
{ latest: true }, | ||
{ | ||
sort: { updatedAt: -1 }, | ||
}, | ||
).exec(); | ||
|
||
payload.logger.info(`Migrated the "${slug}" global.`); | ||
} | ||
}), | ||
); | ||
} | ||
|
||
export async function down({ payload }: MigrateDownArgs): Promise<void> { | ||
// Migration code | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.