...

Text file src/github.com/golang-migrate/migrate/v4/database/neo4j/TUTORIAL.md

Documentation: github.com/golang-migrate/migrate/v4/database/neo4j

     1## Create migrations
     2Let's create nodes called `Users`:
     3```
     4migrate create -ext cypher -dir db/migrations -seq create_user_nodes
     5```
     6If there were no errors, we should have two files available under `db/migrations` folder:
     7- 000001_create_user_nodes.down.cypher
     8- 000001_create_user_nodes.up.cypher
     9
    10Note the `cypher` extension that we provided.
    11
    12In the `.up.cypher` file let's create the table:
    13```
    14CREATE (u1:User {name: "Peter"})
    15CREATE (u2:User {name: "Paul"})
    16CREATE (u3:User {name: "Mary"})
    17```
    18And in the `.down.sql` let's delete it:
    19```
    20MATCH (u:User) WHERE u.name IN ["Peter", "Paul", "Mary"] DELETE u
    21```
    22Ideally your migrations should be idempotent. You can read more about idempotency in [getting started](GETTING_STARTED.md#create-migrations)
    23
    24## Run migrations
    25```
    26migrate -database ${NEO4J_URL} -path db/migrations up
    27```
    28Let's check if the table was created properly by running `bin/cypher-shell -u neo4j -p password`, then `neo4j> MATCH (u:User)`
    29The output you are supposed to see:
    30```
    31+-----------------------------------------------------------------+
    32| u                                                               |
    33+-----------------------------------------------------------------+
    34| (:User {name: "Peter")                                          |
    35| (:User {name: "Paul")                                           |
    36| (:User {name: "Mary")                                           |
    37+-----------------------------------------------------------------+
    38```
    39Great! Now let's check if running reverse migration also works:
    40```
    41migrate -database ${NEO4J_URL} -path db/migrations down
    42```
    43Make sure to check if your database changed as expected in this case as well.
    44
    45## Database transactions
    46
    47To show database transactions usage, let's create another set of migrations by running:
    48```
    49migrate create -ext cypher -dir db/migrations -seq add_mood_to_users
    50```
    51Again, it should create for us two migrations files:
    52- 000002_add_mood_to_users.down.cypher
    53- 000002_add_mood_to_users.up.cypher
    54
    55In Neo4j, when we want our queries to be done in a transaction, we need to wrap it with `:BEGIN` and `:COMMIT` commands.
    56Migration up:
    57```
    58:BEGIN
    59
    60MATCH (u:User)
    61SET u.mood = "Cheery"
    62
    63:COMMIT
    64```
    65Migration down:
    66```
    67:BEGIN
    68
    69MATCH (u:User)
    70SET u.mood = null
    71
    72:COMMIT
    73```
    74
    75## Optional: Run migrations within your Go app
    76Here is a very simple app running migrations for the above configuration:
    77```
    78import (
    79	"log"
    80
    81	"github.com/golang-migrate/migrate/v4"
    82	_ "github.com/golang-migrate/migrate/v4/database/neo4j"
    83	_ "github.com/golang-migrate/migrate/v4/source/file"
    84)
    85
    86func main() {
    87	m, err := migrate.New(
    88		"file://db/migrations",
    89		"neo4j://neo4j:password@localhost:7687/")
    90	if err != nil {
    91		log.Fatal(err)
    92	}
    93	if err := m.Up(); err != nil {
    94		log.Fatal(err)
    95	}
    96}
    97```

View as plain text