【问题标题】:Flyway setting placeholders programmaticallyFlyway 以编程方式设置占位符
【发布时间】:2019-02-15 17:33:26
【问题描述】:

我有一个非常简单的 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 来设置 flyway 占位符,但所有其他属性(如 spring.flyway.userspring.flyway.password 等)都希望由 application.properties 设置。

【问题讨论】:

    标签: spring spring-boot flyway


    【解决方案1】:

    您可以像这样使用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")
        }
    }
    

    我有 Spring Boot Flyway 集成的示例here

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 2016-08-27
      • 2013-05-29
      • 2023-03-16
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-05-16
      • 2015-11-13
      相关资源
      最近更新 更多