Skip to content

Commit

Permalink
fix: do not change created_at on entity updates (#1061)
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele authored Oct 31, 2023
1 parent d8b1753 commit 50b05dd
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 44 deletions.
4 changes: 3 additions & 1 deletion diff/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,9 @@ func (sc *Syncer) Solve(ctx context.Context, parallelism int, dry bool, isJSONOu
}
} else {
// diff mode
// return the new obj as is
// return the new obj as is but with timestamps zeroed out
utils.ZeroOutTimestamps(e.Obj)
utils.ZeroOutTimestamps(e.OldObj)
result = e.Obj
}
// record operation in both: diff and sync commands
Expand Down
22 changes: 19 additions & 3 deletions diff/diff_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/hexops/gotextdiff/myers"
"github.com/hexops/gotextdiff/span"
"github.com/kong/deck/state"
"github.com/kong/deck/utils"
)

var differ = gojsondiff.New()
Expand Down Expand Up @@ -61,8 +60,6 @@ func prettyPrintJSONString(JSONString string) (string, error) {
}

func getDiff(a, b interface{}) (string, error) {
utils.ZeroOutTimestamps(a)
utils.ZeroOutTimestamps(b)
aJSON, err := json.Marshal(a)
if err != nil {
return "", err
Expand All @@ -71,6 +68,11 @@ func getDiff(a, b interface{}) (string, error) {
if err != nil {
return "", err
}

// remove timestamps from JSON data without modifying the original data
aJSON = removeTimestamps(aJSON)
bJSON = removeTimestamps(bJSON)

d, err := differ.Compare(aJSON, bJSON)
if err != nil {
return "", err
Expand All @@ -87,6 +89,20 @@ func getDiff(a, b interface{}) (string, error) {
return diffString, err
}

func removeTimestamps(jsonData []byte) []byte {
var dataMap map[string]interface{}
if err := json.Unmarshal(jsonData, &dataMap); err != nil {
return jsonData
}
delete(dataMap, "created_at")
delete(dataMap, "updated_at")
modifiedJSON, err := json.Marshal(dataMap)
if err != nil {
return jsonData
}
return modifiedJSON
}

type EnvVar struct {
Key string
Value string
Expand Down
Loading

0 comments on commit 50b05dd

Please sign in to comment.