0%

liquibase

Linquibase

一个数据库版本管理工具. 但本渣渣用起来感觉很不顺. 网上资料也少且坑, 官方文档又藏得深.

本文所使用的数据库是 postgresql. 且本文的介绍非常粗浅, 且不保证对.请以官方文档为准.

用命令行直接使用

开始的开始, 安装 liquibase, 省略不写.

  1. 新建文件夹 liquibase

  2. 新建文件 liquibase.properties

    1
    2
    3
    4
    5
    6
    7
    changeLogFile: 要导出的 log 的文件的位置
    url: jdbc:postgresql://localhost:5432/test-db
    defaultSchemaName: sts
    username: postgres
    password: password
    driver: org.postgresql.Driver
    classpath: 驱动 jar 包
  3. 新建文件 liquibase.sql

  4. 在 cmd 中运行指令

    1
    liquibase --changeLogFile=liquibase.sql generateChangeLog

作为 maven 插件使用

参考链接

开始的开始, 创建 maven 工程, 此处省略不写.

  1. 在 pom 中添加 plugin

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    <properties>
    <liquibase.env>dev</liquibase.env>
    </properties>
    <plugin>
    <groupId>org.liquibase</groupId>
    <artifactId>liquibase-maven-plugin</artifactId>
    <version>4.2.2</version>
    <configuration>
    <outputChangeLogFile>src/main/resources/liquibase.sql</outputChangeLogFile>
    <changeLogFile>src/main/resources/liquibase.sql</changeLogFile>
    <propertyFileWillOverride>true</propertyFileWillOverride>
    <propertyFile>src/main/resources/liquibase.${liquibase.env}.properties</propertyFile>
    </configuration>
    <dependencies>
    <dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.18</version>
    </dependency>
    </dependencies>
    </plugin>

    这里要非常注意: 如果你要导出 changeLogFile 一定要写配置:

    1
    <outputChangeLogFile>src/main/resources/liquibase.sql</outputChangeLogFile>

    不然会报错 The output changeLogFile must be specified

    坑了我一下午在网上找, 最后才在上面的参考链接中看到了这个配置.

  2. 在 src/main/resources 中新建文件 liquibase.dev.properties, 填写的内容和上上面的一样. 只是这里的 changeLogFile 所指定的文件是 “只读” 的.

  3. 运行 mvn liquibase:generateChangeLog