当前位置: 移动技术网 > IT编程>开发语言>Java > 详解Spring Boot中使用Flyway来管理数据库版本

详解Spring Boot中使用Flyway来管理数据库版本

2019年07月19日  | 移动技术网IT编程  | 我要评论

如果没有读过上面内容的读者,有兴趣的可以一阅。在上面的使用jdbctemplate一文中,主要通过spring提供的jdbctemplate实现对用户表的增删改查操作。在实现这个例子的时候,我们事先在mysql中创建了用户表。创建表的过程我们在实际开发系统的时候会经常使用,但是一直有一个问题存在,由于一个系统的程序版本通过git得到了很好的版本控制,而数据库结构并没有,即使我们通过git进行了语句的版本化,那么在各个环境的数据库中如何做好版本管理呢?下面我们就通过本文来学习一下在spring boot中如何使用flyway来管理数据库的版本。

flyway简介

flyway是一个简单开源数据库版本控制器(约定大于配置),主要提供migrate、clean、info、validate、baseline、repair等命令。它支持sql(pl/sql、t-sql)方式和java方式,支持命令行客户端等,还提供一系列的插件支持(maven、gradle、sbt、ant等)。

官方网站:

本文对于flyway的自身功能不做过多的介绍,读者可以通过阅读官方文档或利用搜索引擎获得更多资料。下面我们具体说说在spring boot应用中的应用,如何使用flyway来创建数据库以及结构不一致的检查。

动手试一试

下面我们可以通过对使用jdbctemplate一文中的例子进行加工完成。读者也可以拿任何一个与数据访问相关的工程来做如下内容的实验:

第一步,在pom.xml中增加flyway的依赖:

<dependency>
 <groupid>org.flywaydb</groupid>
 <artifactid>flyway-core</artifactid>
 <version>5.0.3</version>
</dependency>

第二步,按flyway的规范创建版本化的sql脚本。

在工程的src/main/resources目录下创建db目录

在db目录下创建版本化的sql脚本v1__base_version.sql

drop table if exists user ;
create table `user` (
 `id` bigint(20) not null auto_increment comment '主键',
 `name` varchar(20) not null comment '姓名',
 `age` int(5) default null comment '年龄',
 primary key (`id`)
) engine=innodb default charset=utf8mb4;

第三步,在application.properties文件中配置flyway要加载的sql脚本位置。按第二步创建的结果配置如下:

flyway.locations=classpath:/db

第四步,执行单元测试applicationtests,此时我们在日志中可以看到如下信息:

info 82441 --- [main] o.f.core.internal.util.versionprinter  : flyway community edition 5.0.3 by boxfuse
info 82441 --- [main] o.f.c.internal.database.databasefactory : database: jdbc:mysql://localhost:3306/test (mysql 5.7)
info 82441 --- [main] o.f.core.internal.command.dbvalidate   : successfully validated 1 migration (execution time 00:00.022s)
info 82441 --- [main] o.f.c.i.s.jdbctableschemahistory     : creating schema history table: `test`.`flyway_schema_history`
info 82441 --- [main] o.f.core.internal.command.dbmigrate   : current version of schema `test`: << empty schema >>
info 82441 --- [main] o.f.core.internal.command.dbmigrate   : migrating schema `test` to version 1 - base version
warn 82441 --- [main] o.f.core.internal.sqlscript.sqlscript  : db: unknown table 'test.user' (sql state: 42s02 - error code: 1051)
info 82441 --- [main] o.f.core.internal.command.dbmigrate   : successfully applied 1 migration to schema `test` (execution time 00:00.128s)

flyway监测到需要运行版本脚本来初始化数据库,因此执行了v1__base_version.sql脚本,从而创建了user表,这才得以让一系列单元测试(对user表的crud操作)通过。

第五步,我们可以继续再执行一下单元测试,此时我们会发现日志输出与之前不同:

info 83150 --- [main] o.f.core.internal.util.versionprinter  : flyway community edition 5.0.3 by boxfuse
info 83150 --- [main] o.f.c.internal.database.databasefactory : database: jdbc:mysql://localhost:3306/test (mysql 5.7)
info 83150 --- [main] o.f.core.internal.command.dbvalidate   : successfully validated 1 migration (execution time 00:00.031s)
info 83150 --- [main] o.f.core.internal.command.dbmigrate   : current version of schema `test`: 1
info 83150 --- [main] o.f.core.internal.command.dbmigrate   : schema `test` is up to date. no migration necessary.

由于在第四步的时候,初始化脚本已经执行过,所以这次执行就没有再去执行v1__base_version.sql脚本来重建user表。

第六步,我们可以尝试修改一下v1__base_version.sql脚本中的name字段长度,然后在运行一下单元测试,此时我们可以得到如下错误:

error 83791 --- [main] o.s.boot.springapplication        : application startup failed

org.springframework.beans.factory.beancreationexception: error creating bean with name 'flywayinitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/flywayautoconfiguration$flywayconfiguration.class]: invocation of init method failed; nested exception is org.flywaydb.core.api.flywayexception: validate failed: migration checksum mismatch for migration version 1
-> applied to database : 466264992
-> resolved locally  : -270269434

由于初始化脚本的改动,flyway校验失败,认为当前的v1__base_version.sql脚本与上一次执行的内容不同,提示报错并终止程序,以免造成更严重的数据结构破坏。

总结

到这里为止,本文的内容告一段落。由于博文篇幅问题,对于flyway更细节的使用没有说的太多,本文主要作为敲门砖,帮助和引导正在使用spring boot做系统开发的个人或团队在数据库的版本控制上做的更好提供一些思路。至于更深入的应用还请读者自行翻阅官方文档参考和学习。

本文代码:github:https://github.com/dyc87112/springboot-learning/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网