Skip to content

Commit

Permalink
add group_by clause to discovery search to prevent duplicate results
Browse files Browse the repository at this point in the history
  • Loading branch information
woutslakhorst committed Oct 8, 2024
1 parent 8111be2 commit 65160a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
3 changes: 3 additions & 0 deletions discovery/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ func (s *sqlStore) search(serviceID string, query map[string]string) ([]vc.Verif
Where("service_id = ?", serviceID).
Joins("inner join discovery_credential ON discovery_credential.presentation_id = discovery_presentation.id")
stmt = store.CredentialStore{}.BuildSearchStatement(stmt, "discovery_credential.credential_id", query)
// prevent duplicates
// GROUP BY `discovery_presentation`.`service_id`, `discovery_presentation`.`credential_subject_id`
stmt = stmt.Group("discovery_presentation.service_id, discovery_presentation.credential_subject_id")

var matches []presentationRecord
if err := stmt.Preload("Credentials").Preload("Credentials.Credential").Find(&matches).Error; err != nil {
Expand Down
12 changes: 12 additions & 0 deletions discovery/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,18 @@ func Test_sqlStore_search(t *testing.T) {
require.Len(t, actualVPs, 1)
assert.Equal(t, vpAlice.ID.String(), actualVPs[0].ID.String())
})
t.Run("find all", func(t *testing.T) {
vps := []vc.VerifiablePresentation{vpAlice, vpBob}
c := setupStore(t, storageEngine.GetSQLDatabase())
for _, vp := range vps {
err := c.add(testServiceID, vp, 0)
require.NoError(t, err)
}

actualVPs, err := c.search(testServiceID, map[string]string{})
require.NoError(t, err)
require.Len(t, actualVPs, 2)
})
t.Run("not found", func(t *testing.T) {
vps := []vc.VerifiablePresentation{vpAlice, vpBob}
c := setupStore(t, storageEngine.GetSQLDatabase())
Expand Down

0 comments on commit 65160a7

Please sign in to comment.