【问题标题】:Spring Boot Application is not running Flyway migrations on startupSpring Boot 应用程序在启动时未运行 Flyway 迁移
【发布时间】:2020-08-20 12:02:39
【问题描述】:

我有一个带有几个实体类的 Spring Boot 应用程序,我正在尝试使用 flyway 实现数据库迁移。 似乎在启动时,Spring Boot 根本没有运行 flyway。

这是我的 application.properties

spring.datasource.url= jdbc:postgresql://localhost:5555/mfidb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=validate

spring.flyway.enabled=true

这是我的 build.gradle 中与 flyway 有关的行

plugins {
    id 'org.springframework.boot' version '2.2.6.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
    id "org.flywaydb.flyway" version "6.4.1"
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'    
    implementation 'org.flywaydb:flyway-core'
    runtime('org.postgresql:postgresql:42.2.12')
}

flyway {
    url = 'jdbc:postgresql://localhost:5555/mfidb'
    user = 'postgres'
    password = 'postgres'
}

我可以通过在终端中输入 gradle flywayMigrate -i 来运行我的迁移。

但我希望迁移在启动时运行。

任何帮助将不胜感激,在此先感谢。

编辑:

这是运行应用程序时的控制台输出

2020-05-05 11:55:59.022  INFO 50754 --- [           main] com.ubm.mfi.MfiApplication               : Starting MfiApplication on MacBook-Pro.local with PID 50754 (~/Downloads/mfi 5/build/classes/java/main started by will in ~/Downloads/mfi 5)
2020-05-05 11:55:59.024  INFO 50754 --- [           main] com.ubm.mfi.MfiApplication               : No active profile set, falling back to default profiles: default
2020-05-05 11:55:59.457  INFO 50754 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-05-05 11:55:59.495  INFO 50754 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 33ms. Found 2 JPA repository interfaces.
2020-05-05 11:55:59.778  INFO 50754 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-05 11:55:59.783  INFO 50754 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-05 11:55:59.784  INFO 50754 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.33]
2020-05-05 11:55:59.838  INFO 50754 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-05 11:55:59.838  INFO 50754 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 768 ms
2020-05-05 11:55:59.917  INFO 50754 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-05-05 11:55:59.944  INFO 50754 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.4.12.Final
2020-05-05 11:55:59.991  INFO 50754 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-05-05 11:56:00.051  INFO 50754 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-05-05 11:56:00.114  INFO 50754 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-05-05 11:56:00.124  INFO 50754 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL10Dialect
2020-05-05 11:56:00.545  INFO 50754 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2020-05-05 11:56:00.548  INFO 50754 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2020-05-05 11:56:00.818  WARN 50754 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2020-05-05 11:56:00.891  INFO 50754 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-05 11:56:01.005  INFO 50754 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-05 11:56:01.007  INFO 50754 --- [           main] com.ubm.mfi.MfiApplication               : Started MfiApplication in 2.255 seconds (JVM running for 2.983)

这是数据迁移的路径

【问题讨论】:

  • 您能否还显示迁移脚本在项目结构中的保存位置以及您在控制台中获得的输出是什么?
  • 添加了启动时的运行输出和迁移路径的图像
  • 你可以尝试在属性文件中添加 spring.flyway.locations=classpath:/db/migration
  • 没有修复它@JiteshShivnani
  • 虽然@JiteshShivnani,我确实打开了登录并得到了这个错误FlywayAutoConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.flywaydb.core.Flyway' (OnClassCondition),但它在我的build.gradle中

标签: java spring-boot spring-data-jpa database-migration flyway


【解决方案1】:

添加logging.level.root=debug您的application.properties文件以在启动应用程序期间查看详细信息。

也只需将flyway用户和密码添加到application.properties

spring.flyway.url = 'jdbc:postgresql://localhost:5555/mfidb'
spring.flyway.password=postgres
spring.flyway.user=postgres

如果你不需要从 gradle 运行 flyway,你可以从 build.gradle 中删除 flyway 配置

【讨论】:

  • 这并没有解决它,但我打开了登录,我得到了这个FlywayAutoConfiguration: Did not match: - @ConditionalOnClass did not find required class 'org.flywaydb.core.Flyway' (OnClassCondition) 但它就在那里:implementation 'org.flywaydb:flyway-core'
  • 尝试干净并构建
  • 它仍然没有看到它。我还需要做些什么才能将它添加到类路径或类似的东西吗?
  • 我猜我的依赖项在进行 gradle clean build 时没有刷新,所以它从来没有拉掉 flyway 依赖项。我不得不手动告诉它刷新依赖项并修复它。感谢您的日志记录级别提示,这帮助我找到了解决方案。
  • 在文档中不清楚 spring.flyway.url 属性接受什么样的 URL,我发现它只能使用用户名和密码的单独属性/值(而不是部分网址)
【解决方案2】:

您的 application.properties 对我来说很完美。

我也在使用 Flyway,它就像一个魅力,在我的应用程序启动时执行迁移。我的build.gradle 中与flyway 相关的唯一条目是依赖项

runtime("org.flywaydb:flyway-core")

我认为您应该从build.gradle 中删除所有其他与飞行路线相关的条目。

【讨论】:

  • 在 Kotlin DSL 中是: implementation("org.flywaydb:flyway-core:{version}")
【解决方案3】:

当我进行 gradle clean build 时,我的依赖项没有得到刷新,因此 flyway 源永远不存在。我不得不在我的 IDE 中手动刷新我的依赖项,它才开始工作。当您花费数小时在如此简单的事情上时,您会感到难过。 Giga 更改日志记录级别的评论帮助我找到了问题。谢谢

【讨论】:

    【解决方案4】:

    就我而言,这就是发生的事情:

    我花了将近 4 个小时来解决这个问题,我的假设是 Spring JPA 启动器带来了 flyway 核心依赖项。我没有明确添加 flyway-core 的依赖项。因此,如果我们不这样做,spring boot flyway 自动配置不会给出任何错误,而只是简单地忽略迁移设置。

    通过跟踪自动配置我发现了这一点,如果没有手动添加依赖项,自动配置会忽略飞行路径设置,而不是使用像 @ConditionalOnMissingBean({Flyway.class}) 这样的条件注释抛出异常

    【讨论】:

    • 您是否将其添加到 Application.java 中?
    • 我没有在 Application.java 中添加任何内容,只是将 flyway-core 依赖项添加到我的 pom.xml 中。 spring jpa starter 没有带来这种依赖,我认为是这样。
    【解决方案5】:

    检查您的脚本是否已经运行。

    诀窍是,一旦 Flyway 成功运行更新脚本,它就会创建表 flyway_schema_history,记录我已经成功运行过一次创建脚本。

    当我第二次尝试运行脚本时,它被拒绝了,但由于在 spring.jpa.hibernate 中运行时应用程序关闭时,最初创建的表(在我的例子中称为“自行车”)被删除了。 ddl-auto=create-drop 模式之前,我得到了

    SchemaManagementException: Schema-validation: missing table [bike] 异常。

    删除flyway_schema_history表或删除与之前脚本运行和滚动对应的记录!

    【讨论】:

      【解决方案6】:

      在手动检查 SQL 之后,这对我有用(使用 Spring Boot + Cloud):

      我的 build.gradle 中的重要部分:

        implementation  'org.springframework.boot:spring-boot-starter-data-jpa'
        implementation  'org.flywaydb:flyway-core'
        implementation  'org.hibernate:hibernate-core'
        implementation  'org.hibernate.validator:hibernate-validator:7.0.1.Final'
      

      没有 gradle 插件。而在application.yml/application-local.yml中:

      spring:
        flyway:
          baseline-on-migrate: true
      
        jpa:
          database: POSTGRESQL
          show-sql: true
          hibernate:
            ddl-auto: validate
          open-in-view: true
      
        datasource:
          platform: postgres
          url: jdbc:postgresql://localhost:5432/your_db
          username: your_db_user
          password: your_db_passwd
          driverClassName: org.postgresql.Driver
      

      请注意,您不需要数据库用户/url 的 flyway 特定配置。 Spring Boot 在类路径上检测到 Flyway(或 Liquibase,如果您正在使用...)然后它使用主数据源。请参阅:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/howto.html#howto.data-initialization。然后最后一个技巧是制作你的第一个脚本 V2__init.sql 。这一点很关键,因为 baseline-on-migrate: true 位创建了 flyway_schema_history 表,并且它的动作是第一件事,所以版本 1 或 V1_ 即第一行。 ..所以如果它是 V1__init.sql 或其他什么,您的脚本将不会运行。它会认为它已经运行了。它需要是下一个,V2__init.sql 或其他。

      【讨论】:

        【解决方案7】:

        我正在使用 maven,并通过运行 mvn clean install 解决了这个问题

        【讨论】:

          猜你喜欢
          • 2020-02-23
          • 2020-04-20
          • 1970-01-01
          • 2018-09-07
          • 2020-08-20
          • 2021-04-20
          • 2016-09-13
          • 2020-05-16
          • 2018-12-19
          相关资源
          最近更新 更多