Skip to content

Commit

Permalink
Add neo4j driver on params instance (golang-migrate#373)
Browse files Browse the repository at this point in the history
* Add neo4j driver on instance params

* Check constraint if exist return nil

* Create new driver if nil

* Remove URL and AuthToken from neo4j config

* Change call db constraint

* Add neo4j image 4.0

* Handle create new driver

* Use labels instead constraints

* Turn off TLS encryption on test
  • Loading branch information
dynastymasra authored Apr 16, 2020
1 parent 833d007 commit 423c2d3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 16 deletions.
53 changes: 38 additions & 15 deletions database/neo4j/neo4j.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ var (
)

type Config struct {
AuthToken neo4j.AuthToken
URL string // if using WithInstance, don't provide auth in the URL, it will be ignored
MigrationsLabel string
MultiStatement bool
}
Expand All @@ -43,26 +41,21 @@ type Neo4j struct {
config *Config
}

func WithInstance(config *Config) (database.Driver, error) {
func WithInstance(driver neo4j.Driver, config *Config) (database.Driver, error) {
if config == nil {
return nil, ErrNilConfig
}

neoDriver, err := neo4j.NewDriver(config.URL, config.AuthToken)
if err != nil {
return nil, err
}

driver := &Neo4j{
driver: neoDriver,
nDriver := &Neo4j{
driver: driver,
config: config,
}

if err := driver.ensureVersionConstraint(); err != nil {
if err := nDriver.ensureVersionConstraint(); err != nil {
return nil, err
}

return driver, nil
return nDriver, nil
}

func (n *Neo4j) Open(url string) (database.Driver, error) {
Expand All @@ -75,18 +68,35 @@ func (n *Neo4j) Open(url string) (database.Driver, error) {
uri.User = nil
uri.Scheme = "bolt"
msQuery := uri.Query().Get("x-multi-statement")

// Whether to turn on/off TLS encryption.
tlsEncrypted := uri.Query().Get("x-tls-encrypted")
multi := false
encrypted := false
if msQuery != "" {
multi, err = strconv.ParseBool(uri.Query().Get("x-multi-statement"))
if err != nil {
return nil, err
}
}

if tlsEncrypted != "" {
encrypted, err = strconv.ParseBool(tlsEncrypted)
if err != nil {
return nil, err
}
}

uri.RawQuery = ""

return WithInstance(&Config{
URL: uri.String(),
AuthToken: authToken,
driver, err := neo4j.NewDriver(uri.String(), authToken, func(config *neo4j.Config) {
config.Encrypted = encrypted
})
if err != nil {
return nil, err
}

return WithInstance(driver, &Config{
MigrationsLabel: DefaultMigrationsLabel,
MultiStatement: multi,
})
Expand Down Expand Up @@ -256,6 +266,19 @@ func (n *Neo4j) ensureVersionConstraint() (err error) {
}
}()

/**
Get constraint and check to avoid error duplicate
using db.labels() to support Neo4j 3 and 4.
Neo4J 3 doesn't support db.constraints() YIELD name
*/
res, err := neo4j.Collect(session.Run(fmt.Sprintf("CALL db.labels() YIELD label WHERE label=\"%s\" RETURN label", n.config.MigrationsLabel), nil))
if err != nil {
return err
}
if len(res) == 1 {
return nil
}

query := fmt.Sprintf("CREATE CONSTRAINT ON (a:%s) ASSERT a.version IS UNIQUE", n.config.MigrationsLabel)
if _, err := neo4j.Collect(session.Run(query, nil)); err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion database/neo4j/neo4j_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ var (
opts = dktest.Options{PortRequired: true, ReadyFunc: isReady,
Env: map[string]string{"NEO4J_AUTH": "neo4j/migratetest", "NEO4J_ACCEPT_LICENSE_AGREEMENT": "yes"}}
specs = []dktesting.ContainerSpec{
{ImageName: "neo4j:4.0", Options: opts},
{ImageName: "neo4j:4.0-enterprise", Options: opts},
{ImageName: "neo4j:3.5", Options: opts},
{ImageName: "neo4j:3.5-enterprise", Options: opts},
{ImageName: "neo4j:3.4", Options: opts},
Expand All @@ -37,7 +39,12 @@ func isReady(ctx context.Context, c dktest.ContainerInfo) bool {
return false
}

driver, err := neo4j.NewDriver(neoConnectionString(ip, port), neo4j.BasicAuth("neo4j", "migratetest", ""))
driver, err := neo4j.NewDriver(
neoConnectionString(ip, port),
neo4j.BasicAuth("neo4j", "migratetest", ""),
func(config *neo4j.Config) {
config.Encrypted = false
})
if err != nil {
return false
}
Expand Down

0 comments on commit 423c2d3

Please sign in to comment.