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

core/storage: fix nil data unmarshal #4734

Merged
merged 1 commit into from
Nov 10, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 12 additions & 10 deletions pkg/storage/postgres/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,20 @@ func TestBackend(t *testing.T) {
})

t.Run("delete", func(t *testing.T) {
serverVersion, err := backend.Put(ctx, []*databroker.Record{
{
Type: "test-1",
Id: "r3",
Data: protoutil.NewAny(protoutil.NewStructMap(map[string]*structpb.Value{
"k1": protoutil.NewStructString("v1"),
})),
DeletedAt: timestamppb.Now(),
},
})
serverVersion, err := backend.Put(ctx, []*databroker.Record{{
Type: "test-1",
Id: "r3",
DeletedAt: timestamppb.Now(),
}})
assert.NotEqual(t, 0, serverVersion)
assert.NoError(t, err)

stream, err := backend.Sync(ctx, "test-1", serverVersion, 0)
require.NoError(t, err)
t.Cleanup(func() { _ = stream.Close() })
records, err := storage.RecordStreamToList(stream)
require.NoError(t, err)
assert.NotEmpty(t, records)
})

t.Run("capacity", func(t *testing.T) {
Expand Down
18 changes: 11 additions & 7 deletions pkg/storage/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,17 @@ func getNextChangedRecord(ctx context.Context, q querier, recordType string, aft
return nil, fmt.Errorf("error querying next changed record: %w", err)
}

a, err := protoutil.UnmarshalAnyJSON(data)
if isUnknownType(err) {
a = protoutil.ToAny(protoutil.ToStruct(map[string]string{
"id": recordID,
}))
} else if err != nil {
return nil, fmt.Errorf("error unmarshaling changed record data: %w", err)
// data may be nil if a record is deleted
var a *anypb.Any
if len(data) != 0 {
a, err = protoutil.UnmarshalAnyJSON(data)
if isUnknownType(err) {
a = protoutil.ToAny(protoutil.ToStruct(map[string]string{
"id": recordID,
}))
} else if err != nil {
return nil, fmt.Errorf("error unmarshaling changed record data: %w", err)
}
}

return &databroker.Record{
Expand Down