-
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 #670 from CodeForAfrica/feature/upgrade-payload-to-v2
Migrate payload version 2
- Loading branch information
Showing
37 changed files
with
2,796 additions
and
325 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Makefile | ||
|
||
.PHONY: charterafrica mongodb mongodb-keyfile | ||
|
||
charterafrica: | ||
docker compose --env-file apps/charterafrica/.env.local up charterafrica --build -d | ||
|
||
mongodb: | ||
docker compose --env-file apps/charterafrica/.env.local up --wait mongodb | ||
|
||
mongodb-keyfile: | ||
openssl rand -base64 741 > ./mongo-keyfile | ||
chmod 600 ./mongo-keyfile | ||
|
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
61 changes: 0 additions & 61 deletions
61
apps/charterafrica/migrations/20230717085545-update_blocks_rename_collection_to_item.js
This file was deleted.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
apps/charterafrica/migrations/20240111_075951_update_blocks_rename_collection_to_items.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,36 @@ | ||
import { MigrateUpArgs, MigrateDownArgs } from "@payloadcms/db-mongodb"; | ||
|
||
export async function up({ payload }: MigrateUpArgs): Promise<void> { | ||
const { db } = payload; | ||
const collection = db.collections["pages"]; | ||
const result = await collection.updateMany( | ||
{ "blocks.collection": { $exists: true } }, | ||
{ | ||
$set: { | ||
"blocks.$.items": "$blocks.$.collection", | ||
}, | ||
$unset: { | ||
"blocks.$.collection": "", | ||
}, | ||
}, | ||
); | ||
|
||
console.log(`${result.modifiedCount} documents updated.`); | ||
} | ||
|
||
export async function down({ payload }: MigrateDownArgs): Promise<void> { | ||
const { db } = payload; | ||
const collection = db.collections["pages"]; | ||
const result = await collection.updateMany( | ||
{ "blocks.items": { $exists: true } }, | ||
{ | ||
$set: { | ||
"blocks.$.collection": "$blocks.$.items", | ||
}, | ||
$unset: { | ||
"blocks.$.items": "", | ||
}, | ||
}, | ||
); | ||
console.log(`${result.modifiedCount} documents updated.`); | ||
} |
103 changes: 103 additions & 0 deletions
103
apps/charterafrica/migrations/20240118_131136_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
Oops, something went wrong.