一、Flyway簡介
Flyway是一款數據庫遷移(migration)工具。簡單點說,就是在你部署應用的時候,幫你執行數據庫腳本的工具。Flyway支持SQL和Java兩種類型的腳本,你可以將腳本打包到應用程序中,在應用程序啟動時,由Flyway來管理這些腳本的執行,這些腳本被Flyway稱之為migration。
就目前而言,我們部署應用的流程大概是這樣的:
開發人員將應用程序打包、按順序匯總并整理數據庫升級腳本
DBA拿到數據庫升級腳本檢查、備份、執行,以完成數據庫升級
應部署人員拿到應用部署包,備份、替換,以完成應用程序升級
引入Flyway之后的應用部署流程大概是這樣的:
開發人員將應用程序打包
應部署人員拿到應用部署包,備份、替換,以完成應用程序升(Flyway將自動執行升級/備份腳本)。
二、SpringBoot集成使用
1.pom.xml引入依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- mysql 依賴 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 數據庫版本管理 依賴 --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <!-- 數據庫訪問依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 自動生成get,set方法 依賴 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.application.properties
# 端口 server.port=8082 # 數據源 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false spring.datasource.username=root spring.datasource.password=root # flyway 注:可以完全不用配置 ## sql 腳本的位置,默認為 classpath:db/migration。可手動指定 spring.flyway.locations=classpath:db ## 指定數據源,如果沒有指定的話,將使用配置的主數據源 spring.flyway.url=jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false ## Flyway 管理的 Schema 列表,區分大小寫。默認連接對應的默認 Schema ## 如果這里明確指定了庫名,那么在 spring.flyway.url 連接中指定的庫名將無效 spring.flyway.schemas=test1 ## 用戶名 spring.flyway.user=root ## 密碼 spring.flyway.password=root ## 開啟,默認開啟 spring.flyway.enabled=true
3.resources創建db數據庫腳本文件夾
V0.1.0__init_table.sql
-- 創建表 create table t_startAlarm ( id int(100) primary key not null auto_increment, name varchar(100), type varchar(100) )
V0.1.1__init_table.sql
-- 初始化數據 INSERT INTO t_startAlarm ( id , name , type ) VALUES ("1","電飯煲","用來蒸飯")
V0.1.2__init_table.sql
-- 初始化數據 INSERT INTO t_startAlarm ( id , name , type ) VALUES ("2","電飯煲2","用來蒸飯2")
4.啟動DemoApplication主啟動類
4.1只有V0.1.0__init_table.sql和V0.1.1__init_table.sql。
項目啟動,Flyway 會自動創建一個 flyway_schema_history 表,這個表用來記錄數據庫的更新歷史。
有了這條記錄,下次再啟動項目,V0.1.1__init_table.sql 這個腳本文件就不會執行了,因為系統知道這個腳本已經執行過了,如果你還想讓 V0.1.1__init_table.sql 腳本再執行一遍,需要手動刪除 flyway_schema_history 表中的對應記錄,那么項目啟動時,這個腳本就會被執行了。
注意:
我們在定義腳本的時候,除了 V 字開頭的腳本之外,還有一種 R 字開頭的腳本,V 字開頭的腳本只會執行一次,而 R字開頭的腳本,只要腳本內容發生了變化,啟動時候就會執行。
使用了 Flyway之后,如果再想進行數據庫版本升級,就不用該以前的數據庫腳本了,直接創建新的數據庫腳本,項目在啟動時檢測了有新的更高版本的腳本,就會自動執行,這樣,在和其他同事配合工作時,也會方便很多。因為正常我們都是從Git 上拉代碼下來,不拉數據庫腳本,這樣要是有人更新了數據庫,其他同事不一定能夠收到最新的通知,使用了 Flyway就可以有效避免這個問題了。
所有的腳本,一旦執行了,就會在 flyway_schema_history表中有記錄,如果你不小心搞錯了,可以手動從 flyway_schema_history 表中刪除記錄,然后修改 SQL腳本后再重新啟動(生產環境不建議)。
測試數據庫中結果:
添加V0.1.2__init_table.sql,測試數據庫中結果
三、項目整體結構
代碼地址鏈接: SpringbootFlyway
參考文章
https://www.cnblogs.com/wangzh1guo/articles/13834706.html
到此這篇關于SpringBoot使用Flyway進行數據庫管理的文章就介紹到這了,更多相關SpringBoot Flyway數據庫管理內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/jike11231/article/details/120343725