每次我在 WAR 和 IDE 运行 之间切换 运行 模式时,带 spring 引导的 Flyway 都会覆盖整个数据库

Flyway with spring boot overwrites whole DB every time I switch run mode between WAR and IDE run

我在将 flyway 数据库迁移与 spring 启动应用程序集成时遇到了非常奇怪的问题。

当我使用命令行从可执行文件WAR 运行 应用程序时,它会在应用程序启动时创建新的数据库。 现在,如果我将应用程序 运行 模式切换为 IDE(即来自 STS 的 运行),它会再次触发我的 db/migration 文件夹中的所有脚本。每次在这 2 个 运行 模式之间切换时,我都可以看到 installed_on 列时间发生变化。我试过启用 baselineOnMigrate 属性,但没有得到任何效果。 你认为它与 spring boot embedded tomcat 有关吗?因为在 运行 它创建了嵌入的个体 tomcat。

请在下面找到我的 spring 引导 application.properties:

mssql.dbname=issueDB
mssql.password=password
mssql.dbserver=localhost
mssql.port=1501

spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://${mssql.dbserver}:${mssql.port};databaseName=${mssql.dbname}
spring.datasource.username=user
spring.datasource.password=${mssql.password}

spring.flyway.baselineOnMigrate=true
spring.flyway.locations=classpath:db/migration/testissue
spring.flyway.out-of-order=true
spring.flyway.baseline-version=1.3
spring.flyway.placeholder-prefix=$
spring.flyway.placeholder-suffix=$
spring.flyway.mixed=true
spring.flyway.cleanOnValidationError=true

我想,可能是这个属性 spring.flyway.cleanOnValidationError=true造成的。根据 docs:

Whether to automatically call clean or not when a validation error occurs.

This is exclusively intended as a convenience for development. Even tough we strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that the next migration will bring you back to the state checked into SCM.

如果您运行您的应用程序以不同的方式在同一个数据库和 flyway 上运行,可能是您遇到了一些验证问题,只需清理您的数据库并用当前脚本状态覆盖它。