Skip to content

Commit

Permalink
Merge pull request #691 from CodeForAfrica/feature/upgrade-codeforafr…
Browse files Browse the repository at this point in the history
…ic-to-payload2

Migrate codeforafrica to payload v 2
  • Loading branch information
koechkevin authored May 14, 2024
2 parents e835d77 + 2409093 commit 8e7a9ca
Show file tree
Hide file tree
Showing 16 changed files with 1,094 additions and 3,006 deletions.
1 change: 0 additions & 1 deletion .github/workflows/codeforafrica-deploy-review-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ jobs:
- name: Push to dokku
uses: dokku/github-action@master
with:
# create a review app
command: review-apps:create
git_remote_url: ${{ env.DOKKU_REMOTE_URL }}/codeforafrica-ui
# specify a name for the review app
Expand Down
1 change: 1 addition & 0 deletions apps/codeforafrica/.env
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ NEXT_PUBLIC_IMAGE_UNOPTIMIZED="true"
NEXT_PUBLIC_VERCEL_URL=${VERCEL_URL}
NEXT_PUBLIC_APP_DIRECTORY="apps/codeforafrica/"
NEXT_PUBLIC_GOOGLE_ANALYTICS_ID="G-QVY4THBKNT"
MIGRATIONS_DIR=./migrations
103 changes: 103 additions & 0 deletions apps/codeforafrica/migrations/20240508_113513_versions_v1_v2.ts
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
}
8 changes: 7 additions & 1 deletion apps/codeforafrica/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"clean": "rm -rf .next .turbo node_modules"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.470.0",
"@aws-sdk/lib-storage": "^3.470.0",
"@commons-ui/core": "workspace:*",
"@commons-ui/next": "workspace:*",
"@emotion/cache": "^11.11.0",
Expand All @@ -43,9 +45,12 @@
"@mui/material": "^5.14.20",
"@mui/utils": "^5.14.20",
"@next/env": "~14.0.4",
"@payloadcms/bundler-webpack": "^1.0.6",
"@payloadcms/db-mongodb": "^1.4.0",
"@payloadcms/plugin-cloud-storage": "^1.1.1",
"@payloadcms/plugin-nested-docs": "^1.0.9",
"@payloadcms/plugin-seo": "^1.0.15",
"@payloadcms/richtext-slate": "^1.4.0",
"camelcase-keys": "^9.1.2",
"dotenv": "^16.3.1",
"express": "^4.18.2",
Expand All @@ -56,7 +61,7 @@
"next": "^14.1.1",
"next-seo": "^6.4.0",
"nodemailer-sendgrid": "^1.0.3",
"payload": "^1.15.8",
"payload": "^2.16.1",
"prop-types": "^15.8.1",
"qs": "^6.11.2",
"react": "^18.2.0",
Expand All @@ -72,6 +77,7 @@
"@commons-ui/testing-library": "workspace:*",
"@playwright/test": "^1.40.1",
"@svgr/webpack": "^8.1.0",
"@swc/core": "1.3.96",
"@types/express": "^4.17.21",
"@types/node": "^18.19.3",
"@types/react": "^18.2.43",
Expand Down
9 changes: 9 additions & 0 deletions apps/codeforafrica/payload.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import path from "path";

import { buildConfig } from "payload/config";
import { slateEditor } from "@payloadcms/richtext-slate";
import { mongooseAdapter } from "@payloadcms/db-mongodb";
import { webpackBundler } from "@payloadcms/bundler-webpack";
import { CollectionConfig, GlobalConfig } from "payload/types";
import { cloudStorage } from "@payloadcms/plugin-cloud-storage";
import dotenv from "dotenv";
Expand Down Expand Up @@ -54,6 +57,11 @@ const adapter = s3Adapter({

export default buildConfig({
serverURL: appURL,
editor: slateEditor({}),
db: mongooseAdapter({
url: process.env.MONGODB_URL,
migrationDir: process.env.MIGRATIONS_DIR,
}),
collections: [
Authors,
Donors,
Expand Down Expand Up @@ -95,6 +103,7 @@ export default buildConfig({
},
},
}),
bundler: webpackBundler(),
},
cors,
csrf,
Expand Down
1 change: 0 additions & 1 deletion apps/codeforafrica/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ const start = async (): Promise<void> => {
}
: undefined),
secret: process.env.PAYLOAD_SECRET,
mongoURL: process.env.MONGODB_URL,
express: app,
onInit: (initPayload) => {
initPayload.logger.info(`Payload Admin URL: ${payload.getAdminURL()}`);
Expand Down
10 changes: 7 additions & 3 deletions apps/codeforafrica/src/payload/blocks/Error.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import richText from "../fields/richText";

const Error = {
Expand All @@ -12,9 +14,11 @@ const Error = {
},
richText({
name: "subtitle",
admin: {
elements: ["link"],
},
editor: slateEditor({
admin: {
elements: ["link"],
},
}),
}),
],
};
Expand Down
12 changes: 8 additions & 4 deletions apps/codeforafrica/src/payload/blocks/Hero.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import image from "../fields/image";
import richText from "../fields/richText";

Expand All @@ -9,10 +11,12 @@ const Hero = {
richText({
name: "title",
required: true,
admin: {
elements: [],
leaves: ["bold"],
},
editor: slateEditor({
admin: {
elements: [],
leaves: ["bold"],
},
}),
}),
{
name: "messages",
Expand Down
3 changes: 3 additions & 0 deletions apps/codeforafrica/src/payload/blocks/MeetOurTeam.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import image from "../fields/image";
import linkGroup from "../fields/links/linkGroup";
import richText from "../fields/richText";
Expand All @@ -15,6 +17,7 @@ const MeetOurTeam = {
richText({
name: "description",
required: true,
editor: slateEditor({}),
}),
linkGroup({ overrides: { name: "action", label: "Action" } }),
image({
Expand Down
32 changes: 18 additions & 14 deletions apps/codeforafrica/src/payload/blocks/OurMission.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import richText from "../fields/richText";

const OurMission = {
Expand All @@ -18,20 +20,22 @@ const OurMission = {
richText({
name: "description",
required: true,
admin: {
elements: [
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"link",
"ol",
"ul",
"indent",
],
},
editor: slateEditor({
admin: {
elements: [
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"link",
"ol",
"ul",
"indent",
],
},
}),
}),
],
};
Expand Down
32 changes: 18 additions & 14 deletions apps/codeforafrica/src/payload/blocks/RichText.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import blockFields from "../fields/blockFields";
import richText from "../fields/richText";

Expand All @@ -10,20 +12,22 @@ const RichText = {
richText({
name: "content",
required: true,
admin: {
elements: [
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"link",
"ol",
"ul",
"indent",
],
},
editor: slateEditor({
admin: {
elements: [
"h1",
"h2",
"h3",
"h4",
"h5",
"h6",
"link",
"ol",
"ul",
"indent",
],
},
}),
}),
],
}),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import image from "../fields/image";
import richText from "../fields/richText";
import slug from "../fields/slug";
Expand Down Expand Up @@ -29,6 +31,7 @@ const GuidingPrinciples = {
name: "description",
required: true,
localized: true,
editor: slateEditor({}),
}),
],
};
Expand Down
3 changes: 3 additions & 0 deletions apps/codeforafrica/src/payload/collections/Impact.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import image from "../fields/image";
import richText from "../fields/richText";

Expand All @@ -23,6 +25,7 @@ const Impact = {
name: "description",
required: true,
localized: true,
editor: slateEditor({}),
}),
{
name: "value",
Expand Down
3 changes: 3 additions & 0 deletions apps/codeforafrica/src/payload/collections/Members.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import { allCountries } from "../../lib/data/json/countries";
import image from "../fields/image";
import richText from "../fields/richText";
Expand Down Expand Up @@ -50,6 +52,7 @@ const Members = {
name: "description",
required: true,
localized: true,
editor: slateEditor({}),
}),
socialLinks({
name: "connect",
Expand Down
3 changes: 3 additions & 0 deletions apps/codeforafrica/src/payload/collections/Partners.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

import image from "../fields/image";
import richText from "../fields/richText";
import slug from "../fields/slug";
Expand Down Expand Up @@ -48,6 +50,7 @@ const Partners = {
name: "description",
required: true,
localized: true,
editor: slateEditor(),
}),
socialLinks({
name: "connect",
Expand Down
Loading

0 comments on commit 8e7a9ca

Please sign in to comment.