Flyway数据库迁移首次用于依赖的Jar – Spring Boot

huangapple go评论67阅读模式
英文:

Flyway db migrate first for Dependent Jar - Spring boot

问题

我有一个基于Spring Boot构建的基础项目,构建为一个jar包。这个名为base-0.0.1-SNAPSHOT.jar的文件中包含了Flyway迁移脚本,位于db/migration/*.sql

这个base-0.0.1-SNAPSHOT.jar被添加为impl-0.0.1-SNAPSHOT-boot.jar的依赖。同样,这个impl boot jar也包含了位于db/migration/*.sql的Flyway迁移脚本。

基础jar的Flyway迁移脚本创建了表,而impl boot jar则修改了基础jar创建的同一张表。

在这种情况下,我需要先运行基础jar的Flyway脚本,然后再运行impl boot jar的脚本,

基础jar中的迁移脚本

  • db/migration/v1__create.sql
  • db/migration/v2__create.sql

impl jar中的迁移脚本

  • db/migration/v3__create.sql

在执行impl jar的mvn clean install时,我遇到了以下错误:

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:

我已经在基础jar包中添加了迁移脚本,但基础jar的Flyway脚本仍未被执行。

在impl jar的pom.xml中,我已经添加了基础jar的依赖:

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

在这两个pom文件中都没有涉及到Flyway迁移配置。默认情况下,它使用默认的Flyway配置。

英文:

I have a spring boot base project built as a jar. This jar base-0.0.1-SNAPSHOT.jar file has flyway migration scripts in db/migration/*.sql

This base-0.0.1-SNAPSHOT.jar is added as dependency in impl-0.0.1-SNAPSHOT-boot.jar . Again, this impl boot jar is having flyway migration in db/migration/*.sql.

The base jar's flyway migration creates the table and the impl boot jar alters the same table created by base jar.

In this scenario I need base jar's flyway scripts to be run first and then impl boot jar has to be followed,

Migration scripts in base jar

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

Migration scripts in impl jar

> db/migration/v3__create.sql

While mvn clean install of impl jar, I am getting this error

> 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:

I have added migration scripts in the base jar package, but still the base jar's flyway scripts are not executed.

How to execute the base package flyway script first and then impl boot jar's next during mvn build of impl boot jar.?

Update 1:

The base jar is packaged as part of another spring boot application by below plugin,

> <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>

The pom.xml of impl jar has

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

In Both the poms there is nothing related to flyway migration configuration mentioned. By default it used default flyway configuration.

答案1

得分: 1

我假设 "base" 项目的 jar 构件本身并不是一个独立的 Spring Boot 应用程序,我的意思是它并不是使用 spring-boot-maven-plugin 创建的,因为如果是这样,它就不能作为 "impl-boot" 模块的依赖项包含进去(因为 Spring Boot 应用程序实际上不是一个 jar - 所以无法工作)。

基于这个假设,Flyway 会扫描你的 jar 包和 "classes" 文件夹,并在运行时纯粹地查找迁移。这意味着迁移从 "base" 和 "impl" 中获取的话不应该有任何区别 - 只要它们在预定义的位置在类路径中。
如果没有找到 "base" 的迁移,那么以下情况是可能的(仅凭我的想法,我可能遗漏了一些内容,但希望它能为调查提供一些方向):

  1. "base" jar 包没有正确打包,用 Winrar/Winzip 打开构件,确保迁移实际上符合所需的布局。
  2. 这个 jar 包没有正确地打包到 Spring Boot 应用程序中。应用程序中的依赖 jar 通常位于 BOOT-INF/lib 文件夹中,确保它真的在那里出现。
  3. Flyway 迁移位置可能混乱。Spring Boot 可以在 application.properties / application.yml 中包含各种 flyway 定义,参见这里,特别是属性 spring.flyway.locations
  4. 可能数据库的模式出现了问题,通常 flyway 迁移不应该包含模式名称,但这通常取决于许多其他因素,所以我只是一般性地提一下。
  5. 最后但并非最不重要的,你可以在实际执行迁移的代码中设置断点:参见这里,并通过调试查看已解析的迁移情况。
英文:

I assume that the "base" project jar artifact itself is not a spring boot application by itself, I mean it is not created with spring-boot-maven-plugin, because if otherwise it can't be included as a dependency in the impl-boot module (because spring boot application is not really a jar - so it doesn't work)

Based on that assumption, flyway scans your jars and "classes" folder and looks for migrations purely in runtime. This means that there should be any difference between migrations from base and impl - as longs as they're in the classpath in the predefined places.
If base's migrations are not found, then the following is possible (out of my head, I might be missing something but hopefully it will provide some direction for investigation ):

  1. Base jar is not packaged properly, open up the artifact in Winrar/Winzip and make sure that the migrations are indeed in accordance to the required layout.
  2. The jar is not packaged properly into the spring boot application. Dependent Jars in the application are usually found in BOOT-INF/lib folder so make sure it really appears there.
  3. Flyway migration locations are messed up. Spring boot can contain various flyway definitions in application.properties / application.yml, see here, especially property spring.flyway.locations.
  4. Maybe the schemas of the database are messed up, in general flyway migrations should not contain the schema name, but this generally depends on many other factor so I mention it only as a general direction
  5. Last but not the least, you can place a breakpoint in the code that actually executes the migrations: See here and see what migrations have been resolved by debugging

huangapple
  • 本文由 发表于 2020年9月14日 11:40:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63877787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定