Skip to content

Commit

Permalink
fix index query and added test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Solender committed Nov 3, 2021
1 parent ac5fcca commit a21ccd4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
4 changes: 2 additions & 2 deletions index_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func createAllIndexesAndConstraintsV3(ctx context.Context, gogm *Gogm, mappedTyp
Name: node,
Type: structConfig.Label,
Field: config.Name,
})).Cypher("IF NOT EXISTS").ToCypher()
})).ToCypher()
if err != nil {
return err
}
Expand All @@ -180,7 +180,7 @@ func createAllIndexesAndConstraintsV3(ctx context.Context, gogm *Gogm, mappedTyp
cyp, err := dsl.QB().Create(dsl.NewIndex(&dsl.IndexConfig{
Type: structConfig.Label,
Fields: indexFields,
})).Cypher("IF NOT EXISTS").ToCypher()
})).ToCypher()
if err != nil {
return err
}
Expand Down
55 changes: 33 additions & 22 deletions index_v4.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,40 @@ import (
"fmt"
"github.com/adam-hanna/arrayOperations"
"github.com/cornelk/hashmap"
dsl "github.com/mindstand/go-cypherdsl"
"github.com/neo4j/neo4j-go-driver/v4/neo4j"
"strings"
)

const (
constraintOnQuery = "CREATE CONSTRAINT IF NOT EXISTS ON (%s:%s) ASSERT "
uniquePart = "%s.%s IS UNIQUE"
notUniquePart = "exists(%s.%s)"

indexQuery = "CREATE INDEX IF NOT EXISTS FOR (n:%s) ON ("
)

func buildConstraintQuery(unique bool, name, nodeType, field string) string {
cyp := fmt.Sprintf(constraintOnQuery, name, nodeType)

if unique {
cyp += fmt.Sprintf(uniquePart, name, field)
} else {
cyp += fmt.Sprintf(notUniquePart, name, field)
}

return cyp
}

func buildIndexQuery(indexType string, fields ...string) string {
query := fmt.Sprintf(indexQuery, indexType)

for _, field := range fields {
query += fmt.Sprintf("n.%s,", field)
}

return strings.TrimSuffix(query, ",") + ")"
}

func resultToStringArrV4(isConstraint bool, result [][]interface{}) ([]string, error) {
if result == nil {
return nil, errors.New("result is nil")
Expand Down Expand Up @@ -173,18 +203,7 @@ func createAllIndexesAndConstraintsV4(ctx context.Context, gogm *Gogm, mappedTyp
//pk is a special unique key
if config.PrimaryKey != "" || config.Unique {
numIndexCreated++

cyp, err := dsl.QB().Create(dsl.NewConstraint(&dsl.ConstraintConfig{
Unique: true,
Name: node,
Type: structConfig.Label,
Field: config.Name,
})).Cypher("IF NOT EXISTS").ToCypher()
if err != nil {
return err
}

_, _, err = tx.QueryRaw(ctx, cyp, nil)
_, _, err = tx.QueryRaw(ctx, buildConstraintQuery(true, node, structConfig.Label, config.Name), nil)
if err != nil {
return err
}
Expand All @@ -196,15 +215,7 @@ func createAllIndexesAndConstraintsV4(ctx context.Context, gogm *Gogm, mappedTyp
//create composite index
if len(indexFields) > 0 {
numIndexCreated++
cyp, err := dsl.QB().Create(dsl.NewIndex(&dsl.IndexConfig{
Type: structConfig.Label,
Fields: indexFields,
})).Cypher("IF NOT EXISTS").ToCypher()
if err != nil {
return err
}

_, _, err = tx.QueryRaw(ctx, cyp, nil)
_, _, err = tx.QueryRaw(ctx, buildIndexQuery(structConfig.Label, indexFields...), nil)
if err != nil {
return err
}
Expand Down
27 changes: 26 additions & 1 deletion integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ import (
"github.com/stretchr/testify/suite"
)

type indexTestStruct struct {
BaseUUIDNode
StringIndex string `gogm:"name=string_index;index"`
StringUnique string `gogm:"name=string_unique;unique"`
}

func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip()
Expand All @@ -46,6 +52,7 @@ func TestIntegration(t *testing.T) {
type IntegrationTestSuite struct {
suite.Suite
gogm *Gogm
conf *Config
}

func (integrationTest *IntegrationTestSuite) TearDownSuite() {
Expand Down Expand Up @@ -77,6 +84,24 @@ func (integrationTest *IntegrationTestSuite) SetupSuite() {
integrationTest.gogm = gogm
}

func (integrationTest *IntegrationTestSuite) TestV4Index() {
if integrationTest.gogm.neoVersion < 4 {
integrationTest.T().Log("skipping because of incompatible version", integrationTest.gogm.neoVersion)
integrationTest.T().Skip()
return
}

assertCopy := *integrationTest.conf
assertCopy.IndexStrategy = ASSERT_INDEX
_, err := New(&assertCopy, UUIDPrimaryKeyStrategy, &indexTestStruct{})
integrationTest.Assert().Nil(err)

validateCopy := *integrationTest.conf
validateCopy.IndexStrategy = VALIDATE_INDEX
_, err = New(&validateCopy, UUIDPrimaryKeyStrategy, &indexTestStruct{})
integrationTest.Assert().Nil(err)
}

func (integrationTest *IntegrationTestSuite) TestSecureConnection() {
if integrationTest.gogm.neoVersion < 4 {
integrationTest.T().Log("skipping secure test for v3")
Expand All @@ -96,7 +121,7 @@ func (integrationTest *IntegrationTestSuite) TestSecureConnection() {
EnableDriverLogs: true,
DefaultTransactionTimeout: 2 * time.Minute,
}

integrationTest.conf = &conf
gogm, err := New(&conf, UUIDPrimaryKeyStrategy, &a{}, &b{}, &c{}, &propTest{})
integrationTest.Require().Nil(err)
integrationTest.Require().NotNil(gogm)
Expand Down

0 comments on commit a21ccd4

Please sign in to comment.