Fix jsonEq to handle PG JSONB key reordering
All checks were successful
CI/CD / detect-changes (pull_request) Successful in 4s
CI/CD / test-frontend (pull_request) Successful in 5s
CI/CD / test-go-pg (pull_request) Successful in 2m29s
CI/CD / test-sqlite (pull_request) Successful in 2m36s
CI/CD / build-and-deploy (pull_request) Successful in 1m32s

PG JSONB normalizes key order (alphabetical), so json.Compact alone
wasn't sufficient — {"approved":true,"notes":"LGTM"} stored as
{"notes":"LGTM","approved":true}. Unmarshal+remarshal normalizes
both sides before comparison.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-03-28 13:23:57 +00:00
parent 619e6a040a
commit fe176dbfdd

View File

@@ -14,17 +14,19 @@ import (
"switchboard-core/store/sqlite"
)
// jsonEq compares two JSON byte slices ignoring whitespace differences
// (PG JSONB normalizes spacing, SQLite preserves it verbatim).
// jsonEq compares two JSON byte slices ignoring whitespace and key order
// (PG JSONB normalizes spacing and may reorder keys).
func jsonEq(a, b json.RawMessage) bool {
var ca, cb bytes.Buffer
if err := json.Compact(&ca, a); err != nil {
var va, vb interface{}
if err := json.Unmarshal(a, &va); err != nil {
return false
}
if err := json.Compact(&cb, b); err != nil {
if err := json.Unmarshal(b, &vb); err != nil {
return false
}
return ca.String() == cb.String()
na, _ := json.Marshal(va)
nb, _ := json.Marshal(vb)
return bytes.Equal(na, nb)
}
// testStores returns an appropriate Stores for the current dialect.