最简单的一个项目是一个软件连接到一个数据库,但是大多数项目中我们不仅要处理我们开发环境的副本,还需要处理其他很多副本。例如:开发环境、测试环境、生产环境。想到数据库管理,我们立刻就能想到一系列问题
Flyway是一款数据库版本控制管理工具,它可以简单的、可靠的升级你的数据库。它能帮助解决上面的问题。 Flyway核心是记录所有版本演化和状态的MetaData,首次启动创建默认名为SCHEMA_VERSION的元素表。 表中保存了版本,描述,要执行的sql脚本等信息 。 Flyway已经支持数据库包括:Oracle, SQL Server, SQL Azure, DB2, DB2 z/OS, MySQL (including Amazon RDS), MariaDB, Google Cloud SQL, PostgreSQL (including Amazon RDS and Heroku), Redshift, Vertica, H2, Hsql, Derby, SQLite, SAP HANA, solidDB, Sybase ASE and Phoeni
官网链接:Homepage - Flyway
Flyway是如何工作的?可以查看之前的一篇译文,这里就不再重复了,本文主要介绍Springboot如何集成flyway
【译文】数据库管理工具Flyway_学然后知不足!-CSDN博客
参考版本信息
示例信息 | 版本 |
springboot | 2.6.2 |
flyway | 7.15.0 |
mysql | 5.7.30 |
参考目录结构
1. 创建SpringBoot应用,并添加flyway-core依赖,本例中将实现初始化脚本到mysql数据库,因此同时引入了驱动依赖 mysql-connector-java
<dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>7.15.0</version> </dependency>
参考pom.xml依赖如下
<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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency> <dependency> <groupId>org.flywaydb</groupId> <artifactId>flyway-core</artifactId> <version>7.15.0</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.8.2</version> <scope>test</scope> </dependency> </dependencies>
2. 在application.properties中设置flyway信息
server.port=7002 ##是否启动,默认开启 spring.flyway.enabled = true ##脚本存放路径 spring.flyway.locations = classpath:db/migration ##当flyway第一次运行时,会在我们对应的数据库中新建一个记录脚本运行情况的 spring.flyway.table=flyway_schema_history ## flyway指向的数据库链接 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/runoob?useUnicode=true&characterEncoding=utf8 ## 用户名 spring.flyway.user=nacos ## 密码 spring.flyway.password=nacos ## 数据库驱动 spring.flyway.driver-class-name=com.mysql.cj.jdbc.Driver
3. 脚本整理
将脚本整理到resource/db.migration路径下,例如
参考SQL脚本信息如下
//V1.20190621.1854__CREATE_PERSION_TABLE.sql脚本内容 create table PERSON ( ID int not null, NAME varchar(100) not null ); //V1.20190621.1904__INIT_PERSION.sql 脚本内容 insert into PERSON (ID, NAME) values (1, 'Axel'); insert into PERSON (ID, NAME) values (2, 'Mr. Foo'); insert into PERSON (ID, NAME) values (3, 'Ms. Bar');
sql 目录中存放脚本文件,脚本名称命名方式
4. 运行启动主类,运行日志如下,从日志中可以看到如下信息
INFO 190688 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 7.15.0 by Redgate INFO 190688 --- [ main] o.f.c.i.database.base.BaseDatabaseType : Database: jdbc:mysql://127.0.0.1:3306/runoob (MySQL 5.7) INFO 190688 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 2 migrations (execution time 00:00.016s) INFO 190688 --- [ main] o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table `runoob`.`flyway_schema_history` with baseline ... INFO 190688 --- [ main] o.f.core.internal.command.DbBaseline : Successfully baselined schema with version: 1 INFO 190688 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `runoob`: 1 INFO 190688 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `runoob` to version "1.20190621.1854 - CREATE PERSION TABLE" INFO 190688 --- [ main] o.f.core.internal.command.DbMigrate : Migrating schema `runoob` to version "1.20190621.1904 - INIT PERSION" INFO 190688 --- [ main] o.f.core.internal.command.DbMigrate : Successfully applied 2 migrations to schema `runoob`, now at version v1.20190621.1904 (execution time 00:00.225s)
停止服务后,重新运行日志如下,从日志中可以看到信息
INFO 193184 --- [ main] o.f.c.internal.license.VersionPrinter : Flyway Community Edition 7.15.0 by Redgate INFO 193184 --- [ main] o.f.c.i.database.base.BaseDatabaseType : Database: jdbc:mysql://127.0.0.1:3306/runoob (MySQL 5.7) INFO 193184 --- [ main] o.f.core.internal.command.DbValidate : Successfully validated 3 migrations (execution time 00:00.024s) INFO 193184 --- [ main] o.f.core.internal.command.DbMigrate : Current version of schema `runoob`: 1.20190621.1904 INFO 193184 --- [ main] o.f.core.internal.command.DbMigrate : Schema `runoob` is up to date. No migration necessary.
查看Mysql数据库
1.Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s)
Caused by: org.flywaydb.core.api.FlywayException: Found non-empty schema(s) `runoob` but no schema history table. Use baseline() or set baselineOnMigrate to true to initialize the schema history table. at org.flywaydb.core.Flyway$1.execute(Flyway.java:200) ~[flyway-core-7.15.0.jar:na] at org.flywaydb.core.Flyway$1.execute(Flyway.java:170) ~[flyway-core-7.15.0.jar:na] at org.flywaydb.core.Flyway.execute(Flyway.java:586) ~[flyway-core-7.15.0.jar:na]
问题原因: 第一执行的时候没有找到schema history table ,这张表其实就是application.properties文件中spring.flyway.table属性配置的表,因此要么使用命令创建一个或者在application.properties文件中设置 spring.flyway.baseline-on-migrate=true ,
2.Caused by: org.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 5.7
Caused by: org.flywaydb.core.api.FlywayException: Unsupported Database: MySQL 5.7 at org.flywaydb.core.internal.database.DatabaseTypeRegister.getDatabaseTypeForConnection(DatabaseTypeRegister.java:106) ~[flyway-core-8.4.2.jar:na] at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:75) ~[flyway-core-8.4.2.jar:na] at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:143) ~[flyway-core-8.4.2.jar:na] at org.flywaydb.core.Flyway.migrate(Flyway.java:124) ~[flyway-core-8.4.2.jar:na]
问题原因:flyway-core对数据库版本有要求,例如flyway-core的当前最高版本V8.4.3,不能使用 MySQL 5.7, 当flyway-core 降低到V7.15.0后 问题解决,所以匹配flyway-core和数据库版本后问题即可解决
链接: 源码参考Demo-flyway
本文介绍了Springboot集成flyway方式
前一篇: SpringBoot 03 事件监听处理