SpringBoot整合Flyway數據庫版本管理

介紹

flyway官網

Flyway是一款數據庫版本控制管理工具,支持數據庫版本自動升級。

比如我們在開發過程當中,某個成員在某個表當中新增了一個字段,那麼開發庫和測試庫的同步就需要手動去完成。
還比如某個開發成員新增了一個數據表,假如想同步數據庫,那麼也需要手動去完成。

現在我們就可以使用flyway來幫我們自動的去完成這個工作。

 

使用步驟

第一步:新建一個springboot項目,引入flyway依賴,完整pom如下

<?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.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.fsl</groupId>
	<artifactId>springboot-flyway</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-flyway</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-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.flywaydb</groupId>
			<artifactId>flyway-core</artifactId>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
		<!-- JSON Configuration -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.6</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.flywaydb</groupId>
				<artifactId>flyway-maven-plugin</artifactId>
				<configuration>
					<user>root</user>
					<password>123456</password>
					<driver>com.mysql.cj.jdbc.Driver</driver>
					<url>jdbc:mysql://127.0.0.1:3307/springboot?serverTimezone=GMT</url>
					<baselineOnMigrate>true</baselineOnMigrate>
					<!-- //sql腳本位置,flyway會自動去找到這個目錄並且執行裏面的sql腳本 -->
					<locations>classpath:db/migration/</locations>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>

第二步:修改配置文件

#運行端口號
server.port=8084

#應用名稱
spring.application.name=springboot-flyway

#################################### jdbc_config   datasource ##################################
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useSSL=false&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
# 按照上面的數據源配置,會報時區錯誤,所以必須加上serverTimezone的信息
# 報錯信息爲:The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone
spring.datasource.url=jdbc:mysql://localhost:3307/springboot?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
# Hikari will use the above plus the following to setup connection pooling
spring.datasource.hikari.minimum-idle=5
spring.datasource.hikari.maximum-pool-size=15
spring.datasource.hikari.auto-commit=true
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.pool-name=DatebookHikariCP
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.connection-timeout=30000
spring.datasource.hikari.connection-test-query=SELECT 1


#################################### Actuator相關設置 ##################################
#開放所有頁面節點  默認只開啓了health、info兩個節點
management.endpoints.web.exposure.include=*
#顯示健康具體信息  默認不會顯示詳細信息
management.endpoint.health.show-details=always



#################################### flyway相關設置 ##################################
#是否開啓flyway
spring.flyway.enabled=true
#遷移腳本的路徑
spring.flyway.locations=classpath:db/migration
#檢查遷移腳本的位置是否存在
spring.flyway.check-location=true
#當遷移時發現目標schema非空,而且帶有沒有元數據的表時,是否自動執行基準遷移,默認false.
spring.flyway.baseline-on-migrate=true

#flyway.baseline-description對執行遷移時基準版本的描述.
#flyway.baseline-on-migrate當遷移時發現目標schema非空,而且帶有沒有元數據的表時,是否自動執行基準遷移,默認false.
#flyway.baseline-version開始執行基準遷移時對現有的schema的版本打標籤,默認值爲1.
#flyway.check-location檢查遷移腳本的位置是否存在,默認false.
#flyway.clean-on-validation-error當發現校驗錯誤時是否自動調用clean,默認false.
#flyway.enabled是否開啓flywary,默認true.
#flyway.encoding設置遷移時的編碼,默認UTF-8.
#flyway.ignore-failed-future-migration當讀取元數據表時是否忽略錯誤的遷移,默認false.
#flyway.init-sqls當初始化好連接時要執行的SQL.
#flyway.locations遷移腳本的位置,默認db/migration.
#flyway.out-of-order是否允許無序的遷移,默認false.
#flyway.password目標數據庫的密碼.
#flyway.placeholder-prefix設置每個placeholder的前綴,默認${.
#flyway.placeholder-replacementplaceholders是否要被替換,默認true.
#flyway.placeholder-suffix設置每個placeholder的後綴,默認}.
#flyway.placeholders.[placeholder name]設置placeholder的value
#flyway.schemas設定需要flywary遷移的schema,大小寫敏感,默認爲連接默認的schema.
#flyway.sql-migration-prefix遷移文件的前綴,默認爲V.
#flyway.sql-migration-separator遷移腳本的文件名分隔符,默認__
#flyway.sql-migration-suffix遷移腳本的後綴,默認爲.sql
#flyway.tableflyway使用的元數據表名,默認爲schema_version
#flyway.target遷移時使用的目標版本,默認爲latest version
#flyway.url遷移時使用的JDBC URL,如果沒有指定的話,將使用配置的主數據源
#flyway.user遷移數據庫的用戶名
#flyway.validate-on-migrate遷移時是否校驗,默認爲true.

第三步:在resource目錄下面建立db.migration目錄,裏面放置我們變動的sql文件

sql腳本的格式:V+版本號+雙下劃線+秒速+結束符

例如:
V1.1__test.sql
V20190429.1530__ops_update.sql 
V1__INIT_DATABASE.sql

 啓動項目之後,我們就會發現我們的sql文件已經自動的執行了,並且會生成一個flyway_schema_history數據表。

flyway在第一次運行的時候,會在數據庫當中建立一個flyway_schema_history數據表,這個數據表用來記錄每一次版本變更的信息。

上面我們是啓動項目讓腳本自動執行,假如我們不想啓動項目,我們可以使用flyway-maven-plugin來進行操作(我們在上面已經爲插件配置了數據庫連接的信息)

BaseLine:對已經存在數據庫Schema結構的數據庫一種解決方案。實現在非空數據庫新建MetaData表,並把
Migrations應用到該數據庫;也可以應用到已有表結構的數據庫中也可以實現添加Metadata表。

Clean:清除掉對應數據庫Schema中所有的對象,包括表結構,視圖,存儲過程等,clean操作在dev 和 test階
段很好用

Info:用於打印所有的Migrations的詳細和狀態信息,也是通過MetaData和Migrations完成的,可以快速定位
當前的數據庫版本

Migrate:首先檢測MetaData表是否生成,沒有生成自動生成,然後會掃描指定sql文件並與MetaData中的記錄
進行對比,進行版本升級

repair:修復metaData表

Undo:撤銷到上一個數據庫的版本

Validate:驗證以及apply的Migrations是否有變更,默認開啓的;原理是對比MetaData表與本地Migrations的
checkNum值,如果值相同則驗證通過,否則失敗。

我們看網上的教程這個插件的pom如下:

<plugin>
    <groupId>org.flywaydb</groupId>
    <artifactId>flyway-maven-plugin</artifactId>
    <version>5.0.3</version>
</plugin>

執行之後會出現下面的錯誤

[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.flywaydb:flyway-maven-plugin:5.0.3:clean (default-cli) 
on project springboot-flyway: org.flywaydb.core.api.FlywayException: Unable to connect to
the database. Configure the url, user and password! -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

正常的執行結果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章