我有一个非常简单的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}
属性。迁移失败的原因是,.NET系统在处理配置文件之前就开始迁移了。
No value provided for placeholder expressions: ${tablespace_data}
我想在处理配置文件之前就开始迁移。如何解决这个问题?我不想使用application.properties来设置flyway占位符,但其他所有属性如 spring.flyway.user
, spring.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")
}
}
我有弹簧靴flyway集成的样品 此处