跳到内容

快速入门#

如果您不熟悉 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.32.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.32.0)
-- *********************************************************************
-- Update Database Script
-- *********************************************************************
-- Change Log: changeLog.xml
-- Against: neo4j@jdbc:neo4j:bolt://localhost
-- Liquibase version: 4.32.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.32.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.32.0)
Liquibase: Update has been successful.

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

根据您的 Neo4j 设置,打开 Neo4j Browser 包括以下几种方式:

  • 访问 Neo4j Aura,找到您的实例,点击 "Open with" 按钮并选择 Neo4j Browser
  • 访问 Neo4j Sandbox,找到您的实例,点击 "Open" 按钮
  • 直接浏览 http://localhost:7474 (Docker)
  • 打开 Neo4j Desktop,选择您的项目并点击 "Open" 按钮

一旦 Neo4j browser 打开,您可以运行 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.32.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.32.0)
Liquibase: Update has been successful.

再次查看数据库。

Results after second run

没有任何变化!

这实际上是按设计工作的。因为,默认情况下,变更集是增量的,也就是说,它们只运行一次。

数据库中实际包含的不止一个节点

All nodes

该插件将变更日志历史存储在同一个数据库中。这就是 Liquibase 如何记住不再重复运行增量变更集的方法。

敏锐的读者可能还注意到 __LiquibaseChangeSet 节点上有一个 checkSum 属性。该属性确保自上次运行以来,其变更集未发生变化。

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

© . All rights reserved.