chore(api): add data migrations
This commit is contained in:
1 parent
b5aa58dde8
commit
9c05850cba
6 files changed
+1438
No files matched your search
@@ -0,0 +1,102 @@
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import mongoose from 'mongoose';
|
||||
import { AppModule } from '../../../../app.module';
|
||||
import { ConnectionService } from '../connection.service';
|
||||
import { MatchStatsService } from '../../../community-stats/match-stats.service';
|
||||
|
||||
interface Options {
|
||||
communityId?: mongoose.Types.ObjectId;
|
||||
dryRun: boolean;
|
||||
force: boolean;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const options = parseArgs(process.argv.slice(2));
|
||||
const app = await NestFactory.createApplicationContext(AppModule, {
|
||||
logger: ['log', 'warn', 'error'],
|
||||
});
|
||||
|
||||
try {
|
||||
const connectionService = app.get(ConnectionService);
|
||||
const matchStatsService = app.get(MatchStatsService);
|
||||
const MatchModel = await connectionService.getModel('match');
|
||||
const MatchStatsModel = await connectionService.getModel('matchStats');
|
||||
|
||||
const filter: Record<string, unknown> = {
|
||||
status: { $in: ['finished', 'in-progress'] },
|
||||
'result.games.0': { $exists: true },
|
||||
};
|
||||
if (options.communityId) filter.communityId = options.communityId;
|
||||
|
||||
const matches = await MatchModel.find(filter)
|
||||
.sort({ scheduledAt: -1 })
|
||||
.select({ _id: 1 })
|
||||
.exec();
|
||||
console.log(`match_stats backfill target=${matches.length}`);
|
||||
|
||||
let processed = 0;
|
||||
let skipped = 0;
|
||||
let failed = 0;
|
||||
|
||||
for (const match of matches) {
|
||||
if (!options.force) {
|
||||
const existing = await MatchStatsModel.exists({ matchId: match._id });
|
||||
if (existing) {
|
||||
skipped += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (options.dryRun) {
|
||||
processed += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
await matchStatsService.buildAndUpsert(match._id);
|
||||
processed += 1;
|
||||
} catch (error) {
|
||||
failed += 1;
|
||||
console.warn(
|
||||
`failed ${match._id.toString()}: ${
|
||||
error instanceof Error ? error.message : error
|
||||
}`,
|
||||
);
|
||||
}
|
||||
|
||||
if ((processed + skipped + failed) % 50 === 0) {
|
||||
console.log(
|
||||
`progress processed=${processed} skipped=${skipped} failed=${failed}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(
|
||||
`done processed=${processed} skipped=${skipped} failed=${failed}`,
|
||||
);
|
||||
} finally {
|
||||
await app.close();
|
||||
}
|
||||
}
|
||||
|
||||
function parseArgs(args: string[]): Options {
|
||||
const options: Options = { dryRun: false, force: false };
|
||||
for (let index = 0; index < args.length; index += 1) {
|
||||
const arg = args[index];
|
||||
if (arg === '--dry-run') options.dryRun = true;
|
||||
if (arg === '--force') options.force = true;
|
||||
if (arg === '--community-id') {
|
||||
const value = args[index + 1];
|
||||
if (!value || !mongoose.isValidObjectId(value)) {
|
||||
throw new Error('--community-id requires a valid ObjectId');
|
||||
}
|
||||
options.communityId = new mongoose.Types.ObjectId(value);
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
return options;
|
||||
}
|
||||
|
||||
void main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in new issue
Block a user