Skip to content

Fix Postgres configuration store incorrectly handling metadata key in query #3693

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
14 changes: 7 additions & 7 deletions configuration/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 ")
}
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 2 additions & 5 deletions configuration/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand Down
12 changes: 12 additions & 0 deletions tests/certification/configuration/postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading