seed data

This commit is contained in:
2025-12-30 00:15:35 +01:00
parent 7a853d4e43
commit ccaa39c39c
22 changed files with 1342 additions and 173 deletions

View File

@@ -22,8 +22,9 @@ import type { IPenaltyRepository } from '@core/racing/domain/repositories/IPenal
import type { IFeedRepository } from '@core/social/domain/repositories/IFeedRepository';
import type { ISocialGraphRepository } from '@core/social/domain/repositories/ISocialGraphRepository';
import { createRacingSeed } from './racing/RacingSeed';
import { getApiPersistence } from '../../apps/api/src/env';
import { seedId } from './racing/SeedIdHelper';
import { DriverStatsStore } from '@adapters/racing/services/DriverStatsStore';
import { TeamStatsStore } from '@adapters/racing/services/TeamStatsStore';
export type RacingSeedDependencies = {
driverRepository: IDriverRepository;
@@ -54,16 +55,56 @@ export class SeedRacingData {
private readonly seedDeps: RacingSeedDependencies,
) {}
private getApiPersistence(): 'postgres' | 'inmemory' {
const configured = process.env.GRIDPILOT_API_PERSISTENCE?.toLowerCase();
if (configured === 'postgres' || configured === 'inmemory') {
return configured;
}
if (process.env.NODE_ENV === 'test') {
return 'inmemory';
}
return process.env.DATABASE_URL ? 'postgres' : 'inmemory';
}
async execute(): Promise<void> {
const existingDrivers = await this.seedDeps.driverRepository.findAll();
if (existingDrivers.length > 0) {
const persistence = this.getApiPersistence();
// Check for force reseed via environment variable
const forceReseedRaw = process.env.GRIDPILOT_API_FORCE_RESEED;
const forceReseed = forceReseedRaw !== undefined && forceReseedRaw !== '0' && forceReseedRaw.toLowerCase() !== 'false';
if (existingDrivers.length > 0 && !forceReseed) {
this.logger.info('[Bootstrap] Racing seed skipped (drivers already exist), ensuring scoring configs');
await this.ensureScoringConfigsForExistingData();
return;
}
const persistence = getApiPersistence();
const seed = createRacingSeed({ persistence });
if (forceReseed && existingDrivers.length > 0) {
this.logger.info('[Bootstrap] Force reseed enabled - clearing existing racing data');
await this.clearExistingRacingData();
}
const seed = createRacingSeed({
persistence,
driverCount: 150 // Expanded from 100 to 150
});
// Populate the driver stats store for the InMemoryDriverStatsService
const driverStatsStore = DriverStatsStore.getInstance();
driverStatsStore.clear(); // Clear any existing stats
driverStatsStore.loadStats(seed.driverStats);
this.logger.info(`[Bootstrap] Loaded driver stats for ${seed.driverStats.size} drivers`);
// Populate the team stats store for the AllTeamsPresenter
const teamStatsStore = TeamStatsStore.getInstance();
teamStatsStore.clear(); // Clear any existing stats
teamStatsStore.loadStats(seed.teamStats);
this.logger.info(`[Bootstrap] Loaded team stats for ${seed.teamStats.size} teams`);
let sponsorshipRequestsSeededViaRepo = false;
const seedableSponsorshipRequests = this.seedDeps
@@ -268,6 +309,36 @@ export class SeedRacingData {
);
}
private async clearExistingRacingData(): Promise<void> {
// Get all existing drivers
const drivers = await this.seedDeps.driverRepository.findAll();
// Delete drivers first (this should cascade to related data in most cases)
for (const driver of drivers) {
try {
await this.seedDeps.driverRepository.delete(driver.id);
} catch {
// Ignore errors
}
}
// Try to clean up other data if repositories support it
try {
const leagues = await this.seedDeps.leagueRepository.findAll();
for (const league of leagues) {
try {
await this.seedDeps.leagueRepository.delete(league.id.toString());
} catch {
// Ignore
}
}
} catch {
// Ignore
}
this.logger.info('[Bootstrap] Cleared existing racing data');
}
private async ensureScoringConfigsForExistingData(): Promise<void> {
const leagues = await this.seedDeps.leagueRepository.findAll();