Use JSON server configuration and repository database name
This commit is contained in:
@@ -3,6 +3,7 @@ import { dirname, resolve } from 'node:path';
|
||||
import { pathToFileURL } from 'node:url';
|
||||
import { BSON, MongoClient, type Db } from 'mongodb';
|
||||
import { LEGACY_EVENT_TYPES } from '../constants/legacy-data.js';
|
||||
import { loadServerConfig } from '../src/server/config.js';
|
||||
|
||||
// Run while the old app is stopped. Never called by server startup.
|
||||
export async function cleanupLegacyData(db: Db, backupPath?: string) {
|
||||
@@ -32,12 +33,12 @@ export async function cleanupLegacyData(db: Db, backupPath?: string) {
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
|
||||
const args = process.argv.slice(2), apply = args.includes('--apply');
|
||||
const database = args[args.indexOf('--database') + 1];
|
||||
const configured = process.env.MONGODB_DATABASE || 'dragon_ball';
|
||||
const config = loadServerConfig();
|
||||
const configured = config.MONGODB_DATABASE;
|
||||
if (!args.includes('--database') || database !== configured || ['admin', 'config', 'local'].includes(database)) throw new Error('Specify --database matching MONGODB_DATABASE');
|
||||
if (!process.env.MONGODB_URI) throw new Error('MONGODB_URI_REQUIRED');
|
||||
const backup = args.includes('--backup') ? args[args.indexOf('--backup') + 1] : undefined;
|
||||
if (apply && (!backup || backup.startsWith('--'))) throw new Error('--apply requires --backup <new-file>');
|
||||
const client = new MongoClient(process.env.MONGODB_URI, { serverSelectionTimeoutMS: 5000 });
|
||||
const client = new MongoClient(config.MONGODB_URI, { serverSelectionTimeoutMS: 5000 });
|
||||
try { await client.connect(); console.log(JSON.stringify(await cleanupLegacyData(client.db(database), apply ? resolve(backup!) : undefined), null, 2)); }
|
||||
finally { await client.close(); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user