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 85be86c
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 226 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
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@ func TestQueryOneToOneWithGroupRelatedIDAlias(t *testing.T) {
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Painted House"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "John Grisham",
},
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Go Guide for Rust developers"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "Andrew Lone",
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "John Grisham",
"published_id": testUtils.NewDocIndex(0, 0),
"name": "Painted House",
"author_id": testUtils.NewDocIndex(1, 0),
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "Andrew Lone",
"published_id": testUtils.NewDocIndex(0, 1),
"name": "Go Guide for Rust developers",
"author_id": testUtils.NewDocIndex(1, 1),
},
},
testUtils.Request{
Expand All @@ -74,24 +74,24 @@ func TestQueryOneToOneWithGroupRelatedIDAlias(t *testing.T) {
Results: map[string]any{
"Book": []map[string]any{
{
"author_id": "bae-fc7bf08d-9117-5acd-8b49-bc7431b1b238",
"author_id": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55",
"author": map[string]any{
"name": "John Grisham",
"name": "Andrew Lone",
},
"_group": []map[string]any{
{
"name": "Painted House",
"name": "Go Guide for Rust developers",
},
},
},
{
"author_id": "bae-fcb12812-4c38-574e-bc8b-91b37ee6cd9b",
"author_id": "bae-ee5973cf-73c3-558f-8aec-8b590b8e77cf",
"author": map[string]any{
"name": "Andrew Lone",
"name": "John Grisham",
},
"_group": []map[string]any{
{
"name": "Go Guide for Rust developers",
"name": "Painted House",
},
},
},
Expand Down
36 changes: 18 additions & 18 deletions tests/integration/query/one_to_one/with_group_related_id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,29 @@ func TestQueryOneToOneWithGroupRelatedID(t *testing.T) {
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Painted House"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "John Grisham",
},
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Go Guide for Rust developers"
}`,
CollectionID: 1,
DocMap: map[string]any{
"name": "Andrew Lone",
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "John Grisham",
"published_id": testUtils.NewDocIndex(0, 0),
"name": "Painted House",
"author_id": testUtils.NewDocIndex(1, 0),
},
},
testUtils.CreateDoc{
CollectionID: 1,
CollectionID: 0,
DocMap: map[string]any{
"name": "John Grisham",
"published_id": testUtils.NewDocIndex(0, 1),
"name": "Go Guide for Rust developers",
"author_id": testUtils.NewDocIndex(1, 1),
},
},
testUtils.Request{
Expand All @@ -71,18 +71,18 @@ func TestQueryOneToOneWithGroupRelatedID(t *testing.T) {
Results: map[string]any{
"Book": []map[string]any{
{
"author_id": "bae-fc7bf08d-9117-5acd-8b49-bc7431b1b238",
"author_id": "bae-547eb3d8-7fc8-5c21-bcef-590813451e55",
"_group": []map[string]any{
{
"name": "Painted House",
"name": "Go Guide for Rust developers",
},
},
},
{
"author_id": "bae-f2dcf043-d24d-5885-9a0a-60196094c782",
"author_id": "bae-ee5973cf-73c3-558f-8aec-8b590b8e77cf",
"_group": []map[string]any{
{
"name": "Go Guide for Rust developers",
"name": "Painted House",
},
},
},
Expand Down
96 changes: 48 additions & 48 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 Expand Up @@ -248,43 +248,43 @@ func TestQueryOneToOneMultiple_FromSecondary(t *testing.T) {
`,
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "Old Publisher"
}`,
CollectionID: 2,
DocMap: map[string]any{
"name": "Painted House",
},
},
testUtils.CreateDoc{
CollectionID: 0,
Doc: `{
"name": "New Publisher"
}`,
CollectionID: 2,
DocMap: map[string]any{
"name": "Theif Lord",
},
},
testUtils.CreateDoc{
CollectionID: 1,
Doc: `{
"name": "John Grisham"
}`,
CollectionID: 0,
DocMap: map[string]any{
"name": "Old Publisher",
"printed_id": testUtils.NewDocIndex(2, 0),
},
},
testUtils.CreateDoc{
CollectionID: 1,
Doc: `{
"name": "Cornelia Funke"
}`,
CollectionID: 0,
DocMap: map[string]any{
"name": "New Publisher",
"printed_id": testUtils.NewDocIndex(2, 1),
},
},
testUtils.CreateDoc{
CollectionID: 2,
CollectionID: 1,
DocMap: map[string]any{
"name": "Painted House",
"publisher_id": testUtils.NewDocIndex(0, 0),
"author_id": testUtils.NewDocIndex(1, 0),
"name": "John Grisham",
"published_id": testUtils.NewDocIndex(2, 0),
},
},
testUtils.CreateDoc{
CollectionID: 2,
CollectionID: 1,
DocMap: map[string]any{
"name": "Theif Lord",
"publisher_id": testUtils.NewDocIndex(0, 1),
"author_id": testUtils.NewDocIndex(1, 1),
"name": "Cornelia Funke",
"published_id": testUtils.NewDocIndex(2, 1),
},
},
testUtils.Request{
Expand All @@ -302,21 +302,21 @@ func TestQueryOneToOneMultiple_FromSecondary(t *testing.T) {
Results: map[string]any{
"Book": []map[string]any{
{
"name": "Theif Lord",
"name": "Painted House",
"publisher": map[string]any{
"name": "New Publisher",
"name": "Old Publisher",
},
"author": map[string]any{
"name": "Cornelia Funke",
"name": "John Grisham",
},
},
{
"name": "Painted House",
"name": "Theif Lord",
"publisher": map[string]any{
"name": "Old Publisher",
"name": "New Publisher",
},
"author": map[string]any{
"name": "John Grisham",
"name": "Cornelia Funke",
},
},
},
Expand Down
Loading

0 comments on commit 85be86c

Please sign in to comment.