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表的问题的工具。它有两个主要用途:

  1. 删除失败的迁移条目
  2. 将应用的迁移的校验和,描述和类型与可用的迁移重新对齐

[

](https://blog.csdn.net/MGL_1/article/details/88426618)

fly的常用配置

  1. flyway.baseline-description对执行迁移时基准版本的描述.
  2. flyway.baseline-on-migrate当迁移时发现目标schema非空,而且带有没有元数据的表时,
  3. 是否自动执行基准迁移,默认false.
  4. flyway.baseline-version开始执行基准迁移时对现有的schema的版本打标签,默认值为1.
  5. flyway.check-location检查迁移脚本的位置是否存在,默认false.
  6. flyway.clean-on-validation-error当发现校验错误时是否自动调用clean,默认false.
  7. flyway.enabled是否开启flywary,默认true.
  8. flyway.encoding设置迁移时的编码,默认UTF-8.
  9. flyway.ignore-failed-future-migration当读取元数据表时是否忽略错误的迁移,默认false.
  10. flyway.init-sqls当初始化好连接时要执行的SQL.
  11. flyway.locations迁移脚本的位置,默认db/migration.
  12. flyway.out-of-order是否允许无序的迁移,默认false.
  13. flyway.password目标数据库的密码.
  14. flyway.placeholder-prefix设置每个placeholder的前缀,默认${.
  15. flyway.placeholder-replacementplaceholders是否要被替换,默认true.
  16. flyway.placeholder-suffix设置每个placeholder的后缀,默认}.
  17. flyway.placeholders.[placeholder name]设置placeholdervalue
  18. flyway.schemas设定需要flywary迁移的schema,大小写敏感,默认为连接默认的schema.
  19. flyway.sql-migration-prefix迁移文件的前缀,默认为V.
  20. flyway.sql-migration-separator迁移脚本的文件名分隔符,默认__
  21. flyway.sql-migration-suffix迁移脚本的后缀,默认为.sql
  22. flyway.tableflyway使用的元数据表名,默认为schema_version
  23. flyway.target迁移时使用的目标版本,默认为latest version
  24. flyway.url迁移时使用的JDBC URL,如果没有指定的话,将使用配置的主数据源
  25. flyway.user迁移数据库的用户名
  26. flyway.validate-on-migrate迁移时是否校验,默认为true.

问题

1、flyway-core不支持mariadb,会错误的显示为不支持mysql5.5 版本

改正方法:

  1. 因为使用的数据库为mariadb,所以需要将工程中得mysql驱动包换为mariadb驱动包,并且使用flyway-mysql代替flyway-core

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.mariadb.jdbc</groupId>
    4. <artifactId>mariadb-java-client</artifactId>
    5. <version>2.7.4</version>
    6. </dependency>
    7. <dependency>
    8. <groupId>org.flywaydb</groupId>
    9. <artifactId>flyway-mysql</artifactId>
    10. <version>8.2.3</version>
    11. </dependency>
    12. </dependencies>
  2. 配置置application.properties中得数据库驱动

    1. # 设置数据库的配置
    2. spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
    3. spring.datasource.url=jdbc:mariadb://localhost:3306/ltblog?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
    4. spring.datasource.username=longtao
    5. spring.datasource.password=abcd1234

2、出现Error creating bean with name ‘flywayInitializer’ defined in class path resource…错误

问题描述:

出现以下错误:

  1. 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;
  2. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1804) ~[spring-beans-5.3.15.jar:5.3.15]
  3. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) ~[spring-beans-5.3.15.jar:5.3.15]
  4. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542) ~[spring-beans-5.3.15.jar:5.3.15]
  5. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.15.jar:5.3.15]
  6. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.15.jar:5.3.15]
  7. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.15.jar:5.3.15]
  8. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]
  9. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.15.jar:5.3.15]
  10. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.15.jar:5.3.15]
  11. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) ~[spring-beans-5.3.15.jar:5.3.15]
  12. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) ~[spring-context-5.3.15.jar:5.3.15]
  13. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) ~[spring-context-5.3.15.jar:5.3.15]
  14. at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.3.jar:2.6.3]
  15. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:732) [spring-boot-2.6.3.jar:2.6.3]
  16. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:414) [spring-boot-2.6.3.jar:2.6.3]
  17. at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.3.jar:2.6.3]
  18. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.6.3.jar:2.6.3]
  19. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.6.3.jar:2.6.3]
  20. at com.liulongtao.ltblogs.LtBlogApplication.main(LtBlogApplication.java:10) [classes/:na]
  21. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_271]
  22. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_271]
  23. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_271]
  24. at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_271]
  25. at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.6.3.jar:2.6.3]
  26. Caused by: java.lang.AbstractMethodError: org.flywaydb.core.internal.database.base.Database.doQuote(Ljava/lang/String;)Ljava/lang/String;
  27. at org.flywaydb.core.internal.database.base.Database.quote(Database.java:225) ~[flyway-core-8.0.5.jar:na]
  28. at org.flywaydb.core.internal.database.base.Schema.toString(Schema.java:217) ~[flyway-core-8.0.5.jar:na]
  29. at java.lang.String.valueOf(String.java:2994) ~[na:1.8.0_271]
  30. at java.lang.StringBuilder.append(StringBuilder.java:131) ~[na:1.8.0_271]
  31. at org.flywaydb.core.internal.command.DbSchemas.lambda$create$0(DbSchemas.java:98) ~[flyway-core-8.0.5.jar:na]
  32. at org.flywaydb.core.internal.jdbc.TransactionalExecutionTemplate.execute(TransactionalExecutionTemplate.java:55) ~[flyway-core-8.0.5.jar:na]
  33. at org.flywaydb.core.internal.command.DbSchemas.create(DbSchemas.java:86) ~[flyway-core-8.0.5.jar:na]
  34. at org.flywaydb.core.Flyway$1.execute(Flyway.java:161) ~[flyway-core-8.0.5.jar:na]
  35. at org.flywaydb.core.Flyway$1.execute(Flyway.java:124) ~[flyway-core-8.0.5.jar:na]
  36. at org.flywaydb.core.FlywayExecutor.execute(FlywayExecutor.java:214) ~[flyway-core-8.0.5.jar:na]
  37. at org.flywaydb.core.Flyway.migrate(Flyway.java:124) ~[flyway-core-8.0.5.jar:na]
  38. at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66) ~[spring-boot-autoconfigure-2.6.3.jar:2.6.3]
  39. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863) ~[spring-beans-5.3.15.jar:5.3.15]
  40. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) ~[spring-beans-5.3.15.jar:5.3.15]
  41. ... 23 common frames omitted

解决问题:

这个问题的解决方法非常的简单,我到最后才发现这个问题是非常的简单,自己没有添加flyway-core包,才会导致的出现这个问题,非常不应该的,在这个问题上浪费了2个多小时的时间。

  1. <dependency>
  2. <groupId>org.flywaydb</groupId>
  3. <artifactId>flyway-core</artifactId>
  4. <version>8.4.2</version>
  5. </dependency>

对flyway的配置如下:

  1. # 数据库配置
  2. spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
  3. spring.datasource.url=jdbc:mariadb://localhost:3306/ltblog?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8?useMysqlMetadata=true
  4. spring.datasource.username=longtao
  5. spring.datasource.password=abcd1234
  6. # flyway配置
  7. # 开启flyway
  8. spring.flyway.enabled=true
  9. # flyway编码
  10. spring.flyway.encoding=UTF-8
  11. # flyway的sql的位置
  12. spring.flyway.locations=classpath:db/migration
  13. # flyway文件的前缀
  14. spring.flyway.sql-migration-prefix=V
  15. # flyway文件的分隔符
  16. spring.flyway.sql-migration-separator=__
  17. # flyway文件的后缀
  18. spring.flyway.sql-migration-suffixes=.sql
  19. # 是否在合并的时候验证
  20. spring.flyway.validate-on-migrate=true
  21. # 在合并时检查数据库中是否存在schemes,如果不存在,则在数据库中创建schema_verison表
  22. spring.flyway.baseline-on-migrate=true
  23. # flyway的基准版本
  24. spring.flyway.baseline-version=0

在这些配置中有2个非常重要的点要注意:

  1. 因为该项目中使用的是mariadb,而不是mysql,所以需要在DataSource.url中加入useMysqlMetadata=true
  2. flyway的配置中,需要开启基准合并,当数据库中有schemes记录表时,继续在表中记录。当数据库不为空并且不存在schema的元数据表时,自动在数据库中创建schema_verison表。spring.flyway.baseline-on-migrate=true。该值默认为false。