Spring Boot 应用程序在启动时未运行 Flyway 迁移

     2023-03-14     228

关键词:

【中文标题】Spring Boot 应用程序在启动时未运行 Flyway 迁移【英文标题】:Spring Boot Application is not running Flyway migrations on startup 【发布时间】: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中 【参考方案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 解决了这个问题

【讨论】:

在tomcat 8上部署战争时未加载Spring Boot应用程序

】在tomcat8上部署战争时未加载SpringBoot应用程序【英文标题】:Springbootappnotloadingwhendeployingwarontomcat8【发布时间】:2017-12-1721:03:58【问题描述】:这是我的build.gradlepluginsid\'net.ltgt.apt\'version\'0.10\'id\'org.springframework.boot\'version\'1.5.4. 查看详情

在带有 spring-boot rest 和 Swagger 的标头中使用 utf-8 字符时未加载响应

...间】:2017-04-1608:14:04【问题描述】:我有招摇的弹簧启动应用程序。当我测试我的休息服务时,标题中没有utf-8字符,一切正常。我可以用来测试它的Swagger生 查看详情

在 Spring Boot 和 Oracle 中使用 STRUCTS 时未释放连接

...时间】:2017-09-1414:11:30【问题描述】:我有一个SpringBoot应用程序,我在其中托管了多个REST和SOAPWebServices。我客户的最新请求是执行一个存储过程,该过程接收多个参数和3个自定义 查看详情

如何在运行时更改日志级别而不重新启动 Spring Boot 应用程序

】如何在运行时更改日志级别而不重新启动SpringBoot应用程序【英文标题】:howdoIchangeloglevelinruntimewithoutrestartingspringbootapplication【发布时间】:2016-02-2323:56:06【问题描述】:我已经在PCF中部署了springboot应用程序。我想根据环境变... 查看详情

HTTP 状态 404 - 在 Spring Boot 中调用 url 时未找到

...00【问题描述】:Serviceresponseinbrowser在简单创建的SpringBoot应用程序的浏览器上调用URL时,我收到HTTP状态404。下面是代码。我的代码中没有错误或问题。我只是从互联网上复制过去 查看详情

启动时如何配置 Spring Boot 应用程序以在特定数据库上运行

】启动时如何配置SpringBoot应用程序以在特定数据库上运行【英文标题】:HowtoconfigureSpringBootApplicationtorunonaspecificdatabasewhenlaunching【发布时间】:2018-03-0100:50:30【问题描述】:我有一个使用SpringBoot开发的SpringMVC应用程序。顺便说... 查看详情

无法在 IntelliJ Idea 中启动 spring-boot 应用程序

】无法在IntelliJIdea中启动spring-boot应用程序【英文标题】:Unabletostartspring-bootapplicationinIntelliJIdea【发布时间】:2016-12-2802:52:27【问题描述】:概述我可以使用spring-boot的maven插件运行应用程序,但不能使用IDE运行它。以下几行详... 查看详情

保存对象时未填充 Spring Boot JPA@CreatedDate @LastModifiedDate

...题描述】:我正在使用Postgres数据库使用SpringBoot+JPA编写应用程序。我有一个用户实体,我试图在 查看详情

Flyway Spring Boot应用程序在启动时不应用插入脚本

】FlywaySpringBoot应用程序在启动时不应用插入脚本【英文标题】:Flywayspringbootappdoesn\'tapplyinsertscriptuponstarting【发布时间】:2020-05-1607:32:21【问题描述】:我有一个spring-bootJPA应用程序,我正在尝试与flyway集成。我的应用程序启动... 查看详情

错误:使用spring boot mongodb时未定义方法where(String)

...】:2019-04-1407:38:45【问题描述】:我已经在我的springboot应用程序中添加了mongodb依赖项,但是我在“where”方法上遇到了未定义的错误:ChangeStreamRe 查看详情

清单错误将 spring-boot 启动为 java 应用程序

】清单错误将spring-boot启动为java应用程序【英文标题】:Manifesterrorlanchingspring-bootasjavaapplication【发布时间】:2018-02-0700:00:06【问题描述】:我无法在我的Eclipse3.9.0RELEASE中运行我的SpringBoot应用程序。如果使用以下命令在Prompt上启... 查看详情

Spring Boot 应用程序能够在 Eclipse 环境中启动,但不能在带有快照的 Windows 命令行中运行

】SpringBoot应用程序能够在Eclipse环境中启动,但不能在带有快照的Windows命令行中运行【英文标题】:Springbootappablelaunchineclipseenvironmentbutnotwhenruninwindowscommandlinewithsnapshot【发布时间】:2020-02-0501:18:20【问题描述】:我目前正在尝... 查看详情

如何在不启动 HikariPool 关闭的情况下使用 Hikari 数据源运行 Spring Boot 应用程序

...不启动HikariPool关闭的情况下使用Hikari数据源运行SpringBoot应用程序【英文标题】:HowtorunspringbootappwithHikaridatasourcewithoutHikariPoolshutdowninitiated【发布时间】:2021-12-2109:10:26【问题描述】:我有一个带有maven的SpringBoot应用程序,并使... 查看详情

sec:authorize 在 Spring 启动应用程序中使用 Thymeleaf 时未按预期工作

】sec:authorize在Spring启动应用程序中使用Thymeleaf时未按预期工作【英文标题】:sec:authorizenotworkingasexpectedusingThymeleafinaSpringbootapplication【发布时间】:2018-12-1019:26:38【问题描述】:我正在尝试添加指向表中新行的链接,此链接应该... 查看详情

启动jar时出现spring boot错误404

...017-08-2616:04:51【问题描述】:我正在尝试运行一个SpringBoot应用程序。我从教程中下载了一个代码,所以我知道该代码有效。当我运行我的jar文件时,它看起来就像我在端口8080上运行的应用程序,但我仍然得到任何URL的404,我从... 查看详情

Spring Boot 未作为独立 jar 启动

...在命令提示符下运行该jar,但它抛出了以下错误,同一个应用程序正在eclipse中运行。我不能使用maven,也没有在我的应用程序中使用sp 查看详情

Spring boot - 在启动时禁用 Liquibase

...间】:2016-10-0901:19:07【问题描述】:我想用我的SpringBoot应用程序配置Liquibase,所以我将依赖项添加到pom.xml并将路径设置为application.properties中的master.xml。这工作正常,SpringBoot在启动时运行Liquib 查看详情

我需要我的 Spring Boot Web 应用程序在 JUnit 中重新启动

】我需要我的SpringBootWeb应用程序在JUnit中重新启动【英文标题】:IneedmySpringBootWebApplicationtorestartinJUnit【发布时间】:2016-01-1402:21:28【问题描述】:在不深入细节的情况下,我在一次运行我的Junit测试时遇到了问题。如果我逐个... 查看详情