Flyway是一個簡單開源數據庫版本控制器(約定大於配置),主要提供migrate、clean、info、validate、baseline、repair等命令。它支持SQL(PL/SQL、T-SQL)方式和Java方式,支持命令行客戶端等,還提供一系列的插件支持(Maven、Gradle、SBT、ANT等)。mysql
官方網站:https://flywaydb.org/spring
下面咱們能夠經過對使用JdbcTemplate一文中的例子進行加工完成。讀者也能夠拿任何一個與數據訪問相關的工程來作以下內容的實驗:sql
pom.xml
中增長flyway的依賴:
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.0.3</version>
</dependency>
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 flyway.baselineOnMigrate=true flyway.enabled=false
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表。api
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
腳本與上一次執行的內容不一樣,提示報錯並終止程序,以避免形成更嚴重的數據結構破壞。springboot
文章轉載至:http://blog.didispace.com/spring-boot-flyway-db-version/數據結構