Migrate goals and user goals (no ID migration, yet)

This commit is contained in:
zkldi
2021-05-07 17:26:38 +01:00
parent 81159b3f61
commit 2d67d79c35
5 changed files with 115 additions and 3 deletions
+3 -3
View File
@@ -62,7 +62,7 @@ dependencies:
express-async-errors: 3.1.1_express@4.17.1
express-session: 1.17.1
fast-json-stable-hash: 1.0.1
kamaitachi-common: github.com/zkldi/Kamaitachi-common/927113e61ae262678e1890df8c03d7491ccd4ee6
kamaitachi-common: github.com/zkldi/Kamaitachi-common/b63c3213ba997b06d87f72e8dc18dde061502e6c
mongodb: 3.6.6
monk: 7.3.4
multer: 1.4.2
@@ -4638,8 +4638,8 @@ packages:
'@types/yoga-layout': 1.9.2
dev: true
github.com/zkldi/Kamaitachi-common/927113e61ae262678e1890df8c03d7491ccd4ee6:
resolution: {tarball: https://codeload.github.com/zkldi/Kamaitachi-common/tar.gz/927113e61ae262678e1890df8c03d7491ccd4ee6}
github.com/zkldi/Kamaitachi-common/b63c3213ba997b06d87f72e8dc18dde061502e6c:
resolution: {tarball: https://codeload.github.com/zkldi/Kamaitachi-common/tar.gz/b63c3213ba997b06d87f72e8dc18dde061502e6c}
name: kamaitachi-common
version: 0.1.0
dependencies:
@@ -0,0 +1,78 @@
// oh boy
import { GoalDocument } from "kamaitachi-common";
import db from "../../src/db/db";
import CreateLogCtx from "../../src/logger";
import MigrateRecords from "./migrate";
const logger = CreateLogCtx("goals.ts");
function ConvertFn(c: any): GoalDocument | null {
let charts: GoalDocument["charts"];
if (c.directChartID) {
charts = {
type: "single",
data: c.directChartID,
};
} else if (c.directChartIDs) {
charts = {
type: "multi",
data: c.directChartIDs,
};
} else if (c.chartQuery) {
logger.warn(`Skipped goal ${c.title}, as it used cQuery.`);
return null;
} else {
logger.warn(`Any Goal ${c.goalID}, ${c.title}. Skipping.`);
return null;
}
let partialCriteria: Partial<GoalDocument["criteria"]> = {
mode: "single",
};
if (c.scoreQuery["scoreData¬lampIndex"]) {
partialCriteria.key = "scoreData.lampIndex";
} else if (c.scoreQuery["scoreData¬gradeIndex"]) {
partialCriteria.key = "scoreData.gradeIndex";
} else if (c.scoreQuery["scoreData¬score"]) {
partialCriteria.key = "scoreData.score";
} else if (c.scoreQuery["scoreData¬percent"]) {
partialCriteria.key = "scoreData.percent";
} else {
throw new Error(`Invalid goal ${c.goalID} ${c.title}.`);
}
const v = c.scoreQuery[partialCriteria.key.replace(/\./gu, "¬")];
const value = v ? v["~gte"] : undefined;
// would break for value == 0, but a goal would never be
// zero.
if (!value) {
throw new Error(`Invalid goal ${c.goalID} ${c.title}.`);
}
partialCriteria.value = value;
// @ts-expect-error ts cant resolve this union properly
const newGoalDoc: GoalDocument = {
game: c.game,
playtype: c.playtype,
title: c.title,
timeAdded: c.timeAdded,
goalID: c.goalID, // maybe
createdBy: c.createdBy,
charts,
criteria: partialCriteria as GoalDocument["criteria"],
};
return newGoalDoc;
}
(async () => {
await MigrateRecords(db.goals, "goals", ConvertFn);
process.exit(0);
})();
@@ -0,0 +1,26 @@
// oh boy
import { UserGoalDocument } from "kamaitachi-common";
import db from "../../src/db/db";
import CreateLogCtx from "../../src/logger";
import MigrateRecords from "./migrate";
const logger = CreateLogCtx("user-goals.ts");
async function ConvertFn(c: any): Promise<UserGoalDocument | null> {
// const newUG: UserGoalDocument = {};
let relatedGoal = await db.goals.findOne({ goalID: c.goalID });
if (!relatedGoal) {
return null;
}
return c;
}
(async () => {
await MigrateRecords(db["user-goals"], "user-goals", ConvertFn);
process.exit(0);
})();
+2
View File
@@ -53,6 +53,8 @@ const indexes: Partial<Record<ValidDatabases, Index[]>> = {
index({ chartID: 1, tierlistID: 1, type: 1 }),
index({ chartID: 1, tierlistID: 1, type: 1, key: 1 }, UNIQUE),
],
goals: [index({ goalID: 1 }, UNIQUE)],
"user-goals": [index({ goalID: 1, userID: 1 }, UNIQUE), index({ goalID: 1 })],
};
(async () => {
+6
View File
@@ -0,0 +1,6 @@
{
"extends": "../tsconfig.json",
"include": [
"./**/*.ts",
],
}