From f252b8093e2f7844a2182f56463b2b0f3c204ab3 Mon Sep 17 00:00:00 2001 From: zkldi <20380519+zkldi@users.noreply.github.com> Date: Thu, 9 Dec 2021 00:37:18 +0000 Subject: [PATCH] Add an IsPrimary update script. --- .../scripts/single-use/set-isprimary-false.ts | 3 + server/scripts/sync-database/sync-database.ts | 2 + .../lib/score-mutation/update-isprimary.ts | 65 +++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 server/scripts/single-use/set-isprimary-false.ts create mode 100644 server/src/lib/score-mutation/update-isprimary.ts diff --git a/server/scripts/single-use/set-isprimary-false.ts b/server/scripts/single-use/set-isprimary-false.ts new file mode 100644 index 000000000..ae2670934 --- /dev/null +++ b/server/scripts/single-use/set-isprimary-false.ts @@ -0,0 +1,3 @@ +import UpdateIsPrimaryStatus from "lib/score-mutation/update-isprimary"; + +UpdateIsPrimaryStatus().then(() => process.exit(0)); diff --git a/server/scripts/sync-database/sync-database.ts b/server/scripts/sync-database/sync-database.ts index 26e2d0afc..c9ba0bf6b 100644 --- a/server/scripts/sync-database/sync-database.ts +++ b/server/scripts/sync-database/sync-database.ts @@ -6,6 +6,7 @@ import { monkDB } from "external/mongo/db"; import fjsh from "fast-json-stable-hash"; import fs from "fs"; import CreateLogCtx, { KtLogger } from "lib/logger/logger"; +import UpdateIsPrimaryStatus from "lib/score-mutation/update-isprimary"; import { TachiConfig } from "lib/setup/config"; import { BulkWriteOperation } from "mongodb"; import { ICollection } from "monk"; @@ -128,6 +129,7 @@ const syncInstructions: SyncInstructions[] = [ if (r) { await InitaliseFolderChartLookup(); + await UpdateIsPrimaryStatus(); } }, }, diff --git a/server/src/lib/score-mutation/update-isprimary.ts b/server/src/lib/score-mutation/update-isprimary.ts new file mode 100644 index 000000000..f14cdc6fc --- /dev/null +++ b/server/src/lib/score-mutation/update-isprimary.ts @@ -0,0 +1,65 @@ +/* eslint-disable no-await-in-loop */ +import db from "external/mongo/db"; +import { TachiConfig } from "lib/setup/config"; + +export default async function UpdateIsPrimaryStatus() { + for (const game of TachiConfig.GAMES) { + const chartIDs = ( + await db.charts[game].find({ + isPrimary: false, + }) + ).map((e) => e.chartID); + + await db.scores.update( + { + chartID: { $in: chartIDs }, + }, + { $set: { isPrimary: false } } + ); + + await db["personal-bests"].update( + { + chartID: { $in: chartIDs }, + }, + { $set: { isPrimary: false } } + ); + } +} + +// The easier way to do this is just to always update the isPrimary status +// Left, for posterities sake, is an incredibly sub-optimal aggregate that +// achieves this also. +// +// const scores = await db.scores.aggregate([ +// { +// $match: { +// game: "iidx", +// }, +// }, +// { +// $lookup: { +// from: "charts-iidx", +// localField: "chartID", +// foreignField: "chartID", +// as: "chart", +// }, +// }, +// { +// $unwind: { +// path: "$chart", +// }, +// }, +// // This is the best way to do $match $ne, according to SO. +// { +// $addFields: { +// needsUpdate: { +// $ne: ["$chart.isPrimary", "$isPrimary"], +// }, +// }, +// }, +// { +// $match: { +// needsUpdate: true, +// }, +// }, +// ]);