Skip to content

Commit

Permalink
Allow overwriting values of predicates of type uid (#4411)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
martinmr authored Dec 19, 2019
1 parent 8d968ca commit 45a6d5b
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 72 deletions.
36 changes: 1 addition & 35 deletions dgraph/cmd/alpha/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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> <friend> <0x2> .
<0x1> <friend> <0x3> .
}
}
`
require.Error(t, runMutation(m1))

var s2 = `
friend: [uid] .
`
require.NoError(t, alterSchema(s2))
var m2 = `
{
set {
<0x1> <friend> <0x2> .
<0x1> <friend> <0x3> .
}
}
`
require.NoError(t, runMutation(m2))
require.NoError(t, runMutation(m))
}

// Verify a list uid predicate cannot be converted to a single-element predicate.
Expand Down
4 changes: 0 additions & 4 deletions posting/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
33 changes: 0 additions & 33 deletions posting/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 45a6d5b

Please sign in to comment.