From 45a6d5b8ea4fa03a68060d98afc7ea63bea5d1b1 Mon Sep 17 00:00:00 2001 From: Martin Martinez Rivera Date: Wed, 18 Dec 2019 16:01:16 -0800 Subject: [PATCH] Allow overwriting values of predicates of type uid (#4411) When introducing the single uid type, mutations to those predicates were disallowed to prevent existing users from losing data due to the change in syntax. This PR changes the behavior to allow values to be overwritten directy instead of requiring a delete and an add operation. --- dgraph/cmd/alpha/run_test.go | 36 +----------------------------------- posting/index.go | 4 ---- posting/list.go | 33 --------------------------------- 3 files changed, 1 insertion(+), 72 deletions(-) diff --git a/dgraph/cmd/alpha/run_test.go b/dgraph/cmd/alpha/run_test.go index 5b14be8ab61..da4fba9e9ba 100644 --- a/dgraph/cmd/alpha/run_test.go +++ b/dgraph/cmd/alpha/run_test.go @@ -388,41 +388,7 @@ func TestMutationSingleUid(t *testing.T) { } } ` - require.Error(t, runMutation(m)) -} - -// Verify multiple uids are allowed after mutation. -func TestSchemaMutationUid(t *testing.T) { - // reset Schema - require.NoError(t, schema.ParseBytes([]byte(""), 1)) - - var s1 = ` - friend: uid . - ` - require.NoError(t, alterSchema(s1)) - var m1 = ` - { - set { - <0x1> <0x2> . - <0x1> <0x3> . - } - } - ` - require.Error(t, runMutation(m1)) - - var s2 = ` - friend: [uid] . - ` - require.NoError(t, alterSchema(s2)) - var m2 = ` - { - set { - <0x1> <0x2> . - <0x1> <0x3> . - } - } - ` - require.NoError(t, runMutation(m2)) + require.NoError(t, runMutation(m)) } // Verify a list uid predicate cannot be converted to a single-element predicate. diff --git a/posting/index.go b/posting/index.go index f7757c4b9a3..4f169515d77 100644 --- a/posting/index.go +++ b/posting/index.go @@ -318,10 +318,6 @@ func (txn *Txn) addMutationHelper(ctx context.Context, l *List, doUpdateIndex bo "Acquired lock %v %v %v", dur, t.Attr, t.Entity) } - if err := l.canMutateUid(txn, t); err != nil { - return val, found, emptyCountParams, err - } - if doUpdateIndex { // Check original value BEFORE any mutation actually happens. val, found, err = l.findValue(txn.StartTs, fingerprintEdge(t)) diff --git a/posting/list.go b/posting/list.go index edb901a0a78..217508f8b51 100644 --- a/posting/list.go +++ b/posting/list.go @@ -365,39 +365,6 @@ func fingerprintEdge(t *pb.DirectedEdge) uint64 { return id } -// canMutateUid returns an error if all the following conditions are met. -// * Predicate is of type UidID. -// * Predicate is not set to a list of UIDs in the schema. -// * The existing posting list has an entry that does not match the proposed -// mutation's UID. -// In this case, the user should delete the existing predicate and retry, or mutate -// the schema to allow for multiple UIDs. This method is necessary to support UID -// predicates with single values because previously all UID predicates were -// considered lists. -// This functions returns a nil error in all other cases. -func (l *List) canMutateUid(txn *Txn, edge *pb.DirectedEdge) error { - l.AssertRLock() - - if types.TypeID(edge.ValueType) != types.UidID { - return nil - } - - if schema.State().IsList(edge.Attr) { - return nil - } - - return l.iterate(txn.StartTs, 0, func(obj *pb.Posting) error { - if obj.Uid != edge.GetValueId() { - return errors.Errorf( - "cannot add value with uid %x to predicate %s because one of the existing "+ - "values does not match this uid, either delete the existing values first or "+ - "modify the schema to '%s: [uid]'", - edge.GetValueId(), edge.Attr, edge.Attr) - } - return nil - }) -} - func (l *List) addMutation(ctx context.Context, txn *Txn, t *pb.DirectedEdge) error { l.Lock() defer l.Unlock()