diff --git a/backend/__tests__/__integration__/global-setup.ts b/backend/__tests__/__integration__/global-setup.ts index 75a4f2865c07..7af3683eec33 100644 --- a/backend/__tests__/__integration__/global-setup.ts +++ b/backend/__tests__/__integration__/global-setup.ts @@ -7,7 +7,9 @@ export async function setup(): Promise { 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()); diff --git a/backend/docker/compose.db-only.yml b/backend/docker/compose.db-only.yml index 04eb42be2a50..40ad02a849ec 100644 --- a/backend/docker/compose.db-only.yml +++ b/backend/docker/compose.db-only.yml @@ -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" diff --git a/backend/docker/compose.yml b/backend/docker/compose.yml index b7e4fc9193c7..53d2ce8be7a9 100644 --- a/backend/docker/compose.yml +++ b/backend/docker/compose.yml @@ -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" diff --git a/backend/package.json b/backend/package.json index a174a258b375..e53505ccd154 100644 --- a/backend/package.json +++ b/backend/package.json @@ -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", diff --git a/backend/src/init/db.ts b/backend/src/init/db.ts index 1e93ad0d6f7a..e7f746934255 100644 --- a/backend/src/init/db.ts +++ b/backend/src/init/db.ts @@ -57,6 +57,11 @@ export async function connect(): Promise { 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( diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index b9110260938d..d16c9d16f1cd 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -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 diff --git a/docs/SELF_HOSTING.md b/docs/SELF_HOSTING.md index 72a6e1416225..4adaaba041de 100644 --- a/docs/SELF_HOSTING.md +++ b/docs/SELF_HOSTING.md @@ -1,13 +1,8 @@ # Monkeytype Self Hosting - - -## Table of contents - - [Monkeytype Self Hosting](#monkeytype-self-hosting) - - [Table of contents](#table-of-contents) - [Prerequisites](#prerequisites) - [Quickstart](#quickstart) - [Account System](#account-system) @@ -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) @@ -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/ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1f175f14d29b..c50e75d0657c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -126,8 +126,8 @@ importers: specifier: 4.15.0 version: 4.15.0(encoding@0.1.13) mongodb: - specifier: 6.3.0 - version: 6.3.0(socks@2.8.3) + specifier: 6.20.0 + version: 6.20.0(socks@2.8.3) mustache: specifier: 4.2.0 version: 4.2.0 @@ -2379,8 +2379,8 @@ packages: '@mdn/browser-compat-data@5.7.6': resolution: {integrity: sha512-7xdrMX0Wk7grrTZQwAoy1GkvPMFoizStUoL+VmtUkAxegbCCec+3FKwOM6yc/uGU5+BEczQHXAlWiqvM8JeENg==} - '@mongodb-js/saslprep@1.1.8': - resolution: {integrity: sha512-qKwC/M/nNNaKUBMQ0nuzm47b7ZYWQHN3pcXq4IIcoSBc2hOIrflAxJduIvvqmhoz3gR2TacTAs8vlsCVPkiEdQ==} + '@mongodb-js/saslprep@1.3.2': + resolution: {integrity: sha512-QgA5AySqB27cGTXBFmnpifAi7HxoGUeezwo6p9dI03MuDB6Pp33zgclqVb6oVK3j6I9Vesg0+oojW2XxB59SGg==} '@msgpackr-extract/msgpackr-extract-darwin-arm64@3.0.3': resolution: {integrity: sha512-QZHtlVgbAdy2zAqNA9Gu1UpIuI8Xvsd1v8ic6B2pZmeFnFcMWiPLfWXh7TVw4eGEZ/C9TH281KwhVoeQUKbyjw==} @@ -3928,10 +3928,9 @@ packages: engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7} hasBin: true - bson@6.8.0: - resolution: {integrity: sha512-iOJg8pr7wq2tg/zSlCCHMi3hMm5JTOxLTagf3zxhcenHsFp+c6uOs6K7W5UE7A4QIJGtqh/ZovFNMP4mOPJynQ==} + bson@6.10.4: + resolution: {integrity: sha512-WIsKqkSC0ABoBJuT1LEX+2HEvNmNKKgnTAyd0fL8qzK4SH2i9NXg+t08YtdZp/V9IZ33cxe3iV4yM0qg8lMQng==} engines: {node: '>=16.20.1'} - deprecated: a critical bug affecting only useBigInt64=true deserialization usage is fixed in bson@6.10.3 buffer-crc32@1.0.0: resolution: {integrity: sha512-Db1SbgBS/fg/392AblrMJk97KggmvYhr4pB5ZIMTWtaivCPMWLkmb7m21cJvpvgK+J3nsU2CmmixNBZx4vFj/w==} @@ -7132,19 +7131,19 @@ packages: moment@2.30.1: resolution: {integrity: sha512-uEmtNhbDOrWPFS+hdjFCBfy9f2YoyzRpwcl+DqpC6taX21FzsTLQVbMV/W7PzNSX6x/bhC1zA3c2UQ5NzH6how==} - mongodb-connection-string-url@3.0.1: - resolution: {integrity: sha512-XqMGwRX0Lgn05TDB4PyG2h2kKO/FfWJyCzYQbIhXUxz7ETt0I/FqHjUeqj37irJ+Dl1ZtU82uYyj14u2XsZKfg==} + mongodb-connection-string-url@3.0.2: + resolution: {integrity: sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA==} - mongodb@6.3.0: - resolution: {integrity: sha512-tt0KuGjGtLUhLoU263+xvQmPHEGTw5LbcNC73EoFRYgSHwZt5tsoJC110hDyO1kjQzpgNrpdcSza9PknWN4LrA==} + mongodb@6.20.0: + resolution: {integrity: sha512-Tl6MEIU3K4Rq3TSHd+sZQqRBoGlFsOgNrH5ltAcFBV62Re3Fd+FcaVf8uSEQFOJ51SDowDVttBTONMfoYWrWlQ==} engines: {node: '>=16.20.1'} peerDependencies: '@aws-sdk/credential-providers': ^3.188.0 - '@mongodb-js/zstd': ^1.1.0 + '@mongodb-js/zstd': ^1.1.0 || ^2.0.0 gcp-metadata: ^5.2.0 kerberos: ^2.0.1 mongodb-client-encryption: '>=6.0.0 <7' - snappy: ^7.2.2 + snappy: ^7.3.2 socks: ^2.7.1 peerDependenciesMeta: '@aws-sdk/credential-providers': @@ -11880,7 +11879,7 @@ snapshots: '@mdn/browser-compat-data@5.7.6': {} - '@mongodb-js/saslprep@1.1.8': + '@mongodb-js/saslprep@1.3.2': dependencies: sparse-bitfield: 3.0.3 @@ -13592,7 +13591,7 @@ snapshots: node-releases: 2.0.26 update-browserslist-db: 1.1.4(browserslist@4.27.0) - bson@6.8.0: {} + bson@6.10.4: {} buffer-crc32@1.0.0: {} @@ -17577,16 +17576,16 @@ snapshots: moment@2.30.1: {} - mongodb-connection-string-url@3.0.1: + mongodb-connection-string-url@3.0.2: dependencies: '@types/whatwg-url': 11.0.5 whatwg-url: 13.0.0 - mongodb@6.3.0(socks@2.8.3): + mongodb@6.20.0(socks@2.8.3): dependencies: - '@mongodb-js/saslprep': 1.1.8 - bson: 6.8.0 - mongodb-connection-string-url: 3.0.1 + '@mongodb-js/saslprep': 1.3.2 + bson: 6.10.4 + mongodb-connection-string-url: 3.0.2 optionalDependencies: socks: 2.8.3