Flyway 以编程方式设置占位符

Chr*_*ina 5 spring flyway spring-boot

我有一个非常简单的 Spring Boot 应用程序,它使用 Flyway 进行数据库迁移。我想在迁移开始之前使用 Spring 配置类以编程方式设置 Flyway 占位符。

我要做的是:

@Configuration
public class FlywayConfiguration {

  @Autowired
  private Flyway flyway;

  @Value("${threedsserver.db.tableSpaces.data:pg_default}")
  private String tablespaceData;

  @Value("${threedsserver.db.tableSpaces.index:pg_default}")
  private String tablespaceIndex;

  @Value("${threedsserver.db.tableSpaces.lob:pg_default}")
  private String tablespaceLob;

  @PostConstruct
  void setFlywayPlaceholders() {
    Map<String, String> placeholders = flyway.getPlaceholders();
    placeholders.put("tablespace_data", tablespaceData);
    placeholders.put("tablespace_index", tablespaceIndex);
    placeholders.put("tablespace_lob", tablespaceLob);
    flyway.setPlaceholders(placeholders);
  }
}
Run Code Online (Sandbox Code Playgroud)

然后在我的迁移脚本中我使用${tablespace_data}属性。迁移失败:

No value provided for placeholder expressions: ${tablespace_data}
Run Code Online (Sandbox Code Playgroud)

我想迁移在处理配置文件之前就开始了。如何解决这个问题?我不想使用 application.properties 来设置飞行路线占位符,但所有其他属性,如spring.flyway.user、等都spring.flyway.password希望由 application.properties 设置。

小智 0

FlywayConfigurationCustomizer你可以这样使用:

import org.flywaydb.core.api.configuration.FluentConfiguration
import org.springframework.boot.autoconfigure.flyway.FlywayConfigurationCustomizer
import org.springframework.context.annotation.Configuration

@Configuration
class CustomFlywayConfiguration : FlywayConfigurationCustomizer {
    override fun customize(configuration: FluentConfiguration?) {
        configuration?.placeholders?.put("tablespace_index", "some_value")
    }
}
Run Code Online (Sandbox Code Playgroud)

我这里有 Spring Boot Flyway 集成的示例