A CPV code is now stored once, keeping its longest description

This commit is contained in:
bwilliams committed 2026-10-22 09:56:01 +01:00
1 parent b0f93fd437
commit 0a777792d8
5 files changed
+209 -8

No files matched your search

+13 -4
View File
@@ -197,15 +197,24 @@ ORDER BY release_id, tag`
// ---------- classification_codes ----------
// upsertClassificationCodeReturningID returns the id of the row matching
// every content column, inserting it first if needed. Named parameters
// upsertClassificationCodeReturningID returns the id of the row for a
// classification, inserting it first if needed. Named parameters
// (types.ClassificationCode).
//
// The no-op DO UPDATE (rather than DO NOTHING) makes RETURNING yield the
// existing row's id on conflict.
// A CPV code has one row whatever its description (migration 00010): the
// row keeps the longest description it has been given, as EU Supply cuts
// them to 64 characters. Any other scheme's row matches every content
// column. The no-op DO UPDATE (rather than DO NOTHING) makes RETURNING
// yield the existing row's id on conflict.
const upsertClassificationCodeReturningID = `
INSERT INTO classification_codes (is_cpv, code, classification)
VALUES (:is_cpv, :code, :classification)
ON CONFLICT (code) WHERE is_cpv
DO UPDATE SET classification = CASE
WHEN length(excluded.classification) > length(classification)
THEN excluded.classification
ELSE classification
END
ON CONFLICT (is_cpv, code, classification)
DO UPDATE SET is_cpv = excluded.is_cpv
RETURNING id`