Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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 backend/__tests__/__integration__/global-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ export async function setup(): Promise<void> {
process.env.TZ = "UTC";

//use testcontainer to start mongodb
const mongoContainer = new GenericContainer("mongo:5.0.13")
const mongoContainer = new GenericContainer(
"mongodb/mongodb-community-server:8.2.1-ubi8"
)
.withExposedPorts(27017)
.withWaitStrategy(Wait.forListeningPorts());

Expand Down
2 changes: 1 addition & 1 deletion backend/docker/compose.db-only.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:

mongodb:
container_name: monkeytype-mongodb
image: mongo:5.0.13
image: mongodb/mongodb-community-server:8.2.1-ubi8
restart: on-failure
ports:
- "${DOCKER_DB_PORT:-27017}:27017"
Expand Down
2 changes: 1 addition & 1 deletion backend/docker/compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:

mongodb:
container_name: monkeytype-mongodb
image: mongo:5.0.13
image: mongodb/mongodb-community-server:8.2.1-ubi8
restart: on-failure
ports:
- "${DOCKER_DB_PORT:-27017}:27017"
Expand Down
2 changes: 1 addition & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"lodash": "4.17.21",
"lru-cache": "7.10.1",
"mjml": "4.15.0",
"mongodb": "6.3.0",
"mongodb": "6.20.0",
"mustache": "4.2.0",
"nodemailer": "7.0.7",
"object-hash": "3.0.0",
Expand Down
5 changes: 5 additions & 0 deletions backend/src/init/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export async function connect(): Promise<void> {
try {
await mongoClient.connect();
db = mongoClient.db(DB_NAME);

const info = (await db.command({ buildInfo: 1 })) as { version: string };
if (info.version !== "8.2.1") {
throw new Error("Expected version 8.2.1 but got " + info.version);
}
} catch (error) {
Logger.error(getErrorMessage(error) ?? "Unknown error");
Logger.error(
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ services:

monkeytype-mongodb:
container_name: monkeytype-mongodb
image: mongo:5.0.13
image: mongodb/mongodb-community-server:8.2.1-ubi8
restart: on-failure
volumes:
- mongo-data:/data/db
Expand Down
220 changes: 215 additions & 5 deletions docs/SELF_HOSTING.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
# Monkeytype Self Hosting

<!-- TOC ignore:true -->

## Table of contents

<!-- TOC -->

- [Monkeytype Self Hosting](#monkeytype-self-hosting)
- [Table of contents](#table-of-contents)
- [Prerequisites](#prerequisites)
- [Quickstart](#quickstart)
- [Account System](#account-system)
Expand All @@ -20,6 +15,12 @@
- [env file](#env-file)
- [serviceAccountKey.json](#serviceaccountkeyjson)
- [backend-configuration.json](#backend-configurationjson)
- [Upgrades](#upgrades)
- [v.....](#v)
- [Backup](#backup)
- [Upgrade MongoDB with dump/restore](#upgrade-mongodb--with-dumprestore)
- [Upgrade MongoDB incremental](#upgrade-mongodb-incremental)
- [References](#references)

<!-- /TOC -->

Expand Down Expand Up @@ -190,3 +191,212 @@ Configuration of the backend. Check the [default configuration](https://github.c

> [!NOTE]
> Configuration changes are applied only on container startup. You must restart the container for your updates to take effect.


## Upgrades

### v.....

Starting with this version you will need to update the MongoDB container from 5.0 to 8.2. Follow the steps carefully.

#### Backup

Make sure the container is _STOPPED_ and run this command:

```
docker run --rm -v monkeytype_mongo_data:/from -v $(pwd):/backup alpine tar czf /backup/monkeytype-mongodb.tar.gz -C /from .
```

In case you need to restore the backup run this commands:

```
docker volume rm monkeytype_mongo_data
docker volume create monkeytype_mongo_data
docker run --rm -v monkeytype_mongo_data:/to -v $(pwd):/backup alpine tar xzf /backup/monkeytype-mongodb.tar.gz -C /to

```

There are two ways to update your MongoDB instance to 8.2. You can use the dump/restore method if your database is not very huge and you have the storage for the full database dump. If not follow the guide for [Upgrade MongoDB incremental](#upgrade-mongodb-incremental)

#### Upgrade MongoDB with dump/restore

Make sure the container is _STOPPED_ and execute this command:

```
docker run -d \
--name mongodb-upgrade \
-v monkeytype_mongo_data:/data/db \
-v $(pwd):/backup \
-p 27017:27017 \
mongo:5.0.13
```

Then dump the data using

```
docker exec -it mongodb-upgrade \
mongodump --db=monkeytype --out=/backup
```

And stop the container again:

```
docker stop mongodb-upgrade
docker rm mongodb-upgrade
```

Then replace the volume with an empty one:

```
docker volume rm monkeytype_mongo_data
docker volume create monkeytype_mongo_data
```

Now start a new container with version 8.2

```
docker run -d \
--name mongodb-upgrade \
-v monkeytype_mongo_data:/data/db \
-v $(pwd):/backup \
-p 27017:27017 \
mongodb/mongodb-community-server:8.2.1-ubi8
```


Then restore the data using

```
docker exec -it mongodb-upgrade \
mongorestore --host=localhost --port=27017 \
--db=monkeytype \
--drop \
/backup/monkeytype
```

And stop the container again:

```
docker stop mongodb-upgrade
docker rm mongodb-upgrade
```

At this point the MongoDB container is upgraded to version 8.2.

Now you can start your instance with `docker compose` as normal.

#### Upgrade MongoDB incremental

MongoDB needs to be upgraded for each major version. Don't skip any of theese steps:

Make sure the container is _STOPPED_ and run this command:

```
docker run -d \
--name mongodb-upgrade \
-v monkeytype_mongo_data:/data/db \
-p 27017:27017 \
mongo:6.0
```

When the container is _HEALTHY_ run this command:

```
docker exec -it mongodb-upgrade mongosh --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "6.0" } )'
```

Wait a few seconds and execute:

```
docker stop mongodb-upgrade
docker rm mongodb-upgrade
```

At this point the MongoDB container is upgraded to version 6.0.

Make sure the container is _STOPPED_ and run this command:

```
docker run -d \
--name mongodb-upgrade \
-v monkeytype_mongo_data:/data/db \
-p 27017:27017 \
mongo:7.0
```

When the container is _HEALTHY_ run this command:

```
docker exec -it mongodb-upgrade mongosh --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "7.0", confirm: true } )'
```

Wait a few seconds and execute:

```
docker stop mongodb-upgrade
docker rm mongodb-upgrade
```

At this point the MongoDB container is upgraded to version 7.0.

Make sure the container is _STOPPED_ and run this command:

```
docker run -d \
--name mongodb-upgrade \
-v monkeytype_mongo_data:/data/db \
-p 27017:27017 \
mongo:8.0
```

When the container is _HEALTHY_ run this command:

```
docker exec -it mongodb-upgrade mongosh --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "8.0", confirm: true } )'
```

Wait a few seconds and execute:

```
docker stop mongodb-upgrade
docker rm mongodb-upgrade
```

At this point the MongoDB container is upgraded to version 8.0.

Make sure the container is _STOPPED_ and run this command:

```
docker run --rm -v monkeytype_mongo_data:/data/db alpine chown -R 998:996 /data/db
```

```
docker run -d \
--name mongodb-upgrade \
-v monkeytype_mongo_data:/data/db \
-p 27017:27017 \
mongodb/mongodb-community-server:8.2.1-ubi8
```

When the container is _HEALTHY_ run this command:

```
docker exec -it mongodb-upgrade mongosh --eval 'db.adminCommand( { setFeatureCompatibilityVersion: "8.2", confirm: true } )'
```

Wait a few seconds and execute:

```
docker stop mongodb-upgrade
docker rm mongodb-upgrade
```

At this point the MongoDB container is upgraded to version 8.2.

Now you can start your instance with `docker compose` as normal.

#### References
- https://www.mongodb.com/docs/v6.0/release-notes/6.0-upgrade-standalone/
- https://www.mongodb.com/docs/v7.0/release-notes/7.0-upgrade-standalone/
- https://www.mongodb.com/docs/v8.0/release-notes/8.0-upgrade-standalone/#std-label-8.0-upgrade-standalone
- https://www.mongodb.com/docs/manual/release-notes/8.2-upgrade-standalone/
39 changes: 19 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.