liquibase使用

本文介绍如何使用Liquibase进行数据库版本管理和迁移,包括创建表、配置pom.xml、生成及更新ChangeLog文件等操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 创建表

drop database if exists mybatis;
create database mybatis;
use mybatis;

create table mybatis.CUSTOMERS (
  ID bigint not null primary key,
  NAME varchar(15) not null,
  EMAIL varchar(128) not null,
  PASSWORD varchar(8) not null,  
  PHONE int ,  
  ADDRESS varchar(255),
  SEX char(1) ,
  IS_MARRIED bit,
  DESCRIPTION text,
  IMAGE blob,
  BIRTHDAY date,
  REGISTERED_TIME timestamp
);


select * from mybatis.CUSTOMERS;

2. 配置pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>middleware</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <artifactId>liquibase</artifactId>

    <properties>
        <jdbc.driver>com.mysql.cj.jdbc.Driver</jdbc.driver>
        <jdbc.url>jdbc:mysql://localhost:3306/mybatis?useUnicode=true&amp;characterEncoding=utf8&amp;serverTimezone=GMT&amp;useSSL=false&amp;allowPublicKeyRetrieval=true</jdbc.url>
        <jdbc.username>root</jdbc.username>
        <jdbc.password>1234</jdbc.password>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>3.5.3</version>
                <configuration>
                    <!--指定执行主文件 -->
                    <changeLogFile>${basedir}/src/main/resources/conf/liquibase/master_changelog.xml</changeLogFile>
                    <diffChangeLogFile>${basedir}/src/main/resources/conf/liquibase/changelog/${maven.build.timestamp}_changelog.xml</diffChangeLogFile>
                    <outputChangeLogFile>${basedir}/src/main/resources/conf/liquibase/changelog/changelog_original.xml</outputChangeLogFile>

                    <driver>${jdbc.driver}</driver>
                    <url>${jdbc.url}</url>
                    <username>${jdbc.username}</username>
                    <password>${jdbc.password}</password>

                    <dropFirst>false</dropFirst>
                    <defaultSchemaName />
                    <referenceUrl>hibernate:spring:com.jaguar.myapp.domain?dialect=&amp;hibernate.ejb.naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringNamingStrategy</referenceUrl>
                    <verbose>true</verbose>
                    <logging>debug</logging>

                    <!-- 是否需要弹出确认框 -->
                    <promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
                    <!--输出文件的编码 -->
                    <outputFileEncoding>UTF-8</outputFileEncoding>
                    <!--执行的时候是否显示详细的参数信息 -->
                    <verbose>true</verbose>
                    <!--是否每次都重新加载properties -->
                    <propertyFileWillOverride>true</propertyFileWillOverride>
                    <rollbackTag>${project.version}</rollbackTag>
                    <tag>${project.version}</tag>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>

        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.6.1</version>
        </dependency>
    </dependencies>
</project>


3. 根据数据库反向生成changeLog文件    mvn liquibase:generateChangeLog

创建空changelog_original.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
</databaseChangeLog>

执行 mvn liquibase:generateChangeLog


changelog_original.xml

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
    <changeSet author="kd (generated)" id="1529903520054-1">
        <createTable tableName="customers">
            <column name="ID" type="BIGINT">
                <constraints nullable="false"/>
            </column>
            <column name="NAME" type="VARCHAR(15)">
                <constraints nullable="false"/>
            </column>
            <column name="EMAIL" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            <column name="PASSWORD" type="VARCHAR(8)">
                <constraints nullable="false"/>
            </column>
            <column name="PHONE" type="INT"/>
            <column name="ADDRESS" type="VARCHAR(255)"/>
            <column name="SEX" type="CHAR(1)"/>
            <column name="IS_MARRIED" type="BIT(1)"/>
            <column name="DESCRIPTION" type="TEXT"/>
            <column name="IMAGE" type="BLOB"/>
            <column name="BIRTHDAY" type="date"/>
            <column name="REGISTERED_TIME" type="TIMESTAMP(26)"/>
        </createTable>
    </changeSet>
    <changeSet author="kd (generated)" id="1529903520054-2">
        <addPrimaryKey columnNames="ID" constraintName="PRIMARY" tableName="customers"/>
    </changeSet>
</databaseChangeLog>

4. 清空当前数据库,包括liquibase的版本信息 mvn liquibase:dropAll



5. 将xml的改变更新到数据库     mvn liquibase:update

(1) 指定执行主文件 master_changelog.xml

<?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.5.xsd">

    <!-- mvn liquibase:update -->
    <include file="conf/liquibase/changelog/00000000000000_initial_common.xml" relativeToChangelogFile="false"/>
</databaseChangeLog>

(2) 初始化的文件 00000000000000_initial_common.xml

拷贝自changelog_original.xml


两个重要改动

a) 定义了autoIncrement, 关联

b) 将TIMESTAMP(26) 改为TIMESTAMP

<?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.5.xsd">

    <property name="now" value="now()" dbms="mysql,h2"/>
    <property name="now" value="current_timestamp" dbms="postgresql"/>
    <property name="now" value="sysdate" dbms="oracle"/>

    <property name="autoIncrement" value="true" dbms="mysql,h2,postgresql,oracle"/>

     <changeSet author="kd (generated)" id="1529903520054-1">
        <createTable tableName="customers">
            <column name="ID" type="BIGINT" autoIncrement="${autoIncrement}">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="NAME" type="VARCHAR(15)">
                <constraints nullable="false"/>
            </column>
            <column name="EMAIL" type="VARCHAR(128)">
                <constraints nullable="false"/>
            </column>
            <column name="PASSWORD" type="VARCHAR(8)">
                <constraints nullable="false"/>
            </column>
            <column name="PHONE" type="INT"/>
            <column name="ADDRESS" type="VARCHAR(255)"/>
            <column name="SEX" type="CHAR(1)"/>
            <column name="IS_MARRIED" type="BIT(1)"/>
            <column name="DESCRIPTION" type="TEXT"/>
            <column name="IMAGE" type="BLOB"/>
            <column name="BIRTHDAY" type="date"/>
            <column name="REGISTERED_TIME" type="TIMESTAMP"/>
        </createTable>
    </changeSet>


    <changeSet id="00000000000000-05" author="shj">
        <sqlFile path="conf/liquibase/preloaddata/dml.sql"/>
    </changeSet>
</databaseChangeLog>


(3) dml.sql

Insert into mybatis.CUSTOMERS (NAME,EMAIL,PASSWORD, PHONE,  ADDRESS,SEX,IS_MARRIED,DESCRIPTION,IMAGE,BIRTHDAY,REGISTERED_TIME) 
values ('customer','customer@customer.com','1234',123,'customer address','女',1,'customer description',null,now(),now());

执行结果




alter table mybatis.CUSTOMERS add test varchar(25);

再次update...





6. 根据数据库反向生成changeLog文件

执行 mvn liquibase:dbDoc



最常用的命令说明:

update(将xml的改变更新到数据库)

rollback(回滚到某一版本或者某一时刻,必须要带上rollbackTag参数)

dbDoc (生成数据库文档)

dropAll(慎用,清空当前数据库,包括liquibase的版本信息)

generateChangeLog(根据数据库反向生成changeLog文件)

tag(为当前数据库打上标签)




### 回答1: Liquibase是一个数据库版本控制工具,可以用来管理数据库变更。下面是Liquibase的简单使用步骤: 1. 创建一个Liquibase项目,可以使用Maven或Gradle等构建工具来创建。 2. 配置Liquibase,包括数据库连接信息、变更日志文件路径等。 3. 创建变更集,可以使用XML、YAML或JSON等格式来定义。 4. 运行Liquibase命令来执行变更集,例如执行更新命令: ``` liquibase update ``` 5. 查看数据库变更记录,可以使用命令: ``` liquibase history ``` 6. 回滚数据库变更,可以使用命令: ``` liquibase rollback <tag> ``` 其中,<tag>是变更集的标签,用于指定回滚到哪个点。 以上是Liquibase的简单使用步骤,更多详细信息请参考官方文档。 ### 回答2: Liquibase是一个开源的数据库版本控制工具,可以帮助开发人员管理数据库的演变过程。它使用XML、YAML或JSON格式的脚本文件来定义数据库结构和数据的变更,从而使数据库的追踪和同步变得更加容易。 Liquibase的简单使用可以分为以下几个步骤: 1. 引入Liquibase依赖:在项目的构建配置文件中添加Liquibase的依赖项,例如在Maven项目中,在pom.xml文件中添加Liquibase相关的依赖。 2. 创建Liquibase配置文件:在项目的根目录下创建一个名为liquibase.properties的文件,并配置数据库连接信息,如数据库驱动、URL、用户名和密码等。 3. 编写变更脚本:创建一个名为changelog.xml的文件,用于定义数据库的结构和数据的变更。可以使用Liquibase提供的各种标签和属性来描述添加表、字段、索引、约束、存储过程等操作。 4. 执行变更脚本:使用Liquibase命令行工具或API来执行变更脚本。命令行方式可以通过运行liquibase update命令来应用所有未应用的变更,API方式可以在应用程序启动时通过编程方式来初始化和执行变更脚本。 5. 查看数据库状态:可以使用Liquibase的status命令来查看数据库中已应用和未应用的变更,以及当前数据库的状态信息。 通过上述步骤,我们可以很容易地使用Liquibase来管理数据库的变更。它不仅可以帮助我们追踪数据库结构和数据的变化历史,还能够自动处理数据库的升级和回滚,大大简化了数据库的管理工作,提高了开发人员的工作效率。 ### 回答3: Liquibase是一个流行的开源数据库重构工具,可以帮助开发人员对数据库进行版本控制和管理。 使用Liquibase进行数据库重构的步骤如下: 1. 首先,需要在项目中引入Liquibase的库文件。可以通过将Liquibase的依赖添加到项目的构建文件(如pom.xml)中,或者手动下载并添加到项目中。 2. 然后,在项目的资源文件夹中创建一个Liquibase配置文件(如liquibase.properties、liquibase.yml或liquibase.xml)。配置文件中包含了数据库连接信息、变更集文件路径等配置项。 3. 创建一个变更集文件(如changelog.xml),用于存储数据库的变更历史。可以通过添加与数据库相关的标签(如创建表、修改表结构、插入数据等)来定义具体的变更操作。 4. 运行Liquibase命令行工具或使用代码方式启动Liquibase,执行变更集文件中的变更操作。Liquibase会根据变更集文件中定义的变更操作,自动检测数据库的当前状态,并将其与变更集文件中的期望状态进行比对,然后执行必要的变更操作以使数据库达到期望状态。 5. 可以使用Liquibase提供的其他功能,如回滚变更、生成数据库文档等。 总结来说,Liquibase简单使用的步骤包括引入Liquibase库文件、创建配置文件、定义变更集文件,然后通过运行Liquibase命令行工具或使用代码方式执行变更操作。Liquibase可以帮助开发人员在多人协作或持续集成环境中轻松管理和控制数据库的变更。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值