From a3150da8a2488833d482101ca7b08746a1a7c94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Thu, 25 Mar 2021 17:07:00 +0100 Subject: [PATCH] fix(migration): add game server model migration --- ...73-mark-all-game-servers-as-not-deleted.js | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 migrations/1616687902073-mark-all-game-servers-as-not-deleted.js diff --git a/migrations/1616687902073-mark-all-game-servers-as-not-deleted.js b/migrations/1616687902073-mark-all-game-servers-as-not-deleted.js new file mode 100644 index 000000000..b3dfd3ec8 --- /dev/null +++ b/migrations/1616687902073-mark-all-game-servers-as-not-deleted.js @@ -0,0 +1,37 @@ +/* eslint-disable @typescript-eslint/no-var-requires */ +'use strict' + +// +// Set deleted to false on all game servers that do not have the 'deleted' property. +// + +const { config } = require('dotenv'); +const { MongoClient } = require('mongodb'); + +module.exports.up = next => { + config(); + + let credentials = ''; + if (process.env.MONGODB_USERNAME) { + if (process.env.MONGODB_PASSWORD) { + credentials = `${process.env.MONGODB_USERNAME}:${process.env.MONGODB_PASSWORD}@`; + } else { + credentials = `${process.env.MONGODB_USERNAME}@`; + } + } + + const uri = `mongodb://${credentials}${process.env.MONGODB_HOST}:${process.env.MONGODB_PORT}/${process.env.MONGODB_DB}`; + + MongoClient.connect(uri, { useUnifiedTopology: true }) + .then(client => client.db()) + .then(db => db.collection('gameservers')) + .then(collection => collection.updateMany( + { + deleted: { $exists: false }, + }, + { + $set: { deleted: false }, + }, + )) + .then(() => next()); +}