flyway如何在SQL文件中插入查询中使用占位符

时间:2016-03-31 04:10:19

标签: java spring-boot flyway property-placeholder

Application.properties

TM_ESCALATION_QUALIFICATION=SHIFT_LEADER

Application.java

@Value( "${TM_ESCALATION_QUALIFICATION}" )
String escalationQualification;

@Bean
InitializingBean printConfigurations(DataSource datasource) {
    return () -> {
            Flyway flyway = new Flyway();
            flyway.setDataSource(datasource);
            flyway.getPlaceholders().put( "ESCALATION_QUALIFICATION", escalationQualification );    
            flyway.migrate();
    };
}

SQL文件

insert into tm_qualification (ID, NAME, DELETABLE) values (sys_guid(), ${ESCALATION_QUALIFICATION}, 0); 

当我使用带有默认扩展名的文件时,上面工作正常,但当我将文件设为 .sql 扩展名时,会出现以下错误。

错误

org.flywaydb.core.api.FlywayException: No value provided for placeholder expressions: ${ESCALATION_QUALIFICATION}.  Check your configuration!
    at org.flywaydb.core.internal.util.PlaceholderReplacer.checkForUnmatchedPlaceholderExpression(PlaceholderReplacer.java:101)
    at org.flywaydb.core.internal.util.PlaceholderReplacer.replacePlaceholders(PlaceholderReplacer.java:78)
    at org.flywaydb.core.internal.dbsupport.SqlScript.<init>(SqlScript.java:79)....

1 个答案:

答案 0 :(得分:1)

从网上建议使用

flyway.placeholderReplacement =假

仅在升级到&gt;后才能使用flyway 4.0.3(spring boot最新使用ooold版本)

另一件有用的事情就是使用伪造的占位符前缀,这就是诀窍......

flyway.placeholderPrefix = $$$ - bogus - $$$