diff --git a/discovery/store.go b/discovery/store.go index 9e0b3780e..d09629185 100644 --- a/discovery/store.go +++ b/discovery/store.go @@ -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 { diff --git a/discovery/store_test.go b/discovery/store_test.go index d675905a6..b782cb0a4 100644 --- a/discovery/store_test.go +++ b/discovery/store_test.go @@ -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())