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

fix: fix delete transmission by age with Postgres #4973

Merged
merged 1 commit into from
Oct 16, 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 2 additions & 2 deletions internal/core/data/controller/http/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion internal/pkg/infrastructure/postgres/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/infrastructure/postgres/transmission.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Loading