Skip to content

Commit

Permalink
Add support for deleting non-existent UIDs in Upsert (#3412)
Browse files Browse the repository at this point in the history
  • Loading branch information
mangalaman93 committed Jun 18, 2019
1 parent 8ee387b commit ccc7734
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
39 changes: 39 additions & 0 deletions dgraph/cmd/alpha/upsert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,12 @@ upsert {
uid(u2) <name> "user2" .
uid(u1) <friend> uid(u2) .
}
delete {
uid(u3) <friend> uid(u1) .
uid(u1) <friend> uid(u3) .
uid(u3) <name> * .
}
}
query {
Expand All @@ -703,6 +709,10 @@ upsert {
user2(func: eq(email, "[email protected]")) {
u2 as uid
}
user3(func: eq(email, "[email protected]")) {
u3 as uid
}
}
}`
doUpsert := func(wg *sync.WaitGroup) {
Expand Down Expand Up @@ -755,3 +765,32 @@ upsert {
}`
z.CompareJSON(t, expected, res)
}

func TestUpsertDeleteNonExistent(t *testing.T) {
require.NoError(t, dropAll())
require.NoError(t, alterSchema(`
email: string @index(exact) @upsert .
name: string @index(exact) @lang .
friend: uid @reverse .`))

m := `
upsert {
mutation {
delete {
uid (u1) <friend> uid ( u2 ) .
}
}
query {
user1(func: eq(name@en, "user1")) {
u1 as uid
}
user2(func: eq(name@en, "user2")) {
u2 as uid
}
}
}`
_, _, _, err := mutationWithTs(m, "application/rdf", false, true, true, 0)
require.NoError(t, err)
}
16 changes: 15 additions & 1 deletion edgraph/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,22 @@ func (s *Server) doMutate(ctx context.Context, mu *api.Mutation, authorize bool)
return s
}

// Remove the mutations from gmu.Del when no UID was found
gmuDel := make([]*api.NQuad, 0, len(gmu.Del))
for _, nq := range gmu.Del {
nq.Subject = getNewVal(nq.Subject)
nq.ObjectId = getNewVal(nq.ObjectId)

if !strings.HasPrefix(nq.Subject, "_:uid(") &&
!strings.HasPrefix(nq.ObjectId, "_:uid(") {

gmuDel = append(gmuDel, nq)
}
}
gmu.Del = gmuDel

// update the values in mutation block from the query block.
for _, nq := range append(gmu.Set, gmu.Del...) {
for _, nq := range gmu.Set {
nq.Subject = getNewVal(nq.Subject)
nq.ObjectId = getNewVal(nq.ObjectId)
}
Expand Down

0 comments on commit ccc7734

Please sign in to comment.