Fix PR review findings

- Replace copy-pasted New Relic URLs in Resend third-party entry
  with correct resend.com URLs and drop inapplicable fields
- Escape single '%' instead of '%%' in LIKE pattern conversion so
  literal percent signs are not treated as wildcards
- Return actual row ID from CommonTrackerPattern.Upsert via
  RETURNING id so conflict-path callers get the existing ID
- Add ORDER BY id ASC to vendor-by-common-third-party query for
  deterministic LIMIT 1 selection

Signed-off-by: Émile Ré <emile@getprobo.com>
Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
Émile Ré
2026-05-14 14:10:41 +04:00
committed by Émile Ré
parent f52a4746fd
commit a16821802e
5 changed files with 27 additions and 19 deletions

View File

@@ -192,7 +192,7 @@ INSERT INTO common_tracker_patterns (
func (p CommonTrackerPattern) Upsert(
ctx context.Context,
conn pg.Tx,
) (inserted bool, err error) {
) (actualID gid.GID, inserted bool, err error) {
q := `
INSERT INTO common_tracker_patterns (
id,
@@ -224,7 +224,7 @@ SET
description = EXCLUDED.description,
confidence = EXCLUDED.confidence,
updated_at = EXCLUDED.updated_at
RETURNING (xmax = 0) AS inserted
RETURNING id, (xmax = 0) AS inserted
`
args := pgx.StrictNamedArgs{
@@ -242,16 +242,24 @@ RETURNING (xmax = 0) AS inserted
rows, err := conn.Query(ctx, q, args)
if err != nil {
return false, fmt.Errorf("cannot upsert common tracker pattern: %w", err)
return gid.GID{}, false, fmt.Errorf("cannot upsert common tracker pattern: %w", err)
}
defer rows.Close()
inserted, err = pgx.CollectExactlyOneRow(rows, pgx.RowTo[bool])
if err != nil {
return false, fmt.Errorf("cannot collect upsert result: %w", err)
type upsertResult struct {
ID gid.GID
Inserted bool
}
return inserted, nil
res, err := pgx.CollectExactlyOneRow(rows, func(row pgx.CollectableRow) (upsertResult, error) {
var r upsertResult
return r, row.Scan(&r.ID, &r.Inserted)
})
if err != nil {
return gid.GID{}, false, fmt.Errorf("cannot collect upsert result: %w", err)
}
return res.ID, res.Inserted, nil
}
func (p CommonTrackerPattern) Delete(
@@ -301,7 +309,7 @@ WHERE
(match_type = @match_type_glob
AND @identifier LIKE
replace(replace(replace(replace(
pattern, E'\\', E'\\\\'), '%%', E'\\%%'), '_', E'\\_'), '*', '%%')
pattern, E'\\', E'\\\\'), '%', E'\\%'), '_', E'\\_'), '*', '%')
ESCAPE E'\\')
OR (match_type = @match_type_exact AND pattern = @identifier)
)

View File

@@ -1358,6 +1358,7 @@ WHERE
AND organization_id = @organization_id
AND common_third_party_id = @common_third_party_id
AND snapshot_id IS NULL
ORDER BY id ASC
LIMIT 1;
`

View File

@@ -161,7 +161,8 @@ func (h *trackerMappingHandler) matchByDomain(
UpdatedAt: now,
}
if _, err := commonPattern.Upsert(ctx, tx); err != nil {
actualID, _, err := commonPattern.Upsert(ctx, tx)
if err != nil {
h.logger.ErrorCtx(
ctx,
"cannot upsert common tracker pattern from domain match",
@@ -170,6 +171,7 @@ func (h *trackerMappingHandler) matchByDomain(
return nil, nil
}
commonPattern.ID = actualID
thirdPartyID := h.resolveThirdParty(ctx, tx, tp, &commonPattern)
return &commonPattern.ID, thirdPartyID