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 versions

  • 3.5+

Prerequisites

  1. Introduction to Liquibase – Dive into Liquibase concepts.
  2. Install Liquibase – Download Liquibase on your machine.
  3. Get Started with Liquibase – Learn how to use Liquibase with an example database.
  4. init project – Create a new Liquibase project folder to store all Liquibase files.
  5. How to Apply Your Liquibase Pro License Key – If you use Liquibase Pro, activate your license.

To access Neo4j, do one of the following:

Install drivers

To use Liquibase and Neo4j, you need the latest JAR from the Liquibase extension for Neo4j (GitHub link).

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

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.20.0</version>
</dependency>

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.

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:
  3. url: jdbc:neo4j:bolt://<host>:<port>/?username=foo,password=bar

    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>

  1. 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).
  2. XML example
    <?xml version="1.0" encoding="UTF-8"?>
    <databaseChangeLog
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xmlns:pro="http://www.liquibase.org/xml/ns/pro"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
            http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
            http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
            http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
        <changeSet id="1" author="Liquibase">
            <neo4j:cypher>CREATE (:TestNode {test_property: "Value"});</neo4j:cypher>
        </changeSet>
    </databaseChangeLog>
    Cypher (SQL) example
    -- liquibase formatted cypher
    
    -- changeset liquibase:1
    CREATE (:TestNode {test_property: "Value"});

    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.

    YAML example
    databaseChangeLog:
       - changeSet:
           id: 1
           author: Liquibase
           changes:
           - neo4j:cypher:
               sql: CREATE (:TestNode {test_property: "Value"});
    JSON example
    {
      "databaseChangeLog": [
        {
          "changeSet": {
            "id": "1",
            "author": "Liquibase",
            "changes": [
              {
                "neo4j:cypher": {
                  "sql": CREATE (:TestNode {test_property: "Value"});
                }
              }
            ]
          }
        }
      ]
    }
  3. Navigate to your project folder in the CLI and run the Liquibase status command to see whether the connection is successful:
  4. 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.
  6. liquibase update-sql --changelog-file=<changelog.xml>
    liquibase update --changelog-file=<changelog.xml>
  7. 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.

Related links


Last update: May 23, 2023
Created: April 26, 2023