如何使用Liquibase

Maven配置信息,大部分配置可以通过命令行参数获得

           <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase.version}</version>
                <configuration>
                    <changeLogFile>resources/config/liquibase/master.xml</changeLogFile>
                    <!--<diffChangeLogFile>classpath:config/liquibase/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>-->
                    <driver>com.mysql.jdbc.Driver</driver>
                    <url>jdbc:mysql://localhost:3306/entranceguard?useUnicode=true&amp;characterEncoding=utf8</url>
                    <defaultSchemaName>entranceguard</defaultSchemaName>
                    <username>root</username>
                    <password>root</password>
                    <referenceUrl>jdbc:mysql://localhost:3306/entranceguard2?useUnicode=true&amp;characterEncoding=utf8</referenceUrl>
                    <referenceUsername>root</referenceUsername>
                    <referencePassword>root</referencePassword>
                    <!--<rollbackTag>T4</rollbackTag>-->
                    <!--<rollbackDate>Feb 16, 2016</rollbackDate>-->
                    <!--<tag>Release0100-2</tag>-->
                    <referenceDefaultSchemaName>entranceguard2</referenceDefaultSchemaName>
                    <verbose>true</verbose>
                    <logging>debug</logging>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.javassist</groupId>
                        <artifactId>javassist</artifactId>
                        <version>3.18.2-GA</version>
                    </dependency>
                    <dependency>
                        <groupId>org.liquibase.ext</groupId>
                        <artifactId>liquibase-hibernate4</artifactId>
                        <version>${liquibase-hibernate4.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-data-jpa</artifactId>
                        <version>1.3.1.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>

通过maven命令行传递参数, 参数形式如下所示

mvn liquibase:rollback -Dliquibase.rollbackTag=T4

http://www.liquibase.org/documentation/maven/generated/update-mojo.html#changeLogFile

直接在命令行调用liquibase

java -jar /xxx/liquibase-core.jar
--driver=com.mysql.jdbc.Driver
--classpath=/opt/temp/mysql.jar
--url="jdbc:mysql://localhost:3306/entranceguard?useUnicode=true&amp;characterEncoding=utf8"
--changeLogFile=/Users/yejianxin/workspace_hs_saas_all/hs-dms/hs-dms-database/resources/config/liquibase/master.xml
--username=root
--password=root
rollback
Release0100-2

主要流程是这样的,

  1. 通过运行update将所有变更导入到数据库,
  2. 通过tag命令进行更新标记 如果要rollback,
  3. 则可以根据时间或tag进行回滚

数据库脚本可以通过XML或sql形式存放,一般结构如下

这里写图片描述

master.xml文件是总文件,它会include其他需要执行的文件。 注意路径,可以用相对路径,只要将relativeToChangelogFile设为”true”

<?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"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd">

    <include file="./changelog/0100_20160217_01_init.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/0100_20160219_01_addtable.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/initdata/HS_PLATFORM_SYS_CONFIG.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/initdata/HS_PLATFORM_SYS_DICT.sql" relativeToChangelogFile="true"/>
    <include file="./changelog/initdata/HS_PLATFORM_SYS_LABEL.sql" relativeToChangelogFile="true"/>
</databaseChangeLog>

在SQL文件里,必须加上头标签如下所示

--liquibase formatted sql
--changeset JasonYe:Release0100-1
update table1 set column1=333;
--rollback delete from table1 where column1=333;
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Liquibase 管理 H2 数据库需要以下步骤: 1. 首先,需要在项目中添加 H2 数据库和 Liquibase 的支持。可以使用 Maven 或 Gradle 等构建工具来添加这些依赖项。 2. 然后,在项目中创建一个 Liquibase 的配置文件,例如 `liquibase.properties` 或者 `liquibase.yml`,并指定数据库连接信息、change log 文件等相关信息。 3. 在 `change log` 文件中,可以使用 Liquibase 的语法来定义数据库的 schema 和数据。例如,可以使用 `createTable` 来创建表,使用 `addColumn` 来添加列,使用 `insert` 来插入数据等。 4. 最后,在项目启动时,可以使用 Liquibase 的 API 来执行 change log 文件,并更新数据库的 schema 和数据。可以通过命令行或者 Java 代码来执行 Liquibase。 下面是一个示例 `liquibase.properties` 文件的内容: ``` driver: org.h2.Driver classpath: path/to/h2.jar url: jdbc:h2:mem:test username: sa password: changeLogFile: path/to/changelog.xml ``` 其中,`driver`、`classpath`、`url`、`username`、`password` 分别指定了数据库连接信息,`changeLogFile` 指定了 change log 文件的位置。 下面是一个示例 `changelog.xml` 文件的内容: ```xml <databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> <changeSet id="1" author="me"> <createTable tableName="person"> <column name="id" type="INT"> <constraints primaryKey="true" nullable="false"/> </column> <column name="name" type="VARCHAR(50)"> <constraints nullable="false"/> </column> <column name="age" type="INT"> <constraints nullable="true"/> </column> </createTable> </changeSet> <changeSet id="2" author="me"> <insert tableName="person"> <column name="id" value="1"/> <column name="name" value="John"/> <column name="age" value="30"/> </insert> <insert tableName="person"> <column name="id" value="2"/> <column name="name" value="Jane"/> <column name="age" value="25"/> </insert> </changeSet> </databaseChangeLog> ``` 其中,`changeSet` 标签用于表示一个变更集,`createTable` 标签用于创建表,`insert` 标签用于插入数据。 使用 Liquibase 可以方便地管理 H2 数据库的 schema 和数据,同时也可以兼容其他数据库。在实际开发中,可以根据具体需求来使用 Liquibase 的不同功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值