Liquibase----XML格式通过update更新H2 Database数据库

简介: Liquibase----XML格式通过update更新H2 Database数据库

1 将Liquibase安装目录中的example文件夹拷贝至其他位置,如:

在这里插入图片描述

2 启动H2 database作为测试使用

在cmd窗口进入 example目录,然后执行 liquibase init start-h2 命令

D:\src\examples>liquibase init start-h2
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ##
##  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 at 14:11:59 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)
Liquibase Version: 4.9.1
Liquibase Community 4.9.1 by Liquibase
Starting Example H2 Database...
NOTE: The database does not persist data, so stopping and restarting this process will reset it back to a blank database

Connection Information:
  Dev database:
    JDBC URL: jdbc:h2:tcp://localhost:9090/mem:dev
    Username: dbuser
    Password: letmein
  Integration database:
    JDBC URL: jdbc:h2:tcp://localhost:9090/mem:integration
    Username: dbuser
    Password: letmein

Opening Database Console in Browser...
  Dev Web URL: http://192.168.2.69:8090/frame.jsp?jsessionid=6f0e45dd9a8f7c060f9b76e8633b183d
  Integration Web URL: http://192.168.2.69:8090/frame.jsp?jsessionid=a4cb3c7057107c91a2c7318b3c4a7841

同时在浏览器打开H2 database终端的web页面
在这里插入图片描述

3 执行update命令

新打开一个cmd窗口,进入 example/xml目录,然后执行 liquibase update 命令

D:\src\examples\xml>liquibase update
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ##
##  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 at 16:13:58 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)
Liquibase Version: 4.9.1
Liquibase Community 4.9.1 by Liquibase
Do you want to see this operation's report in Liquibase Hub, which improves team collaboration?
If so, enter your email. If not, enter [N] to no longer be prompted, or [S] to skip for now, but ask again next time [S]:
N
No operations will be reported. Simply add a liquibase.hub.apiKey setting to generate free deployment reports. Learn more at https://hub.liquibase.com
* Updated properties file liquibase.properties to set liquibase.hub.mode=off
Running Changeset: example-changelog.xml::1::your.name
Running Changeset: example-changelog.xml::2::your.name
Running Changeset: example-changelog.xml::3::other.dev
Liquibase command 'update' was executed successfully.

D:\src\examples\xml>

4 分析update命令的操作原理

执行update命令实质是参照example-changelog.xml文件执行了sql命令,打开example-changelog.xml文件,内容如下:可见,这里创建了一个person表,一个company表,此外还给person表增加了一个country的列

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

      <changeSet id="1" author="your.name" labels="example-label" context="example-context">
          <comment>example-comment</comment>
        <createTable tableName="person">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="address1" type="varchar(50)"/>
            <column name="address2" type="varchar(50)"/>
            <column name="city" type="varchar(30)"/>
        </createTable>
    </changeSet>

    <changeSet id="2" author="your.name" labels="example-label" context="example-context">
        <comment>example-comment</comment>
        <createTable tableName="company">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="address1" type="varchar(50)"/>
            <column name="address2" type="varchar(50)"/>
            <column name="city" type="varchar(30)"/>
        </createTable>
    </changeSet>

    <changeSet id="3" author="other.dev" labels="example-label" context="example-context">
        <comment>example-comment</comment>
        <addColumn tableName="person">
            <column name="country" type="varchar(2)"/>
        </addColumn>
    </changeSet>
</databaseChangeLog>

5 到H2 database查看结果

此时刷新H2 database的web页面,如下可以看到确实增加了person和company两个表,而且person表中有country列
在这里插入图片描述

6 执行liquibase update查找配置文件的流程

执行liquibase的命令是从配置文件liquibase.properties中读取配置的,如下,可以看出是读取example-changelog.xml中的sql配置命令,然后去链接jdbc:h2:tcp://localhost:9090/mem:devde 的数据库进行操作的

####     _     _             _ _
##      | |   (_)           (_) |
##      | |    _  __ _ _   _ _| |__   __ _ ___  ___
##      | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \
##      | |___| | (_| | |_| | | |_) | (_| \__ \  __/
##      \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|
##                  | |
##                  |_|
##
##      The liquibase.properties file stores properties which do not change often,
##      such as database connection information. Properties stored here save time
##      and reduce risk of mistyped command line arguments.
##      Learn more: https://www.liquibase.org/documentation/config_properties.html
####
####
##   Note about relative and absolute paths:
##      The liquibase.properties file requires paths for some properties.
##      The classpath is the path/to/resources (ex. src/main/resources).
##      The changeLogFile path is relative to the classpath.
##      The url H2 example below is relative to 'pwd' resource.
####
# Enter the path for your changelog file.
changeLogFile=example-changelog.xml

#### Enter the Target database 'url' information  ####
liquibase.command.url=jdbc:h2:tcp://localhost:9090/mem:dev

# Enter the username for your Target database.
liquibase.command.username: dbuser

# Enter the password for your Target database.
liquibase.command.password: letmein

#### Enter the Source Database 'referenceUrl' information ####
## The source database is the baseline or reference against which your target database is compared for diff/diffchangelog commands.

# Enter URL for the source database
liquibase.command.referenceUrl: jdbc:h2:tcp://localhost:9090/mem:integration

# Enter the username for your source database
liquibase.command.referenceUsername: dbuser

# Enter the password for your source database
liquibase.command.referencePassword: letmein

# Logging Configuration
# logLevel controls the amount of logging information generated. If not set, the default logLevel is INFO.
# Valid values, from least amount of logging to most, are:
#   OFF, ERROR, WARN, INFO, DEBUG, TRACE, ALL
# If you are having problems, setting the logLevel to DEBUG and re-running the command can be helpful.
# logLevel: DEBUG

# The logFile property controls where logging messages are sent. If this is not set, then logging messages are
# displayed on the console. If this is set, then messages will be sent to a file with the given name.
# logFile: liquibase.log


#### Liquibase Pro Key Information ####
# Learn more, contact support, or get or renew a Pro Key at https://www.liquibase.com/protrial
# liquibase.pro.licensekey:

#### Liquibase Hub Information ####
# Liquibase Hub is a free secure SaaS portal providing status reporting, monitoring & insights
# into your Liquibase database release automation.
# https://hub.liquibase.com

## Add your free Hub API key here
# liquibase.hub.apikey:
# liquibase.hub.mode:all




##  Get documentation at docs.liquibase.com       ##
##  Get certified courses at learn.liquibase.com  ##
##  Get support at liquibase.com/support         ##

liquibase.hub.mode=off

7 对example-changelog.xml修改,再次执行

比如在末尾增加如下两行

    <changeSet author="redrose2100" id="1">
        <addColumn tableName="company">
            <column name="country" type="varchar(2)"/>
        </addColumn>
    </changeSet>

完成内容如下:

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

      <changeSet id="1" author="your.name" labels="example-label" context="example-context">
          <comment>example-comment</comment>
        <createTable tableName="person">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="address1" type="varchar(50)"/>
            <column name="address2" type="varchar(50)"/>
            <column name="city" type="varchar(30)"/>
        </createTable>
    </changeSet>

    <changeSet id="2" author="your.name" labels="example-label" context="example-context">
        <comment>example-comment</comment>
        <createTable tableName="company">
            <column name="id" type="int" autoIncrement="true">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="name" type="varchar(50)">
                <constraints nullable="false"/>
            </column>
            <column name="address1" type="varchar(50)"/>
            <column name="address2" type="varchar(50)"/>
            <column name="city" type="varchar(30)"/>
        </createTable>
    </changeSet>

    <changeSet id="3" author="other.dev" labels="example-label" context="example-context">
        <comment>example-comment</comment>
        <addColumn tableName="person">
            <column name="country" type="varchar(2)"/>
        </addColumn>
    </changeSet>
    <changeSet author="redrose2100" id="1">
        <addColumn tableName="company">
            <column name="country" type="varchar(2)"/>
        </addColumn>
    </changeSet>
</databaseChangeLog>

8 再次执行update命令

执行回显如下所示,提示执行成功

D:\src\examples\sql>liquibase update
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ##
##  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 at 14:38:37 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)
Liquibase Version: 4.9.1
Liquibase Community 4.9.1 by Liquibase
Running Changeset: example-changelog.sql::1::redrose2100
Liquibase command 'update' was executed successfully.

D:\src\examples\sql>

9 刷新H2 database的WEB页面

如下,可以看到已经增加,字段类型也符合
在这里插入图片描述

10 此时不做任何修改再次执行一次update命令

如下,可以看出,此时并为做什么操作

D:\src\examples\sql>liquibase update
####################################################
##   _     _             _ _                      ##
##  | |   (_)           (_) |                     ##
##  | |    _  __ _ _   _ _| |__   __ _ ___  ___   ##
##  | |   | |/ _` | | | | | '_ \ / _` / __|/ _ \  ##
##  | |___| | (_| | |_| | | |_) | (_| \__ \  __/  ##
##  \_____/_|\__, |\__,_|_|_.__/ \__,_|___/\___|  ##
##              | |                               ##
##              |_|                               ##
##                                                ##
##  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 at 14:41:45 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)
Liquibase Version: 4.9.1
Liquibase Community 4.9.1 by Liquibase
Liquibase command 'update' was executed successfully.

D:\src\examples\sql>
目录
相关文章
|
5天前
|
NoSQL 关系型数据库 MySQL
AWS Database Migration Service 助力数据库搬迁
AWS Database Migration Service 助力数据库搬迁
|
17天前
|
XML JSON 前端开发
C#使用HttpClient四种请求数据格式:json、表单数据、文件上传、xml格式
C#使用HttpClient四种请求数据格式:json、表单数据、文件上传、xml格式
197 0
|
1月前
|
SQL XML Java
mybatis :sqlmapconfig.xml配置 ++++Mapper XML 文件(sql/insert/delete/update/select)(增删改查)用法
当然,这些仅是MyBatis功能的初步介绍。MyBatis还提供了高级特性,如动态SQL、类型处理器、插件等,可以进一步提供对数据库交互的强大支持和灵活性。希望上述内容对您理解MyBatis的基本操作有所帮助。在实际使用中,您可能还需要根据具体的业务要求调整和优化SQL语句和配置。
39 1
|
3月前
|
SQL 关系型数据库 MySQL
云服务器 ECS产品使用问题之出现“1044 - Access denied for user ‘root‘@‘%‘ to database ‘数据库名称‘”这样的错误,该怎么办
云服务器ECS(Elastic Compute Service)是各大云服务商阿里云提供的一种基础云计算服务,它允许用户租用云端计算资源来部署和运行各种应用程序。以下是一个关于如何使用ECS产品的综合指南。
|
4月前
|
存储 关系型数据库 分布式数据库
PolarDB产品使用问题之如何避免在修改数据库的编码格式时出现乱码状况
PolarDB产品使用合集涵盖了从创建与管理、数据管理、性能优化与诊断、安全与合规到生态与集成、运维与支持等全方位的功能和服务,旨在帮助企业轻松构建高可用、高性能且易于管理的数据库环境,满足不同业务场景的需求。用户可以通过阿里云控制台、API、SDK等方式便捷地使用这些功能,实现数据库的高效运维与持续优化。
104 5
|
4月前
|
SQL druid Java
传统后端SQL数据层替代解决方案: 内置数据源+JdbcTemplate+H2数据库 详解
传统后端SQL数据层替代解决方案: 内置数据源+JdbcTemplate+H2数据库 详解
56 1
|
3月前
|
关系型数据库 MySQL 数据库
生成订单的过程------支付系统21------支付宝支付----统一收单下单并支付页面接口----创建订单,下订单,我们要在我们数据库的订单表中,设置订单,订单表常用数据库设置格式
生成订单的过程------支付系统21------支付宝支付----统一收单下单并支付页面接口----创建订单,下订单,我们要在我们数据库的订单表中,设置订单,订单表常用数据库设置格式
|
3月前
|
数据库
数据库bug-[08001] Could not create connection to database server. Attempted reconnect 3,主机名ip必须写对
数据库bug-[08001] Could not create connection to database server. Attempted reconnect 3,主机名ip必须写对
|
4月前
|
XML JSON 开发框架
一篇文章讲明白JSON格式转换成XML格式
一篇文章讲明白JSON格式转换成XML格式
23 0
|
4月前
|
XML JSON 开发框架
一篇文章讲明白JSON格式转换成XML格式
一篇文章讲明白JSON格式转换成XML格式
33 0