Skip to content

Commit

Permalink
internal/services/v1: return ZedTokens on schema operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jakedt committed May 31, 2023
1 parent 10d4cb8 commit 491a991
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
8 changes: 6 additions & 2 deletions internal/services/v1/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/authzed/spicedb/pkg/schemadsl/compiler"
"github.com/authzed/spicedb/pkg/schemadsl/generator"
"github.com/authzed/spicedb/pkg/schemadsl/input"
"github.com/authzed/spicedb/pkg/zedtoken"
)

// NewSchemaServer creates a SchemaServiceServer instance.
Expand Down Expand Up @@ -88,6 +89,7 @@ func (ss *schemaServer) ReadSchema(ctx context.Context, _ *v1.ReadSchemaRequest)

return &v1.ReadSchemaResponse{
SchemaText: schemaText,
ReadAt: zedtoken.MustNewFromRevision(headRevision),
}, nil
}

Expand All @@ -114,7 +116,7 @@ func (ss *schemaServer) WriteSchema(ctx context.Context, in *v1.WriteSchemaReque
}

// Update the schema.
_, err = ds.ReadWriteTx(ctx, func(rwt datastore.ReadWriteTransaction) error {
revision, err := ds.ReadWriteTx(ctx, func(rwt datastore.ReadWriteTransaction) error {
applied, err := shared.ApplySchemaChanges(ctx, rwt, validated)
if err != nil {
return err
Expand All @@ -128,5 +130,7 @@ func (ss *schemaServer) WriteSchema(ctx context.Context, in *v1.WriteSchemaReque
return nil, shared.RewriteError(ctx, err)
}

return &v1.WriteSchemaResponse{}, nil
return &v1.WriteSchemaResponse{
WrittenAt: zedtoken.MustNewFromRevision(revision),
}, nil
}
26 changes: 20 additions & 6 deletions internal/services/v1/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func TestSchemaWriteNoPrefix(t *testing.T) {
conn, cleanup, _, _ := testserver.NewTestServer(require.New(t), 0, memdb.DisableGC, true, tf.EmptyDatastore)
t.Cleanup(cleanup)
client := v1.NewSchemaServiceClient(conn)
_, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
resp, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
Schema: `definition user {}`,
})
require.NoError(t, err)
require.NotNil(t, resp.WrittenAt)
require.NotEmpty(t, resp.WrittenAt.Token)
}

func TestSchemaWriteInvalidSchema(t *testing.T) {
Expand Down Expand Up @@ -69,14 +71,18 @@ func TestSchemaWriteAndReadBack(t *testing.T) {

userSchema := "caveat someCaveat(somecondition int) {\n\tsomecondition == 42\n}\n\ndefinition example/document {\n\trelation viewer: example/user | example/user with someCaveat\n}\n\ndefinition example/user {}"

_, err = client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
writeResp, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
Schema: userSchema,
})
require.NoError(t, err)
require.NotNil(t, writeResp.WrittenAt)
require.NotEmpty(t, writeResp.WrittenAt.Token)

readback, err := client.ReadSchema(context.Background(), &v1.ReadSchemaRequest{})
require.NoError(t, err)
require.Equal(t, userSchema, readback.SchemaText)
require.NotNil(t, readback.ReadAt)
require.NotEmpty(t, readback.ReadAt.Token)
}

func TestSchemaDeleteRelation(t *testing.T) {
Expand All @@ -86,7 +92,7 @@ func TestSchemaDeleteRelation(t *testing.T) {
v1client := v1.NewPermissionsServiceClient(conn)

// Write a basic schema.
_, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
writeResp, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
Schema: `definition example/user {}
definition example/document {
Expand All @@ -95,6 +101,8 @@ func TestSchemaDeleteRelation(t *testing.T) {
}`,
})
require.NoError(t, err)
require.NotNil(t, writeResp.WrittenAt)
require.NotEmpty(t, writeResp.WrittenAt.Token)

// Write a relationship for one of the relations.
_, err = v1client.WriteRelationships(context.Background(), &v1.WriteRelationshipsRequest{
Expand All @@ -115,14 +123,16 @@ func TestSchemaDeleteRelation(t *testing.T) {
grpcutil.RequireStatus(t, codes.InvalidArgument, err)

// Attempt to delete the `anotherrelation` relation, which should succeed.
_, err = client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
updateResp, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
Schema: `definition example/user {}
definition example/document {
relation somerelation: example/user
}`,
})
require.Nil(t, err)
require.NotNil(t, updateResp.WrittenAt)
require.NotEmpty(t, updateResp.WrittenAt.Token)

// Delete the relationship.
_, err = v1client.WriteRelationships(context.Background(), &v1.WriteRelationshipsRequest{
Expand All @@ -133,12 +143,14 @@ func TestSchemaDeleteRelation(t *testing.T) {
require.Nil(t, err)

// Attempt to delete the `somerelation` relation, which should succeed.
_, err = client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
deleteRelResp, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
Schema: `definition example/user {}
definition example/document {}`,
})
require.Nil(t, err)
require.NotNil(t, deleteRelResp.WrittenAt)
require.NotEmpty(t, deleteRelResp.WrittenAt.Token)
}

func TestSchemaDeletePermission(t *testing.T) {
Expand Down Expand Up @@ -390,10 +402,12 @@ func TestSchemaEmpty(t *testing.T) {
require.Nil(t, err)

// Attempt to empty the schema, which should succeed.
_, err = client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
emptyResp, err := client.WriteSchema(context.Background(), &v1.WriteSchemaRequest{
Schema: ``,
})
require.Nil(t, err)
require.NotNil(t, emptyResp.WrittenAt)
require.NotEmpty(t, emptyResp.WrittenAt.Token)

// Ensure it was deleted.
_, err = client.ReadSchema(context.Background(), &v1.ReadSchemaRequest{})
Expand Down

0 comments on commit 491a991

Please sign in to comment.