addQueryIfUnique adds dummy queries in the request for checking whether predicate is unique in the db
(qctx context.Context, qc *queryContext)
| 1862 | |
| 1863 | // addQueryIfUnique adds dummy queries in the request for checking whether predicate is unique in the db |
| 1864 | func addQueryIfUnique(qctx context.Context, qc *queryContext) error { |
| 1865 | if len(qc.gmuList) == 0 { |
| 1866 | return nil |
| 1867 | } |
| 1868 | |
| 1869 | ctx := context.WithValue(qctx, schema.IsWrite, false) |
| 1870 | namespace, err := x.ExtractNamespace(ctx) |
| 1871 | if err != nil { |
| 1872 | // It's okay to ignore this here. If namespace is not set, it could mean either there is no |
| 1873 | // authorization or it's trying to be bypassed. So the namespace is either 0 or the mutation would fail. |
| 1874 | glog.Errorf("Error while extracting namespace, assuming default %s", err) |
| 1875 | namespace = 0 |
| 1876 | } |
| 1877 | isGalaxyQuery := x.IsRootNsOperation(ctx) |
| 1878 | |
| 1879 | missingPreds := make(map[string]struct{}) |
| 1880 | for _, gmu := range qc.gmuList { |
| 1881 | for _, pred := range gmu.Set { |
| 1882 | currNs := namespace |
| 1883 | if isGalaxyQuery { |
| 1884 | currNs = pred.Namespace |
| 1885 | } |
| 1886 | if pred.Predicate == "dgraph.xid" { |
| 1887 | continue |
| 1888 | } |
| 1889 | fullPred := x.NamespaceAttr(currNs, pred.Predicate) |
| 1890 | if _, ok := schema.State().Get(ctx, fullPred); !ok { |
| 1891 | missingPreds[fullPred] = struct{}{} |
| 1892 | } |
| 1893 | } |
| 1894 | } |
| 1895 | |
| 1896 | repaired := make(map[string]bool) |
| 1897 | if len(missingPreds) > 0 { |
| 1898 | predList := make([]string, 0, len(missingPreds)) |
| 1899 | for p := range missingPreds { |
| 1900 | predList = append(predList, p) |
| 1901 | } |
| 1902 | |
| 1903 | schReq := &pb.SchemaRequest{Predicates: predList} |
| 1904 | remoteNodes, err := worker.GetSchemaOverNetwork(ctx, schReq) |
| 1905 | if err != nil { |
| 1906 | return errors.Wrapf(err, "unique validation failed to fetch schema for predicates %v", predList) |
| 1907 | } |
| 1908 | |
| 1909 | for _, node := range remoteNodes { |
| 1910 | repaired[node.Predicate] = node.Unique |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | qc.uniqueVars = map[uint64]uniquePredMeta{} |
| 1915 | for gmuIndex, gmu := range qc.gmuList { |
| 1916 | var buildQuery strings.Builder |
| 1917 | for rdfIndex, pred := range gmu.Set { |
| 1918 | if isGalaxyQuery { |
| 1919 | // The caller should make sure that the directed edges contain the namespace we want |
| 1920 | // to insert into. |
| 1921 | namespace = pred.Namespace |
no test coverage detected