-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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 #320
Merged
Merged
Neo4J Support #320
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
729c440
initial pass
mvid 5a1b351
new cli build file
mvid e67feb1
add ready function for docker start
mvid d5340b9
lowercase j
mvid 79946b1
explicit return
mvid a4e7370
test migration
mvid f1f063f
undo dockerfile change
mvid 85e0676
Merge pull request #1 from mvid/neo4j
mvid b4a4116
Merge branch 'master' into master
mvid 2866d25
neo4j in dockerfile
mvid ae48550
Merge branch 'master' of github.com:mvid/migrate
mvid d1cc5cd
remove auth from url once transferred to auth token
mvid 441db85
assorted review notes
mvid 46a6265
initial docs attempt
mvid 2050d0f
unlock behavior more in line with lock behavior
mvid ea75d4b
consistent naming
mvid e994a5d
linting
mvid 4d636f7
seabolt in travis ci
mvid a0f6bfe
stdlib logger
mvid 7124092
install seabolt as sudo
mvid d969cad
enabled cgo
mvid c468895
correct version #
mvid af4be16
remove neo4j todo
mvid ce35a58
named return values for errors
mvid 7931609
review notes
mvid 9936ded
add libssl for alpine
mvid ff92b04
updates for review
mvid File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# neo4j | ||
|
||
`bolt://user:password@host:port/` | ||
dhui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
| URL Query | WithInstance Config | Description | | ||
|------------|---------------------|-------------| | ||
| `user` | Contained within `AuthConfig` | The user to sign in as | | ||
| `password` | Contained within `AuthConfig` | The user's password | | ||
| `host` | | The host to connect to. Values that start with / are for unix domain sockets. (default is localhost) | | ||
| `port` | | The port to bind to. (default is 7687) | | ||
| | `MigrationsLabel` | Name of the migrations node label | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
## Create migrations | ||
Let's create nodes called `Users`: | ||
``` | ||
migrate create -ext cypher -dir db/migrations -seq create_user_nodes | ||
``` | ||
If there were no errors, we should have two files available under `db/migrations` folder: | ||
- 000001_create_user_nodes.down.cypher | ||
- 000001_create_user_nodes.up.cypher | ||
|
||
Note the `cypher` extension that we provided. | ||
|
||
In the `.up.cypher` file let's create the table: | ||
``` | ||
CREATE (u1:User {name: "Peter"}) | ||
CREATE (u2:User {name: "Paul"}) | ||
CREATE (u3:User {name: "Mary"}) | ||
``` | ||
And in the `.down.sql` let's delete it: | ||
``` | ||
MATCH (u:User) WHERE u.name IN ["Peter", "Paul", "Mary"] DELETE u | ||
``` | ||
Ideally your migrations should be idempotent. You can read more about idempotency in [getting started](GETTING_STARTED.md#create-migrations) | ||
|
||
## Run migrations | ||
``` | ||
migrate -database ${NEO4J_URL} -path db/migrations up | ||
``` | ||
Let's check if the table was created properly by running `bin/cypher-shell -u neo4j -p password`, then `neo4j> MATCH (u:User)` | ||
The output you are supposed to see: | ||
``` | ||
+-----------------------------------------------------------------+ | ||
| u | | ||
+-----------------------------------------------------------------+ | ||
| (:User {name: "Peter") | | ||
| (:User {name: "Paul") | | ||
| (:User {name: "Mary") | | ||
+-----------------------------------------------------------------+ | ||
``` | ||
Great! Now let's check if running reverse migration also works: | ||
``` | ||
migrate -database ${NEO4J_URL} -path db/migrations down | ||
``` | ||
Make sure to check if your database changed as expected in this case as well. | ||
|
||
## Database transactions | ||
|
||
To show database transactions usage, let's create another set of migrations by running: | ||
``` | ||
migrate create -ext cypher -dir db/migrations -seq add_mood_to_users | ||
``` | ||
Again, it should create for us two migrations files: | ||
- 000002_add_mood_to_users.down.cypher | ||
- 000002_add_mood_to_users.up.cypher | ||
|
||
In Neo4j, when we want our queries to be done in a transaction, we need to wrap it with `:BEGIN` and `:COMMIT` commands. | ||
Migration up: | ||
``` | ||
:BEGIN | ||
|
||
MATCH (u:User) | ||
SET u.mood = "Cheery" | ||
|
||
:COMMIT | ||
``` | ||
Migration down: | ||
``` | ||
:BEGIN | ||
|
||
MATCH (u:User) | ||
SET u.mood = null | ||
|
||
:COMMIT | ||
``` | ||
|
||
## Optional: Run migrations within your Go app | ||
Here is a very simple app running migrations for the above configuration: | ||
``` | ||
import ( | ||
"log" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
_ "github.com/golang-migrate/migrate/v4/database/neo4j" | ||
_ "github.com/golang-migrate/migrate/v4/source/file" | ||
) | ||
|
||
func main() { | ||
m, err := migrate.New( | ||
"file://db/migrations", | ||
"bolt://neo4j:password@localhost:7687/") | ||
dhui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
if err := m.Up(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add docs about installing
seabolt
and statically linkingOpenSSL
to theREADME