跳至内容

快速入门#

如果您不熟悉 Liquibase 的概念,请从这里开始。

设置#

本教程的其余部分将假设使用CLI安装,以及一个正在运行的 Neo4j 数据库。请参阅下载部分以安装扩展。

有几种方法可以安装 Neo4j

从现在开始,本教程假设 Neo4j 运行在URI bolt://localhost上,用户为neo4j,密码为非常(不)安全的changeme

变更日志#

本教程的其余部分假设变更日志以 XML 文件changeLog.xml编写,以下是一些支持的格式

<?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>
--liquibase formatted cypher
--changeset fbiville:my-movie-init
CREATE (:Movie {title: 'My Life'})
{"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''})'

这将创建一个带有标签Movie的节点,以及一个属性,其键为title,值为字符串My Life

提示

如果您不熟悉这些图形概念,请从这里了解更多信息。

预演#

这是updateSQL命令(一个不太以RDBMS为中心的名称正在考虑中)。执行预演是在更改持久化到数据库之前尽早检测错误的好方法。这将显示如果它是正常执行,则将针对 Neo4j 执行的所有查询。

liquibase --url jdbc:neo4j:bolt://localhost \
          --username neo4j \
          --password changeme \
          --changeLogFile changeLog.xml \
          updateSQL

(截断的)输出如下所示

Liquibase Community 4.30.0 by Datical
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ##
##  Get documentation at docs.liquibase.com       ##
##  Get certified courses at learn.liquibase.com  ##
##  Free schema change activity reports at        ##
##      https://hub.liquibase.com                 ##
##                                                ##
####################################################
Starting Liquibase (version 4.30.0)
-- *********************************************************************
-- Update Database Script
-- *********************************************************************
-- Change Log: changeLog.xml
-- Against: neo4j@jdbc:neo4j:bolt://localhost
-- Liquibase version: 4.30.0
-- *********************************************************************

[...]

-- Changeset changeLog.xml::my-movie-init::fbiville
CREATE (:Movie {title: 'My Life'});

[...]

Liquibase command 'updateSQL' was executed successfully.

运行#

这是update命令。

liquibase --url jdbc:neo4j:bolt://localhost \
          --username neo4j \
          --password changeme \
          --changeLogFile changeLog.xml \
          update

输出应该类似于

Liquibase Community 4.30.0 by Datical
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ##
##  Get documentation at docs.liquibase.com       ##
##  Get certified courses at learn.liquibase.com  ##
##  Free schema change activity reports at        ##
##      https://hub.liquibase.com                 ##
##                                                ##
####################################################
Starting Liquibase (version 4.30.0)
Liquibase: Update has been successful.

查看数据库并检查是否已创建Movie节点。

根据您的 Neo4j 设置,打开 Neo4j 浏览器将包括以下操作之一

  • 转到Neo4j Aura,找到您的实例并单击“使用”按钮,然后选择 Neo4j 浏览器
  • 转到Neo4j Sandbox,找到您的实例并单击“打开”按钮
  • 直接浏览http://localhost:7474(Docker)
  • 打开 Neo4j Desktop,选择您的项目并单击“打开”按钮

打开 Neo4j 浏览器后,您可以运行MATCH (movie:Movie) RETURN movie查询,您应该会看到与以下相同的输出

Results after first run

如果再次运行相同的update命令会发生什么?

让我们来找出答案

liquibase --url jdbc:neo4j:bolt://localhost \
          --username neo4j \
          --password changeme \
          --changeLogFile changeLog.xml \
          update

输出应该类似于

Liquibase Community 4.30.0 by Datical
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ##
##  Get documentation at docs.liquibase.com       ##
##  Get certified courses at learn.liquibase.com  ##
##  Free schema change activity reports at        ##
##      https://hub.liquibase.com                 ##
##                                                ##
####################################################
Starting Liquibase (version 4.30.0)
Liquibase: Update has been successful.

再次查看数据库。

Results after second run

没有任何变化!

它实际上按设计工作。实际上,更改集默认情况下是增量的,即它们只运行一次。

数据库实际上包含超过 1 个节点

All nodes

该插件将更改日志历史记录存储到同一个数据库中。这就是 Liquibase 记住不重新运行增量更改集的方式。

热心的读者可能还注意到__LiquibaseChangeSet节点上的checkSum属性。该属性确保其更改集自上次运行以来未发生更改。

阅读参考文档了解更多信息。