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

网友投稿 1192 2022-10-15

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 #### #### ######################################################Starting Liquibase at 14:11:59 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)Liquibase Version: 4.9.1Liquibase Community 4.9.1 by LiquibaseStarting Example H2 Database...NOTE: The database does not persist data, so stopping and restarting this process will reset it back to a blank databaseConnection 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: letmeinOpening Database Console in Browser... Dev Web URL: Integration Web URL: 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 #### #### ######################################################Starting Liquibase at 16:13:58 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)Liquibase Version: 4.9.1Liquibase Community 4.9.1 by LiquibaseDo 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]:NNo operations will be reported. Simply add a liquibase.hub.apiKey setting to generate free deployment reports. Learn more at Updated properties file liquibase.properties to set liquibase.hub.mode=offRunning Changeset: example-changelog.xml::1::your.nameRunning Changeset: example-changelog.xml::2::your.nameRunning Changeset: example-changelog.xml::3::other.devLiquibase command 'update' was executed successfully.D:\src\examples\xml>

4 分析update命令的操作原理

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

example-comment example-comment example-comment

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: 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 databaseliquibase.command.referenceUrl: jdbc:h2:tcp://localhost:9090/mem:integration# Enter the username for your source databaseliquibase.command.referenceUsername: dbuser# Enter the password for your source databaseliquibase.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 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.# 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修改,再次执行

比如在末尾增加如下两行

完成内容如下:

example-comment example-comment example-comment

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 #### #### ######################################################Starting Liquibase at 14:38:37 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)Liquibase Version: 4.9.1Liquibase Community 4.9.1 by LiquibaseRunning Changeset: example-changelog.sql::1::redrose2100Liquibase 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 #### #### ######################################################Starting Liquibase at 14:41:45 (version 4.9.1 #1978 built at 2022-03-28 19:39+0000)Liquibase Version: 4.9.1Liquibase Community 4.9.1 by LiquibaseLiquibase command 'update' was executed successfully.D:\src\examples\sql>

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Liquibase----SQL格式通过update更新H2 Database数据库
下一篇:Netty分布式获取异线程释放对象
相关文章

 发表评论

暂时没有评论,来抢沙发吧~