Repository: dgraph-io/dgraph
File: edgraph/server.go (Go)

Code before:
		namespace = 0
	}
	isGalaxyQuery := x.IsRootNsOperation(ctx)

	qc.uniqueVars = map[uint64]uniquePredMeta{}
	repaired := make(map[string]bool)
	for gmuIndex, gmu := range qc.gmuList {
		var buildQuery strings.Builder
		for rdfIndex, pred := range gmu.Set {
			if isGalaxyQuery {
				// The caller should make sure that the directed edges contain the namespace we want
				// to insert into.
				namespace = pred.Namespace
			}
			if pred.Predicate != "dgraph.xid" {
				// [TODO] Don't check if it's dgraph.xid. It's a bug as this node might not be aware
				// of the schema for the given predicate. This is a bug issue for dgraph.xid hence
				// we are bypassing it manually until the bug is fixed.
				fullPred := x.NamespaceAttr(namespace, pred.Predicate)
				predSchema, ok := schema.State().Get(ctx, fullPred)

				if !ok {
					unique, predChecked := repaired[fullPred]
					if !predChecked {
						schReq := &pb.SchemaRequest{Predicates: []string{fullPred}}
						remoteNodes, err := worker.GetSchemaOverNetwork(ctx, schReq)
						if err == nil && len(remoteNodes) > 0 {
							predSchema.Unique = remoteNodes[0].Unique
							ok = true
						}
						repaired[fullPred] = predSchema.Unique
					} else {
						predSchema.Unique = unique
						ok = true
					}
				}

				if !ok || !predSchema.Unique {
					continue
				}
			}

			// Wrapping predicateName with angle brackets ensures that if the predicate contains any non-Latin letters,
			// the unique query will not fail. Additionally,
			// it helps ensure that non-Latin predicate names are properly formatted
			// during the automatic serialization of a structure into JSON.
			predicateName := fmt.Sprintf("<%v>", pred.Predicate)
			if pred.Lang != "" {
				predicateName = fmt.Sprintf("%v@%v", predicateName, pred.Lang)
			}


Review comment (bug):
@matthewmcneely when GetSchemaOverNetwork fails we currently skip the unique check and let the mutation through, so duplicates can still happen on schema fetch failure.
Should we instead fail the mutation when the schema fetch fails, or keep the current behavior?

Code after:
		namespace = 0
	}
	isGalaxyQuery := x.IsRootNsOperation(ctx)

	missingPreds := make(map[string]struct{})
	for _, gmu := range qc.gmuList {
		for _, pred := range gmu.Set {
			currNs := namespace
			if isGalaxyQuery {
				currNs = pred.Namespace
			}
			if pred.Predicate == "dgraph.xid" {
				continue
			}
			fullPred := x.NamespaceAttr(currNs, pred.Predicate)
			if _, ok := schema.State().Get(ctx, fullPred); !ok {
				missingPreds[fullPred] = struct{}{}
			}
		}
	}

	repaired := make(map[string]bool)
	if len(missingPreds) > 0 {
		predList := make([]string, 0, len(missingPreds))
		for p := range missingPreds {
			predList = append(predList, p)
		}

		schReq := &pb.SchemaRequest{Predicates: predList}
		remoteNodes, err := worker.GetSchemaOverNetwork(ctx, schReq)
		if err != nil {
			return errors.Wrapf(err, "unique validation failed to fetch schema for predicates %v", predList)
		}

		for _, node := range remoteNodes {
			repaired[node.Predicate] = node.Unique
		}
	}

	qc.uniqueVars = map[uint64]uniquePredMeta{}
	for gmuIndex, gmu := range qc.gmuList {
		var buildQuery strings.Builder
		for rdfIndex, pred := range gmu.Set {
			if isGalaxyQuery {
				// The caller should make sure that the directed edges contain the namespace we want
				// to insert into.
				namespace = pred.Namespace
			}
			if pred.Predicate != "dgraph.xid" {
				// [TODO] Don't check if it's dgraph.xid. It's a bug as this node might not be aware
				// of the schema for the given predicate. This is a bug issue for dgraph.xid hence