perf(processor): Add pre-deduplication logic to prevent duplicate aggregated metrics data before index creation
Some checks failed
CI / build (push) Failing after 6m24s
CI / publish (push) Has been skipped

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
autocommit 2026-06-10 18:18:54 -07:00
parent 19feedfb66
commit 842c180980

View file

@ -28,6 +28,17 @@ export class SchemaGuardService implements OnModuleInit {
constructor(@InjectDataSource() private readonly dataSource: DataSource) {}
async onModuleInit(): Promise<void> {
// 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")