diff --git a/services/processor/src/schema-guard.service.ts b/services/processor/src/schema-guard.service.ts index b560d9c..d0f431d 100644 --- a/services/processor/src/schema-guard.service.ts +++ b/services/processor/src/schema-guard.service.ts @@ -28,6 +28,17 @@ export class SchemaGuardService implements OnModuleInit { constructor(@InjectDataSource() private readonly dataSource: DataSource) {} async onModuleInit(): Promise { + // Pre-dedupe: legacy rows without the index can block CREATE UNIQUE INDEX. + await this.dataSource.query(` + DELETE FROM aggregated_metrics a + USING aggregated_metrics b + WHERE a.ctid < b.ctid + AND a."metricType" = b."metricType" + AND a."granularity" = b."granularity" + AND a."timestamp" = b."timestamp" + AND a."dimension" IS NOT DISTINCT FROM b."dimension" + AND a."dimensionValue" IS NOT DISTINCT FROM b."dimensionValue" + `); await this.dataSource.query(` CREATE UNIQUE INDEX IF NOT EXISTS uq_aggregated_metrics_dedup ON aggregated_metrics ("metricType", "granularity", "timestamp", "dimension", "dimensionValue")