Skip to content

Commit

Permalink
Merge pull request #2115 from josephschorr/metadata-check
Browse files Browse the repository at this point in the history
Small improvements for transaction metadata
  • Loading branch information
vroldanbet authored Nov 6, 2024
2 parents bbc0be2 + ff25914 commit 686ae94
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/datastore/crdb/crdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (cds *crdbDatastore) ReadWriteTx(
}

// If metadata is to be attached, write that row now.
if config.Metadata != nil {
if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 {
expiresAt := time.Now().Add(cds.gcWindow).Add(1 * time.Minute)
insertTransactionMetadata := psql.Insert(tableTransactionMetadata).
Columns(colExpiresAt, colMetadata).
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/memdb/memdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func (mdb *memdbDatastore) ReadWriteTx(

tracked := common.NewChanges(revisions.TimestampIDKeyFunc, datastore.WatchRelationships|datastore.WatchSchema, 0)
if tx != nil {
if config.Metadata != nil {
if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 {
if err := tracked.SetRevisionMetadata(ctx, newRevision, config.Metadata.AsMap()); err != nil {
return datastore.NoRevision, err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (pgd *pgDatastore) ReadWriteTx(
err = wrapError(pgx.BeginTxFunc(ctx, pgd.writePool, pgx.TxOptions{IsoLevel: pgx.Serializable}, func(tx pgx.Tx) error {
var err error
var metadata map[string]any
if config.Metadata != nil {
if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 {
metadata = config.Metadata.AsMap()
}

Expand Down
2 changes: 1 addition & 1 deletion internal/datastore/spanner/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func (sd *spannerDatastore) ReadWriteTx(ctx context.Context, fn datastore.TxUser
return &traceableRTX{delegate: spannerRWT}
}

if config.Metadata != nil {
if config.Metadata != nil && len(config.Metadata.GetFields()) > 0 {
// Insert the metadata into the transaction metadata table.
mutation := spanner.Insert(tableTransactionMetadata,
[]string{colTransactionTag, colMetadata},
Expand Down
5 changes: 5 additions & 0 deletions pkg/tuple/core_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tuple

import (
"strings"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -118,9 +119,13 @@ func TestCoreRelationToString(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
got, err := CoreRelationToString(tt.input)
require.NoError(t, err)

// Golang randomly injects spaces.
got = strings.ReplaceAll(got, " ", "")
require.Equal(t, tt.expected, got)

got = MustCoreRelationToString(tt.input)
got = strings.ReplaceAll(got, " ", "")
require.Equal(t, tt.expected, got)
})
}
Expand Down
6 changes: 5 additions & 1 deletion pkg/tuple/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"sort"
"strings"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/structpb"

core "github.com/authzed/spicedb/pkg/proto/core/v1"
Expand Down Expand Up @@ -131,7 +132,10 @@ func StringCaveatContext(context *structpb.Struct) (string, error) {
return "", nil
}

contextBytes, err := context.MarshalJSON()
contextBytes, err := protojson.MarshalOptions{
Multiline: false,
Indent: "",
}.Marshal(context)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 686ae94

Please sign in to comment.