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

Allow schema updates to reserved preds if the update is the same. #3143

Merged
merged 5 commits into from
Mar 15, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,9 @@ func (s *Server) Alter(ctx context.Context, op *api.Operation) (*api.Payload, er
}

for _, update := range result.Schemas {
// Reserved predicates cannot be altered.
if x.IsReservedPredicate(update.Predicate) {
// Reserved predicates cannot be altered but let the update go through
// if the update is equal to the existing one.
if schema.IsReservedPredicateChanged(update.Predicate, update) {
err := fmt.Errorf("predicate %s is reserved and is not allowed to be modified",
update.Predicate)
return nil, err
Expand Down
8 changes: 8 additions & 0 deletions ee/acl/acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,15 @@ var query = fmt.Sprintf(`

func alterReservedPredicates(t *testing.T, dg *dgo.Dgraph) {
ctx := context.Background()

// Test that alter requests are allowed if the new update is the same as
// the initial update for a reserved predicate.
err := dg.Alter(ctx, &api.Operation{
Schema: "dgraph.xid: string @index(exact) @upsert .",
})
require.NoError(t, err)

err = dg.Alter(ctx, &api.Operation{
Schema: "dgraph.xid: int .",
})
require.Error(t, err)
Expand Down
22 changes: 22 additions & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/dgraph-io/badger"
"github.com/golang/glog"
"github.com/golang/protobuf/proto"
"golang.org/x/net/trace"

"github.com/dgraph-io/dgraph/protos/pb"
Expand Down Expand Up @@ -458,6 +459,27 @@ func InitialSchema() []*pb.SchemaUpdate {
return initialSchema
}

// IsReservedPredicateChanged returns true if the initial update for the reserved
// predicate pred is different than the passed update.
func IsReservedPredicateChanged(pred string, update *pb.SchemaUpdate) bool {
// Return false for non-reserved predicates.
if !x.IsReservedPredicate(pred) {
return false
}

initialSchema := InitialSchema()
for _, original := range initialSchema {
if original.Predicate != pred {
continue
}
if proto.Equal(original, update) {
return false
}
return true
}
return true
}

func reset() {
pstate = new(state)
pstate.init()
Expand Down