Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent duplicate results in discovery search #3459

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions discovery/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,14 @@ func (s *sqlStore) get(serviceID string, startAfter int) (map[string]vc.Verifiab
// Wildcard matching is supported by prefixing or suffixing the value with an asterisk (*).
// It returns the presentations which contain credentials that match the given query.
func (s *sqlStore) search(serviceID string, query map[string]string) ([]vc.VerifiablePresentation, error) {
// first only select columns also used in group by clause
// if the query is empty, there's no need to do a join
stmt := s.db.Model(&presentationRecord{}).
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)
Where("service_id = ?", serviceID)
if len(query) > 0 {
stmt = stmt.Joins("inner join discovery_credential ON discovery_credential.presentation_id = discovery_presentation.id")
stmt = store.CredentialStore{}.BuildSearchStatement(stmt, "discovery_credential.credential_id", query)
}

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
Loading