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

Small improvements for transaction metadata #2115

Merged
merged 2 commits into from
Nov 6, 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 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
Loading