github.com/scraniel/migrate@v0.0.0-20230320185700-339088f36cee/database/neo4j/TUTORIAL.md (about)

     1  ## Create migrations
     2  Let's create nodes called `Users`:
     3  ```
     4  migrate create -ext cypher -dir db/migrations -seq create_user_nodes
     5  ```
     6  If 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  
    10  Note the `cypher` extension that we provided.
    11  
    12  In the `.up.cypher` file let's create the table:
    13  ```
    14  CREATE (u1:User {name: "Peter"})
    15  CREATE (u2:User {name: "Paul"})
    16  CREATE (u3:User {name: "Mary"})
    17  ```
    18  And in the `.down.sql` let's delete it:
    19  ```
    20  MATCH (u:User) WHERE u.name IN ["Peter", "Paul", "Mary"] DELETE u
    21  ```
    22  Ideally your migrations should be idempotent. You can read more about idempotency in [getting started](GETTING_STARTED.md#create-migrations)
    23  
    24  ## Run migrations
    25  ```
    26  migrate -database ${NEO4J_URL} -path db/migrations up
    27  ```
    28  Let's check if the table was created properly by running `bin/cypher-shell -u neo4j -p password`, then `neo4j> MATCH (u:User)`
    29  The 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  ```
    39  Great! Now let's check if running reverse migration also works:
    40  ```
    41  migrate -database ${NEO4J_URL} -path db/migrations down
    42  ```
    43  Make sure to check if your database changed as expected in this case as well.
    44  
    45  ## Database transactions
    46  
    47  To show database transactions usage, let's create another set of migrations by running:
    48  ```
    49  migrate create -ext cypher -dir db/migrations -seq add_mood_to_users
    50  ```
    51  Again, 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  
    55  In Neo4j, when we want our queries to be done in a transaction, we need to wrap it with `:BEGIN` and `:COMMIT` commands.
    56  Migration up:
    57  ```
    58  :BEGIN
    59  
    60  MATCH (u:User)
    61  SET u.mood = "Cheery"
    62  
    63  :COMMIT
    64  ```
    65  Migration down:
    66  ```
    67  :BEGIN
    68  
    69  MATCH (u:User)
    70  SET u.mood = null
    71  
    72  :COMMIT
    73  ```
    74  
    75  ## Optional: Run migrations within your Go app
    76  Here is a very simple app running migrations for the above configuration:
    77  ```
    78  import (
    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  
    86  func 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  ```