Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate payload version 2 #670

Merged
merged 44 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
09c68de
Migrate payload version 2
koechkevin Jan 9, 2024
b3f8cab
Fix styles
koechkevin Jan 9, 2024
594c216
update migrations
koechkevin Jan 10, 2024
4bbe19f
Fix linting issues
koechkevin Jan 10, 2024
a4fae5f
Update old migrations to current versions
koechkevin Jan 11, 2024
e711ddd
Fix lint issues
koechkevin Jan 11, 2024
d874f78
Fix failing migrations
koechkevin Jan 15, 2024
1fc6329
Update old migrations
koechkevin Jan 15, 2024
636df83
Merge branch 'main' into feature/upgrade-payload-to-v2
koechkevin Jan 15, 2024
6fc4642
FIX: Test build phase of migrations in docker compose
koechkevin Jan 17, 2024
409f981
Fix lint issues
koechkevin Jan 17, 2024
bfc98f5
Remove unneeded updates
koechkevin Jan 18, 2024
4293694
Add docker config
koechkevin Jan 18, 2024
1f6dd0a
Reorder migratons
koechkevin Jan 18, 2024
4e862f1
Fix linting issues
koechkevin Jan 18, 2024
ae001a9
Remove unnecessary dependancy
koechkevin Jan 19, 2024
fa4d1ad
Fix lint issues
koechkevin Jan 19, 2024
eecbaa8
Use Make file
koechkevin Jan 19, 2024
f7e2bbc
Fix config lint
koechkevin Jan 19, 2024
55ae04e
Init db on start up
koechkevin Jan 22, 2024
90bef53
Remove log
koechkevin Jan 22, 2024
facb1e8
Rename mongo container, move volume mappings to contrib/docker
koechkevin Jan 22, 2024
ba52dbf
Use Environment variables for mongdb
koechkevin Jan 23, 2024
c9fe79b
Update editor config
koechkevin Jan 23, 2024
1fabff3
Remove env file
koechkevin Jan 23, 2024
7a71ab6
Make runtime env file a variable
koechkevin Jan 24, 2024
4241b0b
Update README.md to include db setup
kelvinkipruto Jan 24, 2024
de9f560
Fix mongofile permissions
kelvinkipruto Jan 24, 2024
a491e08
UPDATE: We are using an image for MongoDB so we shouldn't need build.
koechkevin Jan 24, 2024
db6ffe2
Switch to using healthcheck to initialise cluster
kilemensi Jan 26, 2024
4150435
Ensure healtchecks run when running mongodb
kilemensi Jan 26, 2024
ae8b75b
Remove unnecessary data directory binding
kilemensi Jan 26, 2024
f24289f
Ensure mongodb is health before starting codeforafrica
kilemensi Jan 26, 2024
8a13afc
FEAT: Update Docker compose
koechkevin Jan 26, 2024
3f3871d
Update docker-compose
koechkevin Jan 26, 2024
7790c3e
Working Build
kelvinkipruto Jan 29, 2024
43b982d
Remove as any typings
kelvinkipruto Jan 29, 2024
b5ff9b6
Upgrade payload dependencies
kelvinkipruto Jan 29, 2024
524f509
Fix Formatting
kelvinkipruto Jan 29, 2024
92862de
Remove unused dependencies
kelvinkipruto Jan 29, 2024
72e7c1e
Use env vars for dbInit.js
koechkevin Feb 5, 2024
ce8ce30
Remove dbInit now that we're creating the cluster in healthcheck
koechkevin Feb 6, 2024
2d5ad13
Update Readme with new file
koechkevin Feb 6, 2024
4da8946
Update Readme with commands to start the database server.
koechkevin Feb 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ apps/promisetracker/public/data/**
!apps/promisetracker/public/data/README.md

# Storybook
storybook-static
storybook-static

mongo-keyfile
14 changes: 14 additions & 0 deletions Makefile
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

2 changes: 1 addition & 1 deletion apps/charterafrica/.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ NEXT_PUBLIC_SEO_DISABLED=true

PAYLOAD_PUBLIC_LOCALES="en, fr, pt"
PAYLOAD_PUBLIC_DEFAULT_LOCALE=en

MIGRATIONS_DIR=./migrations
SENTRY_ENV=local
18 changes: 17 additions & 1 deletion apps/charterafrica/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@ and modify the `.env.local` file according to your needs.

The default `.env` file is for the publicly visible environment variables.
**DO NOT** include any secrets in it. All secrets should go into `env.local`.
For more, see <<nextjs env var docs>>
For more, see NextJS env var docs [here](https://nextjs.org/docs/basic-features/environment-variables).

## Database setup

Generate a new MongoDB keyfile by running the following command:
NB: Run this command on the root directory

```bash
make mongodb-keyfile
```

Start the database server:
NB: Run this command on the root directory

```bash
make mongodb
```

Then run the development server:

Expand Down

This file was deleted.

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 apps/charterafrica/migrations/20240118_131136_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
}
13 changes: 8 additions & 5 deletions apps/charterafrica/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@
"@nivo/pie": "^0.84.0",
"@nivo/tooltip": "^0.84.0",
"@nivo/waffle": "^0.84.0",
"@payloadcms/plugin-cloud-storage": "^1.1.1",
"@payloadcms/plugin-nested-docs": "^1.0.9",
"@payloadcms/plugin-seo": "^1.0.15",
"@payloadcms/bundler-webpack": "^1.0.6",
"@payloadcms/db-mongodb": "^1.4.0",
"@payloadcms/plugin-cloud-storage": "^1.1.2",
"@payloadcms/plugin-nested-docs": "^1.0.11",
"@payloadcms/plugin-seo": "^2.2.0",
"@payloadcms/richtext-slate": "^1.4.0",
"@react-spring/web": "^9.7.3",
"@sentry/nextjs": "^7.86.0",
"airtable": "^0.12.2",
Expand All @@ -65,7 +68,7 @@
"next": "^14.0.4",
"next-seo": "^6.4.0",
"nodemailer-sendgrid": "^1.0.3",
"payload": "^1.15.8",
"payload": "^2.9.0",
"prop-types": "^15.8.1",
"qs": "^6.11.2",
"react": "^18.2.0",
Expand All @@ -74,7 +77,7 @@
"react-share": "^5.0.3",
"react-swipeable-views-react-18-fix": "^0.14.1",
"scheduler": "^0.23.0",
"sharp": "^0.33.0",
"sharp": "^0.33.2",
"slate": "^0.101.1",
"swr": "^2.2.4",
"video.js": "^8.6.1",
Expand Down
15 changes: 14 additions & 1 deletion apps/charterafrica/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import nestedDocs from "@payloadcms/plugin-nested-docs";
import seo from "@payloadcms/plugin-seo";
import dotenv from "dotenv";
import { buildConfig } from "payload/config";
import { slateEditor } from "@payloadcms/richtext-slate";
import { mongooseAdapter } from "@payloadcms/db-mongodb";
import { webpackBundler } from "@payloadcms/bundler-webpack";

import Authors from "./src/payload/collections/Authors";
import CommunityPlatforms from "./src/payload/collections/CommunityPlatforms";
Expand Down Expand Up @@ -73,6 +76,14 @@ const adapter = s3Adapter({

export default buildConfig({
serverURL: appURL,
editor: slateEditor({}),
routes: {
admin: "/admin",
},
db: mongooseAdapter({
url: process.env.MONGO_URL,
migrationDir: process.env.MIGRATIONS_DIR,
}),
collections: [
Authors,
CommunityPlatforms,
Expand Down Expand Up @@ -110,6 +121,7 @@ export default buildConfig({
},
}
: undefined),

admin: {
webpack: (config) => ({
...config,
Expand All @@ -123,6 +135,7 @@ export default buildConfig({
},
},
}),
bundler: webpackBundler(),
},
cors,
csrf,
Expand Down Expand Up @@ -170,7 +183,7 @@ export default buildConfig({
generateTitle: ({ doc }: any) => doc?.title?.value as string,
generateURL: ({ doc, locale }: any) =>
doc?.slug?.value ? `${appURL}/${locale}/${doc.slug.value}` : undefined,
} as any),
}),
nestedDocs({
collections: ["pages"],
generateLabel: (_, doc) => doc.title as string,
Expand Down
1 change: 0 additions & 1 deletion apps/charterafrica/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ const start = async (): Promise<void> => {
}
: undefined),
secret: process.env.PAYLOAD_SECRET_KEY,
mongoURL: process.env.MONGO_URL,
express: server,
onInit: (initPayload) => {
initPayload.logger.info(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { slateEditor } from "@payloadcms/richtext-slate";

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

Expand All @@ -11,10 +13,22 @@ const HelpdeskPageContent = {
fr: "La description",
pt: "Descrição",
},
admin: {
elements: ["h2", "h3", "h4", "h5", "h6", "link", "ol", "ul", "indent"],
leaves: ["bold", "code", "italic", "strikethrough", "underline"],
},
editor: slateEditor({
admin: {
elements: [
"h2",
"h3",
"h4",
"h5",
"h6",
"link",
"ol",
"ul",
"indent",
],
leaves: ["bold", "code", "italic", "strikethrough", "underline"],
},
}),
localized: true,
}),
linkGroup(),
Expand Down
Loading
Loading