一、Flyway簡(jiǎn)介
Flyway是一款數(shù)據(jù)庫(kù)遷移(migration)工具。簡(jiǎn)單點(diǎn)說(shuō),就是在你部署應(yīng)用的時(shí)候,幫你執(zhí)行數(shù)據(jù)庫(kù)腳本的工具。Flyway支持SQL和Java兩種類(lèi)型的腳本,你可以將腳本打包到應(yīng)用程序中,在應(yīng)用程序啟動(dòng)時(shí),由Flyway來(lái)管理這些腳本的執(zhí)行,這些腳本被Flyway稱(chēng)之為migration。
就目前而言,我們部署應(yīng)用的流程大概是這樣的:
開(kāi)發(fā)人員將應(yīng)用程序打包、按順序匯總并整理數(shù)據(jù)庫(kù)升級(jí)腳本
DBA拿到數(shù)據(jù)庫(kù)升級(jí)腳本檢查、備份、執(zhí)行,以完成數(shù)據(jù)庫(kù)升級(jí)
應(yīng)部署人員拿到應(yīng)用部署包,備份、替換,以完成應(yīng)用程序升級(jí)
引入Flyway之后的應(yīng)用部署流程大概是這樣的:
開(kāi)發(fā)人員將應(yīng)用程序打包
應(yīng)部署人員拿到應(yīng)用部署包,備份、替換,以完成應(yīng)用程序升(Flyway將自動(dòng)執(zhí)行升級(jí)/備份腳本)。
二、SpringBoot集成使用
1.pom.xml引入依賴(lài)
<?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 依賴(lài) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 數(shù)據(jù)庫(kù)版本管理 依賴(lài) --> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> </dependency> <!-- 數(shù)據(jù)庫(kù)訪問(wèn)依賴(lài) --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- 自動(dòng)生成get,set方法 依賴(lài) --> <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 # 數(shù)據(jù)源 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 腳本的位置,默認(rèn)為 classpath:db/migration。可手動(dòng)指定 spring.flyway.locations=classpath:db ## 指定數(shù)據(jù)源,如果沒(méi)有指定的話,將使用配置的主數(shù)據(jù)源 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 列表,區(qū)分大小寫(xiě)。默認(rèn)連接對(duì)應(yīng)的默認(rèn) Schema ## 如果這里明確指定了庫(kù)名,那么在 spring.flyway.url 連接中指定的庫(kù)名將無(wú)效 spring.flyway.schemas=test1 ## 用戶(hù)名 spring.flyway.user=root ## 密碼 spring.flyway.password=root ## 開(kāi)啟,默認(rèn)開(kāi)啟 spring.flyway.enabled=true
3.resources創(chuàng)建db數(shù)據(jù)庫(kù)腳本文件夾
V0.1.0__init_table.sql
-- 創(chuàng)建表 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
-- 初始化數(shù)據(jù) INSERT INTO t_startAlarm ( id , name , type ) VALUES ("1","電飯煲","用來(lái)蒸飯")
V0.1.2__init_table.sql
-- 初始化數(shù)據(jù) INSERT INTO t_startAlarm ( id , name , type ) VALUES ("2","電飯煲2","用來(lái)蒸飯2")
4.啟動(dòng)DemoApplication主啟動(dòng)類(lèi)
4.1只有V0.1.0__init_table.sql和V0.1.1__init_table.sql。
項(xiàng)目啟動(dòng),F(xiàn)lyway 會(huì)自動(dòng)創(chuàng)建一個(gè) flyway_schema_history 表,這個(gè)表用來(lái)記錄數(shù)據(jù)庫(kù)的更新歷史。
有了這條記錄,下次再啟動(dòng)項(xiàng)目,V0.1.1__init_table.sql 這個(gè)腳本文件就不會(huì)執(zhí)行了,因?yàn)橄到y(tǒng)知道這個(gè)腳本已經(jīng)執(zhí)行過(guò)了,如果你還想讓 V0.1.1__init_table.sql 腳本再執(zhí)行一遍,需要手動(dòng)刪除 flyway_schema_history 表中的對(duì)應(yīng)記錄,那么項(xiàng)目啟動(dòng)時(shí),這個(gè)腳本就會(huì)被執(zhí)行了。
注意:
我們?cè)诙x腳本的時(shí)候,除了 V 字開(kāi)頭的腳本之外,還有一種 R 字開(kāi)頭的腳本,V 字開(kāi)頭的腳本只會(huì)執(zhí)行一次,而 R字開(kāi)頭的腳本,只要腳本內(nèi)容發(fā)生了變化,啟動(dòng)時(shí)候就會(huì)執(zhí)行。
使用了 Flyway之后,如果再想進(jìn)行數(shù)據(jù)庫(kù)版本升級(jí),就不用該以前的數(shù)據(jù)庫(kù)腳本了,直接創(chuàng)建新的數(shù)據(jù)庫(kù)腳本,項(xiàng)目在啟動(dòng)時(shí)檢測(cè)了有新的更高版本的腳本,就會(huì)自動(dòng)執(zhí)行,這樣,在和其他同事配合工作時(shí),也會(huì)方便很多。因?yàn)檎N覀兌际菑腉it 上拉代碼下來(lái),不拉數(shù)據(jù)庫(kù)腳本,這樣要是有人更新了數(shù)據(jù)庫(kù),其他同事不一定能夠收到最新的通知,使用了 Flyway就可以有效避免這個(gè)問(wèn)題了。
所有的腳本,一旦執(zhí)行了,就會(huì)在 flyway_schema_history表中有記錄,如果你不小心搞錯(cuò)了,可以手動(dòng)從 flyway_schema_history 表中刪除記錄,然后修改 SQL腳本后再重新啟動(dòng)(生產(chǎn)環(huán)境不建議)。
測(cè)試數(shù)據(jù)庫(kù)中結(jié)果:
添加V0.1.2__init_table.sql,測(cè)試數(shù)據(jù)庫(kù)中結(jié)果
三、項(xiàng)目整體結(jié)構(gòu)
代碼地址鏈接: SpringbootFlyway
參考文章
https://www.cnblogs.com/wangzh1guo/articles/13834706.html
到此這篇關(guān)于SpringBoot使用Flyway進(jìn)行數(shù)據(jù)庫(kù)管理的文章就介紹到這了,更多相關(guān)SpringBoot Flyway數(shù)據(jù)庫(kù)管理內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/jike11231/article/details/120343725