feat: support syncing database-seeds from local

This commit is contained in:
zkldi
2022-08-22 02:06:30 +01:00
parent 26ceaac910
commit b72ffeec57
4 changed files with 24 additions and 3 deletions
+2
View File
@@ -8,6 +8,8 @@
"start-server": "pnpm --filter ./server start",
"test-server": "pnpm --filter ./server test",
"test-dbseeds": "pnpm --filter ./database-seeds/scripts test",
"sync-database": "pnpm --filter ./server sync-database",
"sync-database-local": "pnpm --filter ./server sync-database-local",
"start-score-import-worker": "pnpm --filter ./server runscoreworker",
"start-docs": "cd docs/ && mkdocs serve"
},
+2 -1
View File
@@ -14,7 +14,8 @@
"start": "pnpm build && pnpm start-no-build",
"start-no-build": "node -r ts-node/register -r tsconfig-paths/register js/server/src/main.js",
"runscoreworker": "node -r ts-node/register -r tsconfig-paths/register js/server/src/lib/score-import/worker/worker.js",
"sync-database": "ts-node src/scripts/sync-database"
"sync-database": "ts-node src/scripts/sync-database",
"sync-database-local": "ts-node src/scripts/sync-database --localPath '../database-seeds'"
},
"author": "zkldi",
"license": "AGPL3",
+8 -1
View File
@@ -176,8 +176,15 @@ export class DatabaseSeedsRepo {
/**
* Pulls the database seeds from github, returns an object that can be used to manipulate them.
*
* @param fetchFromLocalPath - Whether or not to fetch this from a local instance, like a
* monorepo database-seeds directory.
*/
export async function PullDatabaseSeeds() {
export async function PullDatabaseSeeds(fetchFromLocalPath: string | null = null) {
if (fetchFromLocalPath) {
return new DatabaseSeedsRepo(fetchFromLocalPath);
}
if (!ServerConfig.SEEDS_CONFIG) {
throw new Error(`SEEDS_CONFIG was not defined. You cannot pull a seeds repo.`);
}
+12 -1
View File
@@ -1,6 +1,7 @@
/* eslint-disable no-await-in-loop */
// This script syncs this tachi instances database up with the tachi-database-seeds.
import { Command } from "commander";
import db, { monkDB } from "external/mongo/db";
import fjsh from "fast-json-stable-hash";
import { PullDatabaseSeeds } from "lib/database-seeds/repo";
@@ -13,6 +14,7 @@ import { RecalcAllScores } from "utils/calculations/recalc-scores";
import { UpdateGameSongIDCounter } from "utils/db";
import { InitaliseFolderChartLookup } from "utils/folder";
import { ArrayDiff, IsSupported, WrapScriptPromise } from "utils/misc";
import path from "path";
import type { KtLogger } from "lib/logger/logger";
import type { BulkWriteOperation, DeleteWriteOpResultObject } from "mongodb";
import type { ICollection } from "monk";
@@ -336,11 +338,20 @@ const syncInstructions: Array<SyncInstructions> = [
const logger = CreateLogCtx("Database Sync");
const program = new Command();
program.option("-l, --localPath <path/to/database-seeds>");
program.parse(process.argv);
const options: {
localPath: string | undefined;
} = program.opts();
async function SynchroniseDBWithSeeds() {
// Wait for mongo to connect first.
await monkDB.then(() => void 0);
const databaseSeedsRepo = await PullDatabaseSeeds();
const databaseSeedsRepo = await PullDatabaseSeeds(options.localPath);
for await (const { collectionName, data } of databaseSeedsRepo.IterateCollections()) {
const spawnLogger = CreateLogCtx(`${collectionName} Sync`);