diff --git a/package.json b/package.json index 27aa3ec2a..98efdf106 100644 --- a/package.json +++ b/package.json @@ -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" }, diff --git a/server/package.json b/server/package.json index df767c4ca..3cf9f4a02 100644 --- a/server/package.json +++ b/server/package.json @@ -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", diff --git a/server/src/lib/database-seeds/repo.ts b/server/src/lib/database-seeds/repo.ts index 799ebbfa2..369d452fc 100644 --- a/server/src/lib/database-seeds/repo.ts +++ b/server/src/lib/database-seeds/repo.ts @@ -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.`); } diff --git a/server/src/scripts/sync-database.ts b/server/src/scripts/sync-database.ts index 0061411c3..d9840d5fd 100644 --- a/server/src/scripts/sync-database.ts +++ b/server/src/scripts/sync-database.ts @@ -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 = [ const logger = CreateLogCtx("Database Sync"); +const program = new Command(); + +program.option("-l, --localPath "); + +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`);