以编程方式设置飞行路线占位符



我有一个非常简单的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 设置。

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

我这里有弹簧靴飞行路线集成的示例

最新更新