flyway的命令
1、migration命令
将Schema Migrate到最新版本。如果flyway_schema_history不存在,Flyway将自动创建它。Migrate是Flyway工作流程的核心。它将扫描文件系统或类路径以获取可用的Migrate。它会将它们与已应用于数据库的Migrate进行比较。如果发现任何差异,它将Migrate数据库以缩小差距。Migrate最好应在应用程序启动时执行,以避免数据库与代码期望之间的任何不兼容性。
配置项:locations
[
](https://blog.csdn.net/MGL_1/article/details/88426618)
2、clean命令
清除掉对应数据库Schema中的所有对象。Clean对开发和测试有很大的帮助。通过彻底清除已配置的schemas,它将有效地为您提供一个全新的开始。所有对象(表、视图、过程等)都将被删除。注意:不要在生产环境使用
3、Info命令
打印所有Migrations的详细信息和状态信息。打印信息让你看到哪些Migrations已经应用,哪些迁移还在等待执行,何时执行,以及迁移是否成功。
4、Validate命令
验证已经Apply的Migrations是否有变更,Flyway是默认是开启验证的。Validate原理是对比Metadata表与本地Migrations的Checksum值,如果值相同则验证通过,否则验证失败,从而可以防止对已经Apply到数据库的本地Migrations的无意修改。
[
](https://blog.csdn.net/MGL_1/article/details/88426618)
5、Baseline命令
Baseline是针对已经存在Schema结构的数据库的一种解决方案,即实现在非空数据库中新建Metadata表,并把Migrations应用到该数据库。Baseline可以应用到特定的版本,这样在已有表结构的数据库中也可以实现添加Metadata表,从而利用Flyway进行新Migrations的管理了。如果表中已经存在了Metadata表,那么baseline相关的配置无效。
主要配置有:
- baselineVersion
- baselineDescription
- table
6、Repair命令
Repair是修复flyway_schema_history表的问题的工具。它有两个主要用途:
- 删除失败的迁移条目
- 将应用的迁移的校验和,描述和类型与可用的迁移重新对齐
[
](https://blog.csdn.net/MGL_1/article/details/88426618)
fly的常用配置
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.
问题
1、flyway-core不支持mariadb,会错误的显示为不支持mysql5.5 版本
改正方法:
因为使用的数据库为mariadb,所以需要将工程中得mysql驱动包换为mariadb驱动包,并且使用flyway-mysql代替flyway-core
<dependencies>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-mysql</artifactId>
<version>8.2.3</version>
</dependency>
</dependencies>
配置置
application.properties
中得数据库驱动# 设置数据库的配置
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/ltblog?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.username=longtao
spring.datasource.password=abcd1234
2、出现Error creating bean with name ‘flywayInitializer’ defined in class path resource…错误
问题描述:
出现以下错误:
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 java.lang.AbstractMethodError: org.flywaydb.core.internal.database.base.Database.doQuote(Ljava/lang/String;)Ljava/lang/String;
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]
at com.liulongtao.ltblogs.LtBlogApplication.main(LtBlogApplication.java:10) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_271]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_271]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_271]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_271]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.6.3.jar:2.6.3]
Caused by: java.lang.AbstractMethodError: org.flywaydb.core.internal.database.base.Database.doQuote(Ljava/lang/String;)Ljava/lang/String;
at org.flywaydb.core.internal.database.base.Database.quote(Database.java:225) ~[flyway-core-8.0.5.jar:na]
at org.flywaydb.core.internal.database.base.Schema.toString(Schema.java:217) ~[flyway-core-8.0.5.jar:na]
at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_271]
at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_271]
at org.flywaydb.core.internal.command.DbSchemas.lambda$create$0(DbSchemas.java:98) ~[flyway-core-8.0.5.jar:na]
at org.flywaydb.core.internal.jdbc.TransactionalExecutionTemplate.execute(TransactionalExecutionTemplate.java:55) ~[flyway-core-8.0.5.jar:na]
at org.flywaydb.core.internal.command.DbSchemas.create(DbSchemas.java:86) ~[flyway-core-8.0.5.jar:na]
at org.flywaydb.core.Flyway$1.execute(Flyway.java:161) ~[flyway-core-8.0.5.jar:na]
at org.flywaydb.core.Flyway$1.execute(Flyway.java:124) ~[flyway-core-8.0.5.jar:na]
at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:214) ~[flyway-core-8.0.5.jar:na]
at org.flywaydb.core.Flyway.migrate(Flyway.java:124) ~[flyway-core-8.0.5.jar:na]
at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
... 23 common frames omitted
解决问题:
这个问题的解决方法非常的简单,我到最后才发现这个问题是非常的简单,自己没有添加flyway-core
包,才会导致的出现这个问题,非常不应该的,在这个问题上浪费了2个多小时的时间。
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>8.4.2</version>
</dependency>
对flyway的配置如下:
# 数据库配置
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.datasource.url=jdbc:mariadb://localhost:3306/ltblog?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8?useMysqlMetadata=true
spring.datasource.username=longtao
spring.datasource.password=abcd1234
# flyway配置
# 开启flyway
spring.flyway.enabled=true
# flyway编码
spring.flyway.encoding=UTF-8
# flyway的sql的位置
spring.flyway.locations=classpath:db/migration
# flyway文件的前缀
spring.flyway.sql-migration-prefix=V
# flyway文件的分隔符
spring.flyway.sql-migration-separator=__
# flyway文件的后缀
spring.flyway.sql-migration-suffixes=.sql
# 是否在合并的时候验证
spring.flyway.validate-on-migrate=true
# 在合并时检查数据库中是否存在schemes,如果不存在,则在数据库中创建schema_verison表
spring.flyway.baseline-on-migrate=true
# flyway的基准版本
spring.flyway.baseline-version=0
在这些配置中有2个非常重要的点要注意:
- 因为该项目中使用的是mariadb,而不是mysql,所以需要在
DataSource.url
中加入useMysqlMetadata=true
- 在
flyway
的配置中,需要开启基准合并,当数据库中有schemes记录表时,继续在表中记录。当数据库不为空并且不存在schema的元数据表时,自动在数据库中创建schema_verison
表。spring.flyway.baseline-on-migrate=true
。该值默认为false。