Merge pull request #571 from TNG-dev:zkldi/issue-569

Add an IsPrimary update script, and make it run if charts change.
This commit is contained in:
zkldi
2021-12-09 00:39:53 +00:00
committed by GitHub
3 changed files with 70 additions and 0 deletions
@@ -0,0 +1,3 @@
import UpdateIsPrimaryStatus from "lib/score-mutation/update-isprimary";
UpdateIsPrimaryStatus().then(() => process.exit(0));
@@ -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();
}
},
},
@@ -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,
// },
// },
// ]);