-
Notifications
You must be signed in to change notification settings - Fork 0
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
Neo4J Support in Go Migrate [WIP] #1
Changes from 3 commits
729c440
5a1b351
e67feb1
d5340b9
79946b1
a4e7370
f1f063f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package neo4j | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
neturl "net/url" | ||
|
||
"github.com/golang-migrate/migrate/v4/database" | ||
"github.com/neo4j/neo4j-go-driver/neo4j" | ||
) | ||
|
||
func init() { | ||
db := Neo4J{} | ||
database.Register("bolt", &db) | ||
database.Register("neo4j", &db) | ||
} | ||
|
||
var DefaultMigrationsLabel = "SchemaMigration" | ||
|
||
var ( | ||
ErrNilConfig = fmt.Errorf("no config") | ||
) | ||
|
||
type Config struct { | ||
AuthToken neo4j.AuthToken | ||
URL string | ||
MigrationsLabel string | ||
} | ||
|
||
type Neo4J struct { | ||
driver neo4j.Driver | ||
|
||
// Open and WithInstance need to guarantee that config is never nil | ||
config *Config | ||
} | ||
|
||
func WithInstance(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, | ||
config: config, | ||
} | ||
|
||
if err := driver.ensureVersionConstraint(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return driver, nil | ||
} | ||
|
||
func (n *Neo4J) Open(url string) (database.Driver, error) { | ||
uri, err := neturl.Parse(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
password, _ := uri.User.Password() | ||
authToken := neo4j.BasicAuth(uri.User.Username(), password, "") | ||
|
||
return WithInstance(&Config{ | ||
URL: url, | ||
AuthToken: authToken, | ||
MigrationsLabel: DefaultMigrationsLabel, | ||
}) | ||
} | ||
|
||
func (n *Neo4J) Close() error { | ||
return n.driver.Close() | ||
} | ||
|
||
func (n *Neo4J) Lock() error { | ||
return nil | ||
} | ||
|
||
func (n *Neo4J) Unlock() error { | ||
return nil | ||
} | ||
|
||
func (n *Neo4J) Run(migration io.Reader) error { | ||
body, err := ioutil.ReadAll(migration) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
session, err := n.driver.Session(neo4j.AccessModeWrite) | ||
if err != nil { | ||
return err | ||
} | ||
defer session.Close() | ||
|
||
_, err = session.Run(string(body[:]), nil) | ||
return err | ||
} | ||
|
||
func (n *Neo4J) SetVersion(version int, dirty bool) error { | ||
session, err := n.driver.Session(neo4j.AccessModeRead) | ||
if err != nil { | ||
return err | ||
} | ||
defer session.Close() | ||
|
||
_, err = session.Run("MERGE (sm:$migration {version: $version, dirty: $dirty})", | ||
map[string]interface{}{"migration": n.config.MigrationsLabel, "version": version, "dirty": dirty}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm with this, wouldn't it be possible to get two nodes with same version but differing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It shouldn't be because we run a |
||
return err | ||
} | ||
|
||
func (n *Neo4J) Version() (version int, dirty bool, err error) { | ||
session, err := n.driver.Session(neo4j.AccessModeRead) | ||
if err != nil { | ||
return -1, false, err | ||
} | ||
defer session.Close() | ||
|
||
result, err := session.Run("MATCH (sm:$migration) RETURN sm.version, sm.dirty ORDER BY sm.version DESC LIMIT 1", | ||
map[string]interface{}{"migration": n.config.MigrationsLabel}) | ||
if err != nil { | ||
return -1, false, err | ||
} | ||
if result.Next() { | ||
versionResult, ok := result.Record().Get("version") | ||
if !ok { | ||
version = -1 | ||
} else { | ||
version = versionResult.(int) | ||
} | ||
} else { | ||
version = -1 | ||
} | ||
|
||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doesn't return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since the variable are named in the function signature, they will all be returned with an empty |
||
} | ||
|
||
func (n *Neo4J) Drop() error { | ||
session, err := n.driver.Session(neo4j.AccessModeWrite); if err != nil { | ||
return err | ||
} | ||
defer session.Close() | ||
|
||
_, err = session.Run("MATCH (n) DETACH DELETE n", map[string]interface{}{}) | ||
return err | ||
} | ||
|
||
func (n *Neo4J) ensureVersionConstraint() (err error) { | ||
session, err := n.driver.Session(neo4j.AccessModeWrite); if err != nil { | ||
return err | ||
} | ||
defer session.Close() | ||
|
||
_, err = session.Run("CREATE CONSTRAINT ON (a:$migration) ASSERT a.version IS UNIQUE", | ||
map[string]interface{}{"migration": n.config.MigrationsLabel}) | ||
return err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package neo4j | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"github.com/dhui/dktest" | ||
"github.com/neo4j/neo4j-go-driver/neo4j" | ||
|
||
dt "github.com/golang-migrate/migrate/v4/database/testing" | ||
"github.com/golang-migrate/migrate/v4/dktesting" | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
) | ||
|
||
var ( | ||
opts = dktest.Options{PortRequired: true, ReadyFunc: isReady} | ||
specs = []dktesting.ContainerSpec{ | ||
{ImageName: "neo4j:3.5", Options: opts}, | ||
//{ImageName: "neo4j:3.5-enterprise", Options: opts}, | ||
} | ||
) | ||
|
||
func neoConnectionString(host, port string) string { | ||
return fmt.Sprintf("bolt://neo4j:neo4j@%s:%s", host, port) | ||
} | ||
|
||
func isReady(ctx context.Context, c dktest.ContainerInfo) bool { | ||
ip, port, err := c.Port(7687) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hardcoded port? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for a test, the function asks the docker test suite which docker port has been mapped to 7687, which is the bolt exposed port in the neo docker container. In real usage, the port would be provided by the URL the user gives |
||
if err != nil { | ||
return false | ||
} | ||
|
||
driver, err := neo4j.NewDriver(neoConnectionString(ip, port), neo4j.BasicAuth("neo4j", "neo4j", "")) | ||
if err != nil { | ||
return false | ||
} | ||
defer func() { | ||
if err := driver.Close(); err != nil { | ||
log.Println("close error:", err) | ||
} | ||
}() | ||
session, err := driver.Session(neo4j.AccessModeRead) | ||
if err != nil { | ||
return false | ||
} | ||
_, err = session.Run("RETURN 1", nil) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func Test(t *testing.T) { | ||
dktesting.ParallelTest(t, specs, func(t *testing.T, c dktest.ContainerInfo) { | ||
ip, port, err := c.Port(7687) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
n := &Neo4J{} | ||
d, err := n.Open(neoConnectionString(ip, port)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
if err := d.Close(); err != nil { | ||
t.Error(err) | ||
} | ||
}() | ||
dt.Test(t, d, []byte("MATCH a RETURN a")) | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// +build neo4j | ||
|
||
package cli | ||
|
||
import ( | ||
_ "github.com/golang-migrate/migrate/v4/database/neo4j" | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are these? Just to fulfill interface methods?
doesn't seem like we need a
sync.Mutex
forNeo4J
struct.Also now that I see it, did you mean to have capital J for Neo4J?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there is a generic
Driver
interface that go migrate has you fill out, and requests you returnnil
if the database isn't capable of locking (which Neo isn't).You are correct about the J, I have replaced it with lowercase