diff --git a/configuration/postgres/postgres.go b/configuration/postgres/postgres.go index 734a199f0d..7b2799bbdf 100644 --- a/configuration/postgres/postgres.go +++ b/configuration/postgres/postgres.go @@ -344,10 +344,10 @@ func buildQuery(req *configuration.GetRequest, configTable string) (string, []in var query string var params []interface{} if len(req.Keys) == 0 { - query = "SELECT * FROM " + configTable + query = "SELECT * FROM " + pgx.Identifier{configTable}.Sanitize() } else { var queryBuilder strings.Builder - queryBuilder.WriteString("SELECT * FROM " + configTable + " WHERE KEY IN (") + queryBuilder.WriteString("SELECT * FROM " + pgx.Identifier{configTable}.Sanitize() + " WHERE KEY IN (") var paramWildcard []string paramPosition := 1 for _, v := range req.Keys { @@ -364,10 +364,9 @@ func buildQuery(req *configuration.GetRequest, configTable string) (string, []in i, j := len(req.Metadata), 0 s.WriteString(" AND ") for k, v := range req.Metadata { - temp := "$" + strconv.Itoa(paramPosition) + " = " + "$" + strconv.Itoa(paramPosition+1) - s.WriteString(temp) - params = append(params, k, v) - paramPosition += 2 + s.WriteString(pgx.Identifier{k}.Sanitize() + " = $" + strconv.Itoa(paramPosition)) + params = append(params, v) + paramPosition++ if j++; j < i { s.WriteString(" AND ") } @@ -432,7 +431,8 @@ func (p *ConfigurationStore) subscribeToChannel(ctx context.Context, pgNotifyCha // GetComponentMetadata returns the metadata of the component. func (p *ConfigurationStore) GetComponentMetadata() (metadataInfo contribMetadata.MetadataMap) { metadataStruct := metadata{} - contribMetadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, contribMetadata.ConfigurationStoreType) + contribMetadata.GetMetadataInfoFromStructType(reflect.TypeOf(metadataStruct), &metadataInfo, + contribMetadata.ConfigurationStoreType) return } diff --git a/configuration/postgres/postgres_test.go b/configuration/postgres/postgres_test.go index f3d12ceb8e..90514349d2 100644 --- a/configuration/postgres/postgres_test.go +++ b/configuration/postgres/postgres_test.go @@ -27,7 +27,7 @@ import ( func TestSelectAllQuery(t *testing.T) { g := &configuration.GetRequest{} - expected := "SELECT * FROM cfgtbl" + expected := "SELECT * FROM \"cfgtbl\"" query, _, err := buildQuery(g, "cfgtbl") if err != nil { t.Errorf("Error building query: %v ", err) @@ -59,7 +59,7 @@ func TestPostgresbuildQuery(t *testing.T) { query, params, err := buildQuery(g, "cfgtbl") _ = params require.NoError(t, err, "Error building query: %v ", err) - expected := "SELECT * FROM cfgtbl WHERE KEY IN ($1) AND $2 = $3" + expected := "SELECT * FROM \"cfgtbl\" WHERE KEY IN ($1) AND \"Version\" = $2" assert.Equal(t, expected, query, "did not get expected result. Got: '%v' , Expected: '%v'", query, expected) i := 0 for _, v := range params { @@ -69,9 +69,6 @@ func TestPostgresbuildQuery(t *testing.T) { expected := "someKey" assert.Equal(t, expected, got, "Did not get expected result. Got: '%v' , Expected: '%v'", got, expected) case 1: - expected := "Version" - assert.Equal(t, expected, got, "Did not get expected result. Got: '%v' , Expected: '%v'", got, expected) - case 2: expected := "1.0" assert.Equal(t, expected, got, "Did not get expected result. Got: '%v' , Expected: '%v'", got, expected) } diff --git a/tests/certification/configuration/postgres/postgres_test.go b/tests/certification/configuration/postgres/postgres_test.go index e8e7d12700..47daa0f21c 100644 --- a/tests/certification/configuration/postgres/postgres_test.go +++ b/tests/certification/configuration/postgres/postgres_test.go @@ -262,6 +262,18 @@ func TestPostgres(t *testing.T) { // Should return the value with version 2 require.Equal(t, "val-version-2", items[key1].Value) + // Add two versions of key1, first with 1.0.0, second with 1.1.0 + err = addKey(key1, "exact-string-version-match-1", "1.0.0") + require.NoError(t, err, "error adding key") + err = addKey(key1, "exact-string-version-match-2", "1.1.0") + require.NoError(t, err, "error adding key") + opt := dapr.WithConfigurationMetadata("version", "1.0.0") + items, err = client.GetConfigurationItems(ctx, storeName, []string{key1}, opt) + require.NoError(t, err) + require.Equal(t, 1, len(items)) + // Should return the value with version 1.0.0 + require.Equal(t, "exact-string-version-match-1", items[key1].Value) + // Delete key1 err = updater.DeleteKey([]string{key1}) require.NoError(t, err, "error deleting key")