Skip to content

Commit

Permalink
WIP - Prevent mutations from secondary side of relation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSisley committed Oct 10, 2024
1 parent bc68f57 commit 7a8a207
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 158 deletions.
10 changes: 10 additions & 0 deletions client/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,16 @@ func (doc *Document) Set(field string, value any) error {
if !exists {
return NewErrFieldNotExist(field)
}

if fd.Kind == FieldKind_DocID && strings.HasSuffix(field, request.RelatedObjectID) {
objFieldName := strings.TrimSuffix(field, request.RelatedObjectID)
ofd, exists := doc.collectionDefinition.GetFieldByName(objFieldName)
if exists && !ofd.IsPrimaryRelation {
return NewErrCannotSetRelationFromSecondarySide(field)
}

}

Check failure on line 703 in client/document.go

View workflow job for this annotation

GitHub Actions / Lint GoLang job

unnecessary trailing newline (whitespace)

if fd.Kind.IsObject() && !fd.Kind.IsArray() {
if !strings.HasSuffix(field, request.RelatedObjectID) {
field = field + request.RelatedObjectID
Expand Down
5 changes: 5 additions & 0 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
errCanNotTurnNormalValueIntoArray string = "can not turn normal value into array"
errCanNotMakeNormalNilFromFieldKind string = "can not make normal nil from field kind"
errFailedToParseKind string = "failed to parse kind"
errCannotSetRelationFromSecondarySide string = "cannot set relation from secondary side"
)

// Errors returnable from this package.
Expand Down Expand Up @@ -190,3 +191,7 @@ func ReviveError(message string) error {
return fmt.Errorf("%s", message)
}
}

func NewErrCannotSetRelationFromSecondarySide(name string) error {
return errors.New(errCannotSetRelationFromSecondarySide, errors.NewKV("Name", name))
}
8 changes: 8 additions & 0 deletions internal/request/graphql/schema/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ func (g *Generator) buildMutationInputTypes(collections []client.CollectionDefin
continue
}

if field.Kind == client.FieldKind_DocID && strings.HasSuffix(field.Name, request.RelatedObjectID) {
objFieldName := strings.TrimSuffix(field.Name, request.RelatedObjectID)
ofd, exists := collection.GetFieldByName(objFieldName)
if exists && !ofd.IsPrimaryRelation {
continue
}
}

var ttype gql.Type
if field.Kind.IsObject() {
if field.Kind.IsArray() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,6 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromPrimarySide(t *testing.T) {
}

func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T) {
author1ID := "bae-53eff350-ad8e-532c-b72d-f95c4f47909c"
author2ID := "bae-c058cfd4-259f-5b08-975d-106f13a143d5"

test := testUtils.TestCase{
Expand All @@ -280,13 +279,9 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T)
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: fmt.Sprintf(
`{
"name": "Painted House",
"author_id": "%s"
}`,
author1ID,
),
Doc: `{
"name": "Painted House"
}`,
},
testUtils.UpdateDoc{
CollectionID: 0,
Expand All @@ -297,7 +292,7 @@ func TestMutationUpdateOneToOne_RelationIDToLinkFromSecondarySide(t *testing.T)
}`,
author2ID,
),
ExpectedError: "target document is already linked to another document.",
ExpectedError: "cannot set relation from secondary side",
},
},
}
Expand Down
36 changes: 18 additions & 18 deletions tests/integration/query/one_to_one_multiple/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,6 @@ func TestQueryOneToOneMultiple_FromMixedPrimaryAndSecondary(t *testing.T) {
}
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Old Publisher"
}`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "New Publisher"
}`,
},
testUtils.CreateDoc{
CollectionID: 1,
Doc: `{
Expand All @@ -170,17 +158,29 @@ func TestQueryOneToOneMultiple_FromMixedPrimaryAndSecondary(t *testing.T) {
testUtils.CreateDoc{
CollectionID: 2,
DocMap: map[string]any{
"name": "Painted House",
"publisher_id": testUtils.NewDocIndex(0, 0),
"author_id": testUtils.NewDocIndex(1, 0),
"name": "Painted House",
"author_id": testUtils.NewDocIndex(1, 0),
},
},
testUtils.CreateDoc{
CollectionID: 2,
DocMap: map[string]any{
"name": "Theif Lord",
"publisher_id": testUtils.NewDocIndex(0, 1),
"author_id": testUtils.NewDocIndex(1, 1),
"name": "Theif Lord",
"author_id": testUtils.NewDocIndex(1, 1),
},
},
testUtils.CreateDoc{
CollectionID: 0,
DocMap: map[string]any{
"name": "Old Publisher",
"printed_id": testUtils.NewDocIndex(2, 0),
},
},
testUtils.CreateDoc{
CollectionID: 0,
DocMap: map[string]any{
"name": "New Publisher",
"printed_id": testUtils.NewDocIndex(2, 1),
},
},
testUtils.Request{
Expand Down
28 changes: 14 additions & 14 deletions tests/integration/query/one_to_one_to_many/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,16 @@ func TestQueryOneToOneToManyFromSecondaryOnOneToMany(t *testing.T) {
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Indicator1"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "Observable1",
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "Observable1",
"indicator_id": testUtils.NewDocIndex(0, 0),
"name": "Indicator1",
"observable_id": testUtils.NewDocIndex(1, 0),
},
},
testUtils.CreateDoc{
Expand Down Expand Up @@ -192,16 +192,16 @@ func TestQueryOneToOneToManyFromSecondaryOnOneToOne(t *testing.T) {
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Indicator1"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "Observable1",
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "Observable1",
"indicator_id": testUtils.NewDocIndex(0, 0),
"name": "Indicator1",
"observable_id": testUtils.NewDocIndex(1, 0),
},
},
testUtils.CreateDoc{
Expand Down
100 changes: 50 additions & 50 deletions tests/integration/query/one_to_one_to_one/simple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,43 +40,43 @@ func TestQueryOneToOneToOne(t *testing.T) {
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Old Publisher"
}`,
CollectionID: 2,
DocMap: map[string]any{
"name": "John Grisham",
},
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "New Publisher"
}`,
CollectionID: 2,
DocMap: map[string]any{
"name": "Cornelia Funke",
},
},
testUtils.CreateDoc{
CollectionID: 1,
DocMap: map[string]any{
"name": "Painted House",
"publisher_id": testUtils.NewDocIndex(0, 0),
"name": "Painted House",
"author_id": testUtils.NewDocIndex(2, 0),
},
},
testUtils.CreateDoc{
CollectionID: 1,
DocMap: map[string]any{
"name": "Theif Lord",
"publisher_id": testUtils.NewDocIndex(0, 1),
"name": "Theif Lord",
"author_id": testUtils.NewDocIndex(2, 1),
},
},
testUtils.CreateDoc{
CollectionID: 2,
CollectionID: 0,
DocMap: map[string]any{
"name": "John Grisham",
"published_id": testUtils.NewDocIndex(1, 0),
"name": "Old Publisher",
"printed_id": testUtils.NewDocIndex(1, 0),
},
},
testUtils.CreateDoc{
CollectionID: 2,
CollectionID: 0,
DocMap: map[string]any{
"name": "Cornelia Funke",
"published_id": testUtils.NewDocIndex(1, 1),
"name": "New Publisher",
"printed_id": testUtils.NewDocIndex(1, 1),
},
},
testUtils.Request{
Expand Down Expand Up @@ -156,31 +156,31 @@ func TestQueryOneToOneToOneSecondaryThenPrimary(t *testing.T) {
}`,
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 2,
DocMap: map[string]any{
"name": "Painted House",
"publisher_id": testUtils.NewDocIndex(0, 0),
"name": "John Grisham",
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 2,
DocMap: map[string]any{
"name": "Theif Lord",
"publisher_id": testUtils.NewDocIndex(0, 1),
"name": "Cornelia Funke",
},
},
testUtils.CreateDoc{
CollectionID: 2,
CollectionID: 1,
DocMap: map[string]any{
"name": "John Grisham",
"published_id": testUtils.NewDocIndex(1, 0),
"name": "Painted House",
"publisher_id": testUtils.NewDocIndex(0, 0),
"author_id": testUtils.NewDocIndex(2, 0),
},
},
testUtils.CreateDoc{
CollectionID: 2,
CollectionID: 1,
DocMap: map[string]any{
"name": "Cornelia Funke",
"published_id": testUtils.NewDocIndex(1, 1),
"name": "Theif Lord",
"publisher_id": testUtils.NewDocIndex(0, 1),
"author_id": testUtils.NewDocIndex(2, 1),
},
},
testUtils.Request{
Expand Down Expand Up @@ -248,29 +248,29 @@ func TestQueryOneToOneToOnePrimaryThenSecondary(t *testing.T) {
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Old Publisher"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "Painted House",
},
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "New Publisher"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "Theif Lord",
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "Painted House",
"publisher_id": testUtils.NewDocIndex(0, 0),
"name": "Old Publisher",
"printed_id": testUtils.NewDocIndex(1, 0),
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "Theif Lord",
"publisher_id": testUtils.NewDocIndex(0, 1),
"name": "New Publisher",
"printed_id": testUtils.NewDocIndex(1, 1),
},
},
testUtils.CreateDoc{
Expand Down Expand Up @@ -302,20 +302,20 @@ func TestQueryOneToOneToOnePrimaryThenSecondary(t *testing.T) {
Results: map[string]any{
"Publisher": []map[string]any{
{
"name": "Old Publisher",
"name": "New Publisher",
"printed": map[string]any{
"name": "Painted House",
"name": "Theif Lord",
"author": map[string]any{
"name": "John Grisham",
"name": "Cornelia Funke",
},
},
},
{
"name": "New Publisher",
"name": "Old Publisher",
"printed": map[string]any{
"name": "Theif Lord",
"name": "Painted House",
"author": map[string]any{
"name": "Cornelia Funke",
"name": "John Grisham",
},
},
},
Expand Down
Loading

0 comments on commit 7a8a207

Please sign in to comment.