mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-08 22:59:52 +03:00
feat: automatically apply new indexes on boot
This commit is contained in:
Vendored
+15
-35
@@ -1,5 +1,4 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
import db, { monkDB } from "./db";
|
||||
import { ONE_DAY } from "lib/constants/time";
|
||||
import CreateLogCtx from "lib/logger/logger";
|
||||
import { TachiConfig } from "lib/setup/config";
|
||||
@@ -22,6 +21,9 @@ function index(fields: Record<string, unknown>, options?: IndexOptions) {
|
||||
|
||||
const UNIQUE = { unique: true };
|
||||
|
||||
// NOTE: INDEXES REMOVED FROM HERE WILL **NOT** AUTOMATICALLY UPDATE.
|
||||
// YOU NEED TO RUN src/scripts/set-indexes.ts --reset TO REMOVE INDEXES.
|
||||
// (indexes added will automatically apply on tachi-server boot, though).
|
||||
const staticIndexes: Partial<Record<Databases, Array<Index>>> = {
|
||||
scores: [
|
||||
index({ scoreID: 1 }, UNIQUE),
|
||||
@@ -164,7 +166,7 @@ for (const game of TachiConfig.GAMES) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function SetIndexesWithDB(db: IMonkManager, reset: boolean) {
|
||||
export async function UpdateIndexes(db: IMonkManager, reset: boolean) {
|
||||
const collections = (await db.listCollections()).map((e) => e.name);
|
||||
|
||||
for (const [collection, values] of Object.entries(indexes)) {
|
||||
@@ -183,11 +185,17 @@ export async function SetIndexesWithDB(db: IMonkManager, reset: boolean) {
|
||||
}
|
||||
|
||||
for (const index of values) {
|
||||
// @ts-expect-error Type-mismatch here. our index.fields are just boring records. I know
|
||||
// that this sucks...
|
||||
const r = await db.get(collection).createIndex(index.fields, index.options);
|
||||
try {
|
||||
// @ts-expect-error Type-mismatch here. our index.fields are just boring records. I know
|
||||
// that this sucks...
|
||||
const r = await db.get(collection).createIndex(index.fields, index.options);
|
||||
|
||||
logger.debug(r);
|
||||
logger.debug(r);
|
||||
} catch (err) {
|
||||
logger.debug(err);
|
||||
|
||||
// index already exists.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,35 +205,7 @@ export async function SetIndexesWithDB(db: IMonkManager, reset: boolean) {
|
||||
export async function SetIndexes(mongoUrl: string, reset: boolean) {
|
||||
const monkDb = monk(mongoUrl);
|
||||
|
||||
await SetIndexesWithDB(monkDb, reset);
|
||||
await UpdateIndexes(monkDb, reset);
|
||||
|
||||
await monkDb.close();
|
||||
}
|
||||
|
||||
export function SetIndexesIfNoneSet() {
|
||||
// If no indexes are set, then we need to load mongo indexes.
|
||||
return db.users
|
||||
.indexes()
|
||||
.then((r) => {
|
||||
// If there's only one index on users
|
||||
// that means that only _id has indexes.
|
||||
// This means that there are likely to be no indexes
|
||||
// configured in the database.
|
||||
if (Object.keys(r).length === 1) {
|
||||
logger.info(
|
||||
`No indexes on users, First-time Tachi-Server startup assumed. Running SetIndexes.`
|
||||
);
|
||||
return SetIndexesWithDB(monkDB, true);
|
||||
}
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
logger.info(
|
||||
`Error in finding users collection. First time startup likely. Running SetIndexes.`,
|
||||
{ err }
|
||||
);
|
||||
return SetIndexesWithDB(monkDB, true);
|
||||
})
|
||||
.catch((err: unknown) => {
|
||||
logger.error(`Failed to set indexes on assumed first-time-setup?`, { err });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
/* eslint-disable no-await-in-loop */
|
||||
import { VERSION_INFO } from "lib/constants/version";
|
||||
import CreateLogCtx from "lib/logger/logger";
|
||||
import { Environment, ServerConfig } from "lib/setup/config";
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
// Before we run anything, set a global to indicate to the code that
|
||||
// we're running as a server, and not as a job runner or score worker.
|
||||
import db from "external/mongo/db";
|
||||
import { SetIndexesIfNoneSet } from "external/mongo/indexes";
|
||||
import { UpdateIndexes } from "external/mongo/indexes";
|
||||
import { InitSequenceDocs } from "external/mongo/sequence-docs";
|
||||
import { LoadDefaultClients } from "lib/builtin-clients/builtin-clients";
|
||||
import { VERSION_PRETTY } from "lib/constants/version";
|
||||
@@ -31,7 +31,7 @@ logger.info(`Loading sequence documents...`, { bootInfo: true });
|
||||
|
||||
async function RunOnInit() {
|
||||
await InitSequenceDocs();
|
||||
await SetIndexesIfNoneSet();
|
||||
await UpdateIndexes(db, false);
|
||||
|
||||
await ApplyUnappliedMigrations();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user