Skip to content

Commit 5974cbd

Browse files
committed
fix: dedup staging rows for range upsert, revert tb fmt on deletedAt (CM-1258)
Signed-off-by: Uroš Marolt <uros@marolt.me>
1 parent 77d4017 commit 5974cbd

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

services/apps/packages_worker/src/deps-dev/workflows/ingestAdvisories.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,15 @@ ON CONFLICT (advisory_id, ecosystem, package_name) DO NOTHING
9090
// typical after supersedeDepsDevRanges soft-deletes deps.dev rows on OSV takeover
9191
// and OSV later drops the package again — otherwise DO NOTHING would leave
9292
// staging's live data with no corresponding live row.
93+
// DISTINCT ON (ap.id) is required because DO UPDATE (unlike DO NOTHING) errors
94+
// with "ON CONFLICT DO UPDATE command cannot affect row a second time" if two
95+
// staging rows for the same package (e.g. multiple disjoint deps.dev ranges
96+
// under one advisory) hit the same conflict target within one statement — every
97+
// deps.dev row shares one key per advisory_package_id since introduced_version
98+
// etc. are always NULL here.
9399
const ADVISORY_AFFECTED_RANGES_MERGE_SQL = `
94100
INSERT INTO advisory_affected_ranges (advisory_package_id, range_raw, unaffected_raw, introduced_version, created_at, updated_at)
95-
SELECT
101+
SELECT DISTINCT ON (ap.id)
96102
ap.id,
97103
s.range_raw,
98104
s.unaffected_raw,
@@ -110,6 +116,7 @@ WHERE NOT EXISTS (
110116
AND live.range_raw IS NULL
111117
AND live.unaffected_raw IS NULL
112118
)
119+
ORDER BY ap.id
113120
ON CONFLICT (advisory_package_id, COALESCE(introduced_version, ''), COALESCE(fixed_version, ''), COALESCE(last_affected, ''))
114121
DO UPDATE SET
115122
updated_at = NOW(),

services/libs/tinybird/datasources/advisoryAffectedRanges.datasource

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ SCHEMA >
2222
`unaffectedRaw` String `json:$.record.unaffected_raw` DEFAULT '',
2323
`createdAt` DateTime64(3) `json:$.record.created_at`,
2424
`updatedAt` DateTime64(3) `json:$.record.updated_at`,
25-
`deletedAt` Nullable(DateTime64(3)) `json:$.record.deleted_at` DEFAULT NULL
25+
`deletedAt` Nullable(DateTime64(3)) `json:$.record.deleted_at`
2626

2727
ENGINE ReplacingMergeTree
2828
ENGINE_PARTITION_KEY toYear(createdAt)

0 commit comments

Comments
 (0)