Skip to content

Commit a2dc276

Browse files
authored
fix(api): update suite metadata on re-index when name changes (#189)
## Summary - Fixes suite `labels.name` not updating in the database when it changes between runs (while suite hash stays the same) - Root cause: `UpsertSuite` passed the same struct pointer to both GORM's `Assign()` and `FirstOrCreate()`, making it first-write-wins — when the record was found, `FirstOrCreate` populated the struct with DB values before `Assign` could apply the updates - Fix: use a map for `Assign()` so update values are independent of the struct pointer ## Test plan - [x] Added `TestStore_UpsertSuiteUpdatesName` — upserts a suite, then upserts again with a different name, asserts the name is updated - [x] All existing indexstore tests pass
1 parent 9bfe80c commit a2dc276

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

pkg/api/indexstore/indexstore.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,10 +678,17 @@ func (s *store) DeleteTestStatsBlockLogsForRun(
678678
}
679679

680680
// UpsertSuite inserts or updates a suite record keyed by suite_hash.
681+
// Uses a map for Assign so update values are not overwritten when
682+
// FirstOrCreate populates the struct with the existing DB record.
681683
func (s *store) UpsertSuite(ctx context.Context, suite *Suite) error {
682684
result := s.db.WithContext(ctx).
683685
Where("suite_hash = ?", suite.SuiteHash).
684-
Assign(suite).
686+
Assign(map[string]any{
687+
"discovery_path": suite.DiscoveryPath,
688+
"name": suite.Name,
689+
"tests_total": suite.TestsTotal,
690+
"indexed_at": suite.IndexedAt,
691+
}).
685692
FirstOrCreate(suite)
686693
if result.Error != nil {
687694
return fmt.Errorf("upserting suite: %w", result.Error)

pkg/api/indexstore/indexstore_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,32 @@ func TestStore_TestStatCRUD(t *testing.T) {
291291
assert.Equal(t, runID2, remaining[0].RunID)
292292
assert.Equal(t, "TestA", remaining[0].TestName)
293293
}
294+
295+
func TestStore_UpsertSuiteUpdatesName(t *testing.T) {
296+
s := setupTestStore(t)
297+
ctx := context.Background()
298+
299+
original := &indexstore.Suite{
300+
SuiteHash: "hash-abc",
301+
DiscoveryPath: "dp/test",
302+
Name: "old-name",
303+
TestsTotal: 5,
304+
IndexedAt: time.Now().UTC(),
305+
}
306+
require.NoError(t, s.UpsertSuite(ctx, original))
307+
308+
// Upsert the same hash with a different name.
309+
updated := &indexstore.Suite{
310+
SuiteHash: "hash-abc",
311+
DiscoveryPath: "dp/test",
312+
Name: "new-name",
313+
TestsTotal: 5,
314+
IndexedAt: time.Now().UTC(),
315+
}
316+
require.NoError(t, s.UpsertSuite(ctx, updated))
317+
318+
// The struct should reflect the updated name.
319+
assert.Equal(t, "new-name", updated.Name)
320+
assert.NotZero(t, updated.ID, "ID should be populated after upsert")
321+
assert.Equal(t, original.ID, updated.ID, "upsert must not create a duplicate")
322+
}

0 commit comments

Comments
 (0)