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
- Introduction to Liquibase – Dive into Liquibase concepts.
- Install Liquibase – Download Liquibase on your machine.
- Get Started with Liquibase – Learn how to use Liquibase with an example database.
- Design Your Liquibase Project – Create a new Liquibase project folder and organize your changelogs
- How to Apply Your Liquibase Pro License Key – If you use Liquibase Pro, activate your license.
To access Neo4j, do one of the following:
- Download Neo4j Desktop locally
- Configure Neo4j locally with Docker
- Create a Neo4j AuraDB cloud instance
- Launch a Neo4j Aura sandbox instance
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
- Ensure your Neo4j database is configured. See Neo4j Operations Manual and Neo4j AuraDB: Creating an instance for more information.
- 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>/?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>
- 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).
XML example
- Navigate to your project folder in the CLI and run the Liquibase status command to see whether the connection is successful:
- Inspect the SQL with the update-sql command. Then make changes to your database with the update command.
-
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.
<?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
.
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"});
}
}
]
}
}
]
}
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.
liquibase update-sql --changelog-file=<changelog.xml>
liquibase update --changelog-file=<changelog.xml>
Related links
- Neo4j documentation: Neo4j Plugin for Liquibase
- Neo4j documentation: Welcome to Neo4j
- Neo4j documentation: Graph database concepts
Created: April 26, 2023