Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for adding of relational fields #1766

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
errParsingFailed string = "failed to parse argument"
errUninitializeProperty string = "invalid state, required property is uninitialized"
errMaxTxnRetries string = "reached maximum transaction reties"
errRelationOneSided string = "relation must be defined on both schemas"
)

// Errors returnable from this package.
Expand All @@ -43,6 +44,7 @@ var (
ErrMalformedDocKey = errors.New("malformed DocKey, missing either version or cid")
ErrInvalidDocKeyVersion = errors.New("invalid DocKey version")
ErrMaxTxnRetries = errors.New(errMaxTxnRetries)
ErrRelationOneSided = errors.New(errRelationOneSided)
)

// NewErrFieldNotExist returns an error indicating that the given field does not exist.
Expand Down Expand Up @@ -97,3 +99,11 @@ func NewErrUninitializeProperty(host string, propertyName string) error {
func NewErrMaxTxnRetries(inner error) error {
return errors.Wrap(errMaxTxnRetries, inner)
}

func NewErrRelationOneSided(fieldName string, typeName string) error {
return errors.New(
errRelationOneSided,
errors.NewKV("Field", fieldName),
errors.NewKV("Type", typeName),
)
}
149 changes: 138 additions & 11 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ func (db *db) createCollection(
func (db *db) updateCollection(
ctx context.Context,
txn datastore.Txn,
existingDescriptionsByName map[string]client.CollectionDescription,
proposedDescriptionsByName map[string]client.CollectionDescription,
desc client.CollectionDescription,
) (client.Collection, error) {
hasChanged, err := db.validateUpdateCollection(ctx, txn, desc)
hasChanged, err := db.validateUpdateCollection(ctx, txn, existingDescriptionsByName, proposedDescriptionsByName, desc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -311,17 +313,18 @@ func (db *db) updateCollection(
func (db *db) validateUpdateCollection(
ctx context.Context,
txn datastore.Txn,
existingDescriptionsByName map[string]client.CollectionDescription,
proposedDescriptionsByName map[string]client.CollectionDescription,
proposedDesc client.CollectionDescription,
) (bool, error) {
existingCollection, err := db.getCollectionByName(ctx, txn, proposedDesc.Name)
if err != nil {
if errors.Is(err, ds.ErrNotFound) {
// Original error is quite unhelpful to users at the moment so we return a custom one
return false, NewErrAddCollectionWithPatch(proposedDesc.Name)
}
return false, err
if proposedDesc.Name == "" {
return false, ErrCollectionNameEmpty
}

existingDesc, collectionExists := existingDescriptionsByName[proposedDesc.Name]
if !collectionExists {
return false, NewErrAddCollectionWithPatch(proposedDesc.Name)
}
existingDesc := existingCollection.Description()

if proposedDesc.ID != existingDesc.ID {
return false, NewErrCollectionIDDoesntMatch(proposedDesc.Name, existingDesc.ID, proposedDesc.ID)
Expand All @@ -346,7 +349,7 @@ func (db *db) validateUpdateCollection(
return false, ErrCannotSetVersionID
}

hasChangedFields, err := validateUpdateCollectionFields(existingDesc, proposedDesc)
hasChangedFields, err := validateUpdateCollectionFields(proposedDescriptionsByName, existingDesc, proposedDesc)
if err != nil {
return hasChangedFields, err
}
Expand All @@ -356,6 +359,7 @@ func (db *db) validateUpdateCollection(
}

func validateUpdateCollectionFields(
descriptionsByName map[string]client.CollectionDescription,
existingDesc client.CollectionDescription,
proposedDesc client.CollectionDescription,
) (bool, error) {
Expand Down Expand Up @@ -386,7 +390,130 @@ func validateUpdateCollectionFields(

if !fieldAlreadyExists && (proposedField.Kind == client.FieldKind_FOREIGN_OBJECT ||
proposedField.Kind == client.FieldKind_FOREIGN_OBJECT_ARRAY) {
return false, NewErrCannotAddRelationalField(proposedField.Name, proposedField.Kind)
if proposedField.Schema == "" {
return false, NewErrRelationalFieldMissingSchema(proposedField.Name, proposedField.Kind)
}

relatedDesc, relatedDescFound := descriptionsByName[proposedField.Schema]

if !relatedDescFound {
return false, NewErrSchemaNotFound(proposedField.Name, proposedField.Schema)
}

if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT {
if !proposedField.RelationType.IsSet(client.Relation_Type_ONE) ||
!(proposedField.RelationType.IsSet(client.Relation_Type_ONEONE) ||
proposedField.RelationType.IsSet(client.Relation_Type_ONEMANY)) {
return false, NewErrRelationalFieldInvalidRelationType(
proposedField.Name,
fmt.Sprintf(
"%v and %v or %v, with optionally %v",
client.Relation_Type_ONE,
client.Relation_Type_ONEONE,
client.Relation_Type_ONEMANY,
client.Relation_Type_Primary,
),
proposedField.RelationType,
)
}
}

if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT_ARRAY {
if !proposedField.RelationType.IsSet(client.Relation_Type_MANY) ||
!proposedField.RelationType.IsSet(client.Relation_Type_ONEMANY) {
return false, NewErrRelationalFieldInvalidRelationType(
proposedField.Name,
client.Relation_Type_MANY|client.Relation_Type_ONEMANY,
proposedField.RelationType,
)
}
}

if proposedField.RelationName == "" {
return false, NewErrRelationalFieldMissingRelationName(proposedField.Name)
}

if proposedField.RelationType.IsSet(client.Relation_Type_Primary) {
if proposedField.Kind == client.FieldKind_FOREIGN_OBJECT_ARRAY {
return false, NewErrPrimarySideOnMany(proposedField.Name)
}

idFieldName := proposedField.Name + "_id"
idField, idFieldFound := proposedDesc.Schema.GetField(idFieldName)
if !idFieldFound {
return false, NewErrRelationalFieldMissingIDField(proposedField.Name, idFieldName)
}

if idField.Kind != client.FieldKind_DocKey {
return false, NewErrRelationalFieldIDInvalidType(idField.Name, client.FieldKind_DocKey, idField.Kind)
}

if idField.RelationType != client.Relation_Type_INTERNAL_ID {
return false, NewErrRelationalFieldInvalidRelationType(
idField.Name,
client.Relation_Type_INTERNAL_ID,
idField.RelationType,
)
}

if idField.RelationName == "" {
return false, NewErrRelationalFieldMissingRelationName(idField.Name)
}
}

var relatedFieldFound bool
var relatedField client.FieldDescription
for _, field := range relatedDesc.Schema.Fields {
if field.RelationName == proposedField.RelationName &&
!field.RelationType.IsSet(client.Relation_Type_INTERNAL_ID) &&
!(relatedDesc.Name == proposedDesc.Name && field.Name == proposedField.Name) {
relatedFieldFound = true
relatedField = field
break
}
}

if !relatedFieldFound {
return false, client.NewErrRelationOneSided(proposedField.Name, proposedField.Schema)
}

if !(proposedField.RelationType.IsSet(client.Relation_Type_Primary) ||
relatedField.RelationType.IsSet(client.Relation_Type_Primary)) {
return false, NewErrPrimarySideNotDefined(proposedField.RelationName)
}

if proposedField.RelationType.IsSet(client.Relation_Type_Primary) &&
relatedField.RelationType.IsSet(client.Relation_Type_Primary) {
return false, NewErrBothSidesPrimary(proposedField.RelationName)
}

if proposedField.RelationType.IsSet(client.Relation_Type_ONEONE) &&
relatedField.Kind != client.FieldKind_FOREIGN_OBJECT {
return false, NewErrRelatedFieldKindMismatch(
proposedField.RelationName,
client.FieldKind_FOREIGN_OBJECT,
relatedField.Kind,
)
}

if proposedField.RelationType.IsSet(client.Relation_Type_ONEMANY) &&
proposedField.Kind == client.FieldKind_FOREIGN_OBJECT &&
relatedField.Kind != client.FieldKind_FOREIGN_OBJECT_ARRAY {
return false, NewErrRelatedFieldKindMismatch(
proposedField.RelationName,
client.FieldKind_FOREIGN_OBJECT_ARRAY,
relatedField.Kind,
)
}

if proposedField.RelationType.IsSet(client.Relation_Type_ONEONE) &&
!relatedField.RelationType.IsSet(client.Relation_Type_ONEONE) {
return false, NewErrRelatedFieldRelationTypeMismatch(
proposedField.RelationName,
client.Relation_Type_ONEONE,
relatedField.RelationType,
)
}
}

if _, isDuplicate := newFieldNames[proposedField.Name]; isDuplicate {
Expand Down
Loading