Skip to content

Using Liquibase with Neo4j

Neo4j is a property graph database management system with native graph storage and processing. It uses a graph query language called Cypher. For more information, see Neo4j Documentation.

Supported database versions

  • 3.5+

Prerequisites

To access Neo4j, do one of the following:

Install drivers

All Users

  1. Download the following JAR files:

    Warning

    Liquibase 4.23.0 is not compatible with the Neo4j Extension

    Upgrade both core and the extension to 4.23.1 (or later).

    Or, use Liquibase core 4.21.1 and the Neo4j extension at version 4.21.1.2.

  2. Place the JAR file(s) in the liquibase/lib directory.

Note

The Neo4j extension has native JDBC connectivity support in version 4.19.0+.

If you're using an earlier version, you must also install a third-party JDBC driver to connect to Liquibase.

For driver configuration information, see Neo4j Configuration. For additional JARs to integrate Neo4j with your preferred programming language, see Connecting to Neo4j.

Maven Users (additional step)

If you use Maven, you must include the driver JAR as a dependency in your pom.xml file.

<dependency>
    <groupId>org.liquibase.ext</groupId>
    <artifactId>liquibase-neo4j</artifactId>
    <version>[4.26.0.1,)</version>
</dependency>

Test your connection

  1. Ensure your Neo4j database is configured. See Neo4j Operations Manual and Neo4j AuraDB: Creating an instance for more information.
  2. Specify the database URL in the liquibase.properties file (defaults file), along with other properties you want to set a default value for. Liquibase does not parse the URL. You can either specify the full database connection string or specify the URL using your database's standard JDBC format:

    url: jdbc:neo4j:bolt://<host>:<port>
    

    Note

    The Liquibase extension for Neo4j only supports connections through the Bolt protocol, not HTTP.

    For more information about the JDBC connection, see Neo4j JDBC Driver Documentation § Technical Reference.

    Tip

    To apply a Liquibase Pro key to your project, add the following property to the Liquibase properties file: licenseKey: <paste code here>

  3. Create a text file called changelog (.xml, .cypher, .json, or .yaml) in your project directory and add a changeset. The <neo4j:cypher> change type has the same behavior as the <sql> change type. For more information about Cypher syntax, see the Neo4j Cypher Manual (general syntax) the Neo4j Extension Cypher Manual (Liquibase syntax).

    --liquibase formatted cypher
    
    --changeset fbiville:my-movie-init
    CREATE (:Movie {title: 'My Life'})
    

    Tip

    Formatted SQL/Cypher changelogs generated from Liquibase versions before 4.2 might cause issues because of the lack of space after a double dash ( -- ). To fix this, add a space after the double dash.

    For example:

    -- liquibase formatted cypher instead of --liquibase formatted cypher and

    -- changeset myname:create instead of --changeset myname:create.

    <?xml version="1.0" encoding="UTF-8"?>
    <databaseChangeLog xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                       xmlns:neo4j="http://www.liquibase.org/xml/ns/dbchangelog-ext"
                       xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">
    
        <changeSet id="my-movie-init" author="fbiville">
            <neo4j:cypher>CREATE (:Movie {title: 'My Life'})</neo4j:cypher>
        </changeSet>
    </databaseChangeLog>
    
    databaseChangeLog:
    - changeSet:
      id: my-movie-init
      author: fbiville
      changes:
      - cypher: 'CREATE (:Movie {title: ''My Life'', genre: ''Comedy''})'
    
    {"databaseChangeLog": [
        {"changeSet": {
            "id": "my-movie-init",
            "author": "fbiville",
            "changes": [
                {"cypher": "CREATE (:Movie {title: 'My Life', genre: 'Comedy'})"}
            ]
        }}
    ]}
    
  4. Navigate to your project folder in the CLI and run the Liquibase status command to see whether the connection is successful:

    liquibase status --username=test --password=test --changelog-file=<changelog.xml>
    

    Note

    You can pass arguments in the CLI or keep them in the Liquibase properties file.

  5. Inspect the SQL with the update-sql command. Then make changes to your database with the update command.

    liquibase update-sql --changelog-file=<changelog.xml>
    liquibase update --changelog-file=<changelog.xml>
    
  6. From the Neo4j browser, ensure that your database contains the changelog node by running MATCH (c:__LiquibaseChangeLog) RETURN c.

    Note

    The __LiquibaseChangeLogLock node is only present during an active Liquibase execution, not after, so it isn't normally visible.