From 3517e8a61a815ce9955e3002ca3e1769cd3d83af Mon Sep 17 00:00:00 2001 From: Ginny Guan Date: Wed, 16 Oct 2024 13:12:40 +0800 Subject: [PATCH] fix: fix delete transmission by age with Postgres also upgrade go-mod-core-contracts to latest version close #4965 Signed-off-by: Ginny Guan --- go.mod | 2 +- go.sum | 4 ++-- internal/core/data/controller/http/event_test.go | 4 ++-- internal/pkg/infrastructure/postgres/sql.go | 7 ++++++- internal/pkg/infrastructure/postgres/transmission.go | 5 +++-- 5 files changed, 14 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index fddbd4a4ab..5e230c3740 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/eclipse/paho.mqtt.golang v1.5.0 github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66 github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19 - github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.50 + github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.51 github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40 github.com/edgexfoundry/go-mod-secrets/v3 v3.2.0-dev.18 github.com/fxamacker/cbor/v2 v2.7.0 diff --git a/go.sum b/go.sum index fce19ff6e9..0908b37137 100644 --- a/go.sum +++ b/go.sum @@ -88,8 +88,8 @@ github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66 h1:kmBEAhNi4ftrJMXM3Iv github.com/edgexfoundry/go-mod-bootstrap/v3 v3.2.0-dev.66/go.mod h1:3IXVpc5Qez5nwFJ8IkMyJMba8Iavj620E0XB42BQzfQ= github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19 h1:274NZdVBkJBuQP6yT3tVrb7psTFuIPogX/DLQqv7OCQ= github.com/edgexfoundry/go-mod-configuration/v3 v3.2.0-dev.19/go.mod h1:BG6hCDxXizpgMdNEljwNfLWSsd4Op7GAHd3Pis1dVv8= -github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.50 h1:Ho1TAQki5DN/ALdYP4YGNzj/Z/CXBXSeBancA+YHtO4= -github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.50/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk= +github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.51 h1:X73zP3/kgOGTXIRRxOMSGr174GrhxwuCfeBs+cCIudM= +github.com/edgexfoundry/go-mod-core-contracts/v3 v3.2.0-dev.51/go.mod h1:MLk37/79M26+bZr3IptNZuYmQBEVbXwzDp1VHQkFhIk= github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40 h1:YyB21HEapV5pENG01vFlpjPI6UkmKpJuaWFfgGFVUsY= github.com/edgexfoundry/go-mod-messaging/v3 v3.2.0-dev.40/go.mod h1:8NpZ6/eAsiyZHgn/s3DRIpcOjUrve+ZONIgvcDvA3Yg= github.com/edgexfoundry/go-mod-registry/v3 v3.2.0-dev.18 h1:AzILZ/xcEmSYVhIwSF4zkWpXyFoBA733E/j8ttzlNnI= diff --git a/internal/core/data/controller/http/event_test.go b/internal/core/data/controller/http/event_test.go index 3a770c70b3..6c43e6791a 100644 --- a/internal/core/data/controller/http/event_test.go +++ b/internal/core/data/controller/http/event_test.go @@ -218,7 +218,7 @@ func TestAddEvent(t *testing.T) { {"Invalid - Invalid Reading ValueType JSON", invalidReadingInvalidValueType, common.ContentTypeJSON, invalidReadingInvalidValueType.Event.ProfileName, invalidReadingInvalidValueType.Event.DeviceName, true, http.StatusBadRequest}, {"Invalid - No SimpleReading Value JSON", noSimpleValue, common.ContentTypeJSON, noSimpleValue.Event.ProfileName, noSimpleValue.Event.DeviceName, true, http.StatusBadRequest}, {"Valid - No BinaryReading BinaryValue JSON", noBinaryValue, common.ContentTypeJSON, noBinaryValue.Event.ProfileName, noBinaryValue.Event.DeviceName, false, http.StatusCreated}, - {"Valid - No BinaryReading MediaType JSON", noBinaryMediaType, common.ContentTypeJSON, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, false, http.StatusCreated}, + {"Invalid - No BinaryReading MediaType JSON", noBinaryMediaType, common.ContentTypeJSON, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, true, http.StatusBadRequest}, {"Valid - AddEventRequest CBOR", validRequest, common.ContentTypeCBOR, validRequest.Event.ProfileName, validRequest.Event.DeviceName, false, http.StatusCreated}, {"Valid - No RequestId CBOR", noRequestId, common.ContentTypeCBOR, noRequestId.Event.ProfileName, noRequestId.Event.DeviceName, false, http.StatusCreated}, {"Invalid - Bad RequestId CBOR", badRequestId, common.ContentTypeCBOR, badRequestId.Event.ProfileName, badRequestId.Event.DeviceName, true, http.StatusBadRequest}, @@ -238,7 +238,7 @@ func TestAddEvent(t *testing.T) { {"Invalid - Invalid Reading ValueType CBOR", invalidReadingInvalidValueType, common.ContentTypeCBOR, invalidReadingInvalidValueType.Event.ProfileName, invalidReadingInvalidValueType.Event.DeviceName, true, http.StatusBadRequest}, {"Invalid - No SimpleReading Value CBOR", noSimpleValue, common.ContentTypeCBOR, noSimpleValue.Event.ProfileName, noSimpleValue.Event.DeviceName, true, http.StatusBadRequest}, {"Valid - No BinaryReading BinaryValue CBOR", noBinaryValue, common.ContentTypeCBOR, noBinaryValue.Event.ProfileName, noBinaryValue.Event.DeviceName, false, http.StatusCreated}, - {"Valid - No BinaryReading MediaType CBOR", noBinaryMediaType, common.ContentTypeCBOR, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, false, http.StatusCreated}, + {"Invalid - No BinaryReading MediaType CBOR", noBinaryMediaType, common.ContentTypeCBOR, noBinaryMediaType.Event.ProfileName, noBinaryMediaType.Event.DeviceName, true, http.StatusBadRequest}, } for _, testCase := range tests { diff --git a/internal/pkg/infrastructure/postgres/sql.go b/internal/pkg/infrastructure/postgres/sql.go index 4b80e7283c..3215fe8b0b 100644 --- a/internal/pkg/infrastructure/postgres/sql.go +++ b/internal/pkg/infrastructure/postgres/sql.go @@ -267,6 +267,11 @@ func sqlDeleteByContentAge(table string) string { return fmt.Sprintf("DELETE FROM %s WHERE COALESCE((content->>'%s')::bigint, 0) < (EXTRACT(EPOCH FROM NOW()) * 1000)::bigint - $1", table, createdField) } +// sqlDeleteByContentAgeWithConds returns the SQL statement for deleting rows from the table by created timestamp from content column with the given where condition. +func sqlDeleteByContentAgeWithConds(table string, condition string) string { + return fmt.Sprintf("DELETE FROM %s WHERE %s AND COALESCE((content->>'%s')::bigint, 0) < (EXTRACT(EPOCH FROM NOW()) * 1000)::bigint - $1", table, condition, createdField) +} + // sqlDeleteByJSONField returns the SQL statement for deleting rows from the table by the given JSON query string func sqlDeleteByJSONField(table string) string { return fmt.Sprintf("DELETE FROM %s WHERE content @> $1::jsonb", table) @@ -315,7 +320,7 @@ func constructWhereCondition(columns ...string) string { return strings.Join(conditions, " AND ") } -// constructWhereCondition constructs the WHERE condition for the given columns with time range +// constructWhereCondWithTimeRange constructs the WHERE condition for the given columns with time range // if arrayColNames is not empty, ANY operator will be added to accept the array argument for the specified array col names func constructWhereCondWithTimeRange(timeRangeCol string, arrayColNames []string, columns ...string) string { var hasArrayColumn bool diff --git a/internal/pkg/infrastructure/postgres/transmission.go b/internal/pkg/infrastructure/postgres/transmission.go index ecc0ff6f7e..dc6c52364e 100644 --- a/internal/pkg/infrastructure/postgres/transmission.go +++ b/internal/pkg/infrastructure/postgres/transmission.go @@ -118,8 +118,9 @@ func (c *Client) TransmissionsByStatus(offset, limit int, status string) ([]mode // DeleteProcessedTransmissionsByAge deletes the processed transmissions that are older than a specific age func (c *Client) DeleteProcessedTransmissionsByAge(age int64) errors.EdgeX { - queryObj := map[string]any{statusField: models.Processed} - _, err := c.ConnPool.Exec(context.Background(), sqlDeleteByJSONFieldAndAge(transmissionTableName), queryObj, age) + status := []string{models.Sent, models.Acknowledged, models.Escalated} + conditions := fmt.Sprintf("(content -> '%s') ?| $2", statusField) + _, err := c.ConnPool.Exec(context.Background(), sqlDeleteByContentAgeWithConds(transmissionTableName, conditions), age, status) if err != nil { return pgClient.WrapDBError("failed to delete processed transmissions by age", err) }