seed data
This commit is contained in:
@@ -2,7 +2,7 @@ import type { Logger } from '@core/shared/application';
|
||||
import type { EnsureInitialData } from '../../../../../adapters/bootstrap/EnsureInitialData';
|
||||
import { SeedRacingData, type RacingSeedDependencies } from '../../../../../adapters/bootstrap/SeedRacingData';
|
||||
import { Inject, Module, OnModuleInit } from '@nestjs/common';
|
||||
import { getApiPersistence, getEnableBootstrap } from '../../env';
|
||||
import { getApiPersistence, getEnableBootstrap, getForceReseed } from '../../env';
|
||||
import { RacingPersistenceModule } from '../../persistence/racing/RacingPersistenceModule';
|
||||
import { SocialPersistenceModule } from '../../persistence/social/SocialPersistenceModule';
|
||||
import { AchievementPersistenceModule } from '../../persistence/achievement/AchievementPersistenceModule';
|
||||
@@ -48,7 +48,21 @@ export class BootstrapModule implements OnModuleInit {
|
||||
if (persistence !== 'postgres') return false;
|
||||
if (process.env.NODE_ENV === 'production') return false;
|
||||
|
||||
return this.isRacingDatabaseEmpty();
|
||||
// Check for force reseed flag
|
||||
const forceReseed = getForceReseed();
|
||||
if (forceReseed) {
|
||||
this.logger.info('[Bootstrap] Force reseed enabled via GRIDPILOT_API_FORCE_RESEED');
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if database is empty
|
||||
const isEmpty = await this.isRacingDatabaseEmpty();
|
||||
if (!isEmpty) {
|
||||
// Database has data, check if it needs reseeding
|
||||
return await this.needsReseed();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private async isRacingDatabaseEmpty(): Promise<boolean> {
|
||||
@@ -58,4 +72,24 @@ export class BootstrapModule implements OnModuleInit {
|
||||
const leagues = await this.seedDeps.leagueRepository.findAll();
|
||||
return leagues.length === 0;
|
||||
}
|
||||
|
||||
private async needsReseed(): Promise<boolean> {
|
||||
// Check if driver count is less than expected (150)
|
||||
// This indicates old seed data that needs updating
|
||||
try {
|
||||
const drivers = await this.seedDeps.driverRepository.findAll();
|
||||
const driverCount = drivers.length;
|
||||
|
||||
// If we have fewer than 150 drivers, we need to reseed
|
||||
if (driverCount < 150) {
|
||||
this.logger.info(`[Bootstrap] Found ${driverCount} drivers (expected 150), triggering reseed`);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (error) {
|
||||
this.logger.warn('[Bootstrap] Error checking driver count for reseed:', error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user