Flyway db 首先为从属 Jar 迁移 - Spring 启动

Flyway db migrate first for Dependent Jar - Spring boot

我有一个构建为 jar 的 spring 引导基础项目。这个 jar base-0.0.1-SNAPSHOT.jar 文件在 db/migration/*.sql

中有 flyway 迁移脚本

base-0.0.1-SNAPSHOT.jar 作为依赖项添加到 impl-0.0.1-SNAPSHOT-boot.jar 中。同样,这个 impl 引导 jar 正在 db/migration/*.sql.

中进行 flyway 迁移

base jar 的 flyway 迁移创建了 table 并且 impl boot jar 改变了由 base jar 创建的 table。

在这种情况下,我需要先 运行 base jar 的 flyway 脚本,然后必须遵循 impl boot jar,

基本 jar 中的迁移脚本

db/migration/v1__create.sql,
db/migration/v2__create.sql

impl jar 中的迁移脚本

db/migration/v3__create.sql

mvn clean install impl jar 时,我收到此错误

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 org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException: Migration V3__create.sql failed ------------------------------- SQL State : 42S02 Error Code : 42102 Message : Table "BASE_TABLE" not found; SQL statement:

我已经在base jar包中添加了迁移脚本,但是base jar的flyway脚本仍然没有执行。

impl boot jar mvn build时如何先执行base package flyway脚本然后impl boot jar's next?

更新 1:

基础 jar 被以下插件打包为另一个 spring 启动应用程序的一部分,

       <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <executions>
              <execution>
                  <goals>
                      <goal>jar</goal>
                  </goals>
                  <phase>package</phase>
                  <configuration>
                      <classifier>com</classifier>
                      <includes>
                          <include>**/entities/*</include>
                          <include>**/services/*</include>
                      
<include>${basedir}/src/main/resources/db/migration/*</include>
                      </includes>
                  </configuration>
              </execution>
          </executions>           </plugin>

impl jar的pom.xml有

  <dependency>            <groupId>com.group</groupId>
      <artifactId>base</artifactId>           <version>0.0.1-SNAPSHOT</version>
      <classifier>com</classifier>        </dependency>

在这两个 poms 中都没有提到与 flyway 迁移配置相关的内容。默认情况下,它使用默认的飞路配置。

我假设“基础”项目 jar 工件本身不是 spring 引导应用程序,我的意思是它不是用 spring-boot-maven-plugin 创建的,因为否则它不能作为依赖项包含在 impl-boot 模块中(因为 spring 引导应用程序并不是真正的 jar - 所以它不起作用)

基于该假设,flyway 会扫描您的 jars 和“类”文件夹,并纯粹在运行时查找迁移。这意味着从 base 和 impl 迁移应该有任何区别——只要它们在预定义位置的类路径中。 如果找不到 base 的迁移,则可以进行以下操作(出于我的想法,我可能会遗漏一些东西,但希望它能为调查提供一些方向):

  1. Base jar 未正确打包,打开 Winrar/Winzip 中的工件并确保迁移确实符合要求的布局。
  2. jar 未正确打包到 spring 启动应用程序中。应用程序中的依赖 Jar 通常位于 BOOT-INF/lib 文件夹中,因此请确保它确实出现在那里。
  3. Flyway 迁移位置混乱。 Spring boot 可以包含 application.properties / application.yml 中的各种 flyway 定义,参见 here,尤其是 属性 spring.flyway.locations.
  4. 可能是数据库的schema搞砸了,一般flyway migrations不应该包含schema名称,但这通常取决于很多其他因素,所以我只是作为一个大方向提到它
  5. 最后但同样重要的是,您可以在实际执行迁移的代码中放置一个断点:请参阅 here 并查看通过调试解决了哪些迁移问题