MCPcopy Create free account
hub / github.com/dgraph-io/dgraph / validateSchemaForUnique

Function validateSchemaForUnique

worker/mutation.go:460–504  ·  view source on GitHub ↗
(prevSchema pb.SchemaUpdate, currentSchema *pb.SchemaUpdate)

Source from the content-addressed store, hash-verified

458}
459
460func validateSchemaForUnique(prevSchema pb.SchemaUpdate, currentSchema *pb.SchemaUpdate) error {
461 validTokenizer := func(tokenizers []string) bool {
462 for _, value := range tokenizers {
463 if value == "hash" || value == "exact" || value == "int" {
464 return true
465 }
466 }
467 return false
468 }
469 if prevSchema.Predicate != "" {
470 if prevSchema.Upsert && !currentSchema.Upsert {
471 return errors.Errorf("could not drop @upsert from [%v] predicate when @unique directive specified",
472 x.ParseAttr(currentSchema.Predicate))
473 }
474
475 if prevSchema.Unique && !validTokenizer(currentSchema.Tokenizer) {
476 return errors.Errorf("could not drop index %v from [%v] predicate when @unique directive specified",
477 prevSchema.Tokenizer, x.ParseAttr(currentSchema.Predicate))
478 }
479 }
480 if !currentSchema.Upsert {
481 currentSchema.Upsert = true
482 }
483 switch currentSchema.ValueType {
484 case pb.Posting_STRING:
485 if len(currentSchema.Tokenizer) == 0 ||
486 (len(currentSchema.Tokenizer) > 0 && !validTokenizer(currentSchema.Tokenizer)) {
487
488 return errors.Errorf("index for predicate [%v] is missing, add either hash or exact index with @unique",
489 x.ParseAttr(currentSchema.Predicate))
490 } else {
491 return nil
492 }
493
494 case pb.Posting_INT:
495 if len(currentSchema.Tokenizer) == 0 {
496 return errors.Errorf("index for predicate [%v] is missing, add int index with @unique",
497 x.ParseAttr(currentSchema.Predicate))
498 } else {
499 return nil
500 }
501 }
502
503 return errors.Errorf("@unique directive not supported on [%v] type predicate", currentSchema.ValueType.String())
504}
505
506// ValidateAndConvert checks compatibility or converts to the schema type if the storage type is
507// specified. If no storage type is specified then it converts to the schema type.

Callers 1

checkSchemaFunction · 0.85

Calls 3

ParseAttrFunction · 0.92
ErrorfMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…