Flyway簡介
Flyway是一個簡單開源數(shù)據(jù)庫版本控制器(約定大于配置),主要提供migrate、clean、info、validate、baseline、repair等命令。它支持SQL(PL/SQL、T-SQL)方式和Java方式,支持命令行客戶端等,還提供一系列的插件支持(Maven、Gradle、SBT、ANT等)。
官方網(wǎng)站:https://flywaydb.org/
本文對于Flyway的自身功能不做過多的介紹,讀者可以通過閱讀官方文檔或利用搜索引擎獲得更多資料。下面我們具體說說在Spring Boot應(yīng)用中的應(yīng)用,如何使用Flyway來創(chuàng)建數(shù)據(jù)庫以及結(jié)構(gòu)不一致的檢查。
#動手試一試
下面我們可以通過對使用JdbcTemplate一文中的例子進(jìn)行加工完成。讀者也可以拿任何一個與數(shù)據(jù)訪問相關(guān)的工程來做如下內(nèi)容的實驗:
- 第一步,在
pom.xml
中增加flyway的依賴:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.3</version>
</dependency>
- 第二步,按Flyway的規(guī)范創(chuàng)建版本化的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腳本位置。按第二步創(chuàng)建的結(jié)果配置如下:
flyway.locations=classpath:/db
- 第四步,執(zhí)行單元測試
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監(jiān)測到需要運行版本腳本來初始化數(shù)據(jù)庫,因此執(zhí)行了V1__Base_version.sql
腳本,從而創(chuàng)建了user表,這才得以讓一系列單元測試(對user表的CRUD操作)通過。
- 第五步,我們可以繼續(xù)再執(zhí)行一下單元測試,此時我們會發(fā)現(xiàn)日志輸出與之前不同:
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.
由于在第四步的時候,初始化腳本已經(jīng)執(zhí)行過,所以這次執(zhí)行就沒有再去執(zhí)行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
由于初始化腳本的改動,F(xiàn)lyway校驗失敗,認(rèn)為當(dāng)前的V1__Base_version.sql
腳本與上一次執(zhí)行的內(nèi)容不同,提示報錯并終止程序,以免造成更嚴(yán)重的數(shù)據(jù)結(jié)構(gòu)破壞。