Flyway以编程方式设置占位符

时间:2018-09-11 08:45:34

标签: spring spring-boot flyway

我有一个非常简单的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);
  }
}

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

No value provided for placeholder expressions: ${tablespace_data}

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

1 个答案:

答案 0 :(得分: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")
    }
}

我有一些用于弹簧引导架集成here

的示例
相关问题