From 30a8696a99613ed99353c02814dad0cf38f3e26e Mon Sep 17 00:00:00 2001 From: zkldi Date: Tue, 2 Nov 2021 18:23:39 +0000 Subject: [PATCH] Add improved init-sequence-docs script --- server/scripts/init-sequence-docs.ts | 74 ++++++++++++++++++++++++++++ server/scripts/set-counters.ts | 26 ---------- 2 files changed, 74 insertions(+), 26 deletions(-) create mode 100644 server/scripts/init-sequence-docs.ts delete mode 100644 server/scripts/set-counters.ts diff --git a/server/scripts/init-sequence-docs.ts b/server/scripts/init-sequence-docs.ts new file mode 100644 index 000000000..e5e4bdc51 --- /dev/null +++ b/server/scripts/init-sequence-docs.ts @@ -0,0 +1,74 @@ +import { Command } from "commander"; +import db from "external/mongo/db"; +import CreateLogCtx from "lib/logger/logger"; + +const program = new Command(); +program.option("-f, --force", "Delete existing sequence documents and recreate them."); + +program.parse(process.argv); +const options = program.opts(); + +const logger = CreateLogCtx(__filename); + +if (require.main === module) { + (async () => { + if (options.force) { + await db.counters.remove({}); + } else { + const anyCounters = await db.counters.findOne(); + + if (anyCounters) { + throw new Error( + `Counters are already in database, cannot create them without --force.` + ); + } + } + + const userWithLargestID = await db.users.findOne( + {}, + { + sort: { + id: -1, + }, + } + ); + + const largestUSCSongID = await db.songs.usc.findOne( + {}, + { + sort: { + id: -1, + }, + } + ); + const largestBMSSongID = await db.songs.bms.findOne( + {}, + { + sort: { + id: -1, + }, + } + ); + + const Counters = [ + { + counterName: "users", + value: userWithLargestID ? userWithLargestID.id + 1 : 1, + }, + { + counterName: "usc-song-id", + value: largestUSCSongID ? largestUSCSongID.id + 1 : 1, + }, + { + counterName: "bms-song-id", + value: largestBMSSongID ? largestBMSSongID.id + 1 : 1, + }, + ]; + + logger.info(`Inserting ${Counters.map((e) => `${e.counterName}: ${e.value}`)}`); + + await db.counters.insert(Counters); + + process.exit(0); + })(); +} diff --git a/server/scripts/set-counters.ts b/server/scripts/set-counters.ts deleted file mode 100644 index 024f14a91..000000000 --- a/server/scripts/set-counters.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { CounterDocument } from "tachi-common"; -import db from "external/mongo/db"; - -(async () => { - const uscSongCount = await db.songs.usc.find({}); - const bmsSongCount = await db.songs.bms.find({}); - - const Counters: CounterDocument[] = [ - { - counterName: "users", - value: 2, - }, - { - counterName: "usc-song-id", - value: uscSongCount.length + 1, - }, - { - counterName: "bms-song-id", - value: bmsSongCount.length + 1, - }, - ]; - - db.counters.insert(Counters).then(() => { - process.exit(0); - }); -})();