Fold PromoteSource into Update
Every PromoteSource caller already loaded the tracker pattern under the same transaction, so a dedicated single-column UPDATE only duplicated machinery and forced callers to learn a second mutation verb. Add `source = @source` to Update's SET clause, mutate Source/UpdatedAt on the receiver, and call Update at the three promotion sites (worker merge loop, worker adoption loop, and reportDetectedTracker). The shouldPromoteSource gate still ranks the candidate against the loaded value; Update is now the single write path that can advance source, with a doc comment spelling out the load-first contract. Re-cast the coredata tests around Update: WritesSource pins the round-trip from receiver to DB, NotFoundForMissingRow preserves the ErrResourceNotFound contract callers rely on. The old OnlyTouchesSourceAndUpdatedAt test was a property of the narrow PromoteSource UPDATE and no longer applies — Update intentionally rewrites the full editable column set from the receiver. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -202,7 +202,10 @@ func (h *patternAnalysisHandler) Process(ctx context.Context, banner coredata.Co
|
||||
}
|
||||
|
||||
if shouldPromoteSource(globPattern.Source, source) {
|
||||
if err := globPattern.PromoteSource(ctx, tx, scope, *source, now); err != nil {
|
||||
globPattern.Source = source
|
||||
globPattern.UpdatedAt = now
|
||||
|
||||
if err := globPattern.Update(ctx, tx, scope); err != nil {
|
||||
return fmt.Errorf("cannot promote source on glob pattern %q: %w", key.template, err)
|
||||
}
|
||||
}
|
||||
@@ -784,12 +787,15 @@ func (h *patternAnalysisHandler) adoptUncategorisedPatterns(
|
||||
// non-uncategorised category. Without this, a
|
||||
// PRE_EXISTING glob never advances to SCRIPT/EXTENSION
|
||||
// even though new SDK-observed exacts confirm the
|
||||
// stronger signal. PromoteSource mutates match.Source
|
||||
// in place, so subsequent adoptions against the same
|
||||
// glob ratchet correctly (PRE_EXISTING → EXTENSION →
|
||||
// SCRIPT) without redundant writes.
|
||||
// stronger signal. We mutate match.Source in place
|
||||
// before calling Update so subsequent adoptions against
|
||||
// the same glob ratchet correctly (PRE_EXISTING →
|
||||
// EXTENSION → SCRIPT) without redundant writes.
|
||||
if shouldPromoteSource(match.Source, ep.Source) {
|
||||
if err := match.PromoteSource(ctx, tx, scope, *ep.Source, time.Now()); err != nil {
|
||||
match.Source = ep.Source
|
||||
match.UpdatedAt = time.Now()
|
||||
|
||||
if err := match.Update(ctx, tx, scope); err != nil {
|
||||
return false, fmt.Errorf("cannot promote source on glob pattern %q: %w", match.Pattern, err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -521,7 +521,8 @@ func TestPatternAnalysisWorker_AdoptionPromotesSourceCrossCategory(t *testing.T)
|
||||
// detected_tracker straight to the glob, so no new exact is
|
||||
// created and the merge/adoption loops in
|
||||
// patternAnalysisHandler.Process never see the new signal. Without
|
||||
// the in-line PromoteSource call in reportDetectedTracker, only
|
||||
// the in-line source promotion in reportDetectedTracker (which
|
||||
// mutates matchedPattern.Source and writes via Update), only
|
||||
// last_matched_at would advance — source would stay stuck at
|
||||
// PRE_EXISTING despite the new SDK-observed evidence. This test
|
||||
// pins the same-category promotion path; the cross-category gap
|
||||
|
||||
@@ -2223,7 +2223,10 @@ func (s *Service) reportDetectedTracker(
|
||||
// items without a source and weaker re-detections cost
|
||||
// nothing.
|
||||
if shouldPromoteSource(matchedPattern.Source, info.Source) {
|
||||
if err := matchedPattern.PromoteSource(ctx, tx, scope, *info.Source, now); err != nil {
|
||||
matchedPattern.Source = info.Source
|
||||
matchedPattern.UpdatedAt = now
|
||||
|
||||
if err := matchedPattern.Update(ctx, tx, scope); err != nil {
|
||||
return fmt.Errorf("cannot promote source on matched tracker pattern %q: %w", matchedPattern.Pattern, err)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user