如何使用参数在触发器中设置架构?

时间:2014-04-03 12:48:50

标签: java spring postgresql triggers liquibase

这里我在Java / Spring中设置使用liquibase changeset的模式。 直到它与表/密钥创建一起工作。但它不适用于自定义SQL。

@Bean
@DependsOn("dataSource")
public SpringLiquibase liquibase() {
    SpringLiquibase liquibase = new SpringLiquibase();
    liquibase.setDataSource(dataSource);
    liquibase.setChangeLog("classpath:META-INF/liquibase/application-changes.xml");
    liquibase.setContexts("default"); 
    liquibase.setDefaultSchema(databaseProperties.getJdbcSchema());
    return liquibase;
}

现在我需要在Postgresql中管理大对象,所以我想添加以下变更集。但我遇到问题,因为它没有使用参数,所以我不得不在SQL文件中添加“my_schema”。有人知道解决方法或解决方案吗?

<changeSet id="xxx-0.0.1-SNAPSHOT-postgres" runInTransaction="true" failOnError="true"
    onValidationFail="HALT" dbms="postgresql">
    <sql >
        CREATE TRIGGER t_table BEFORE UPDATE OR DELETE ON my_schema.table
        FOR EACH ROW EXECUTE PROCEDURE my_schema.lo_manage(content);
    </sql>
</changeSet>

1 个答案:

答案 0 :(得分:2)

正如@pozs指出的那样,这里有一个解决方案: https://forum.liquibase.org/topic/setting-default-schema-working-for-xml-refactorings-but-not-sql

所以使这项工作的代码是

@Bean
@DependsOn("dataSource")
public SpringLiquibase liquibase() {
  SpringLiquibase liquibase = new SpringLiquibase();
  liquibase.setDataSource(dataSource);
  liquibase.setChangeLog("classpath:META-INF/liquibase/application-changes.xml");
  liquibase.setContexts("default"); 
  liquibase.setDefaultSchema(databaseProperties.getJdbcSchema());
  liquibase.setChangeLogParameters(createChangeLogParameters());
  return liquibase;

}

private Map<String, String> createChangeLogParameters() {
    Map<String, String> map = new HashMap<String, String>();
    map.put("defaultSchema", databaseProperties.getJdbcSchema()); // needed on SQL in <sql> tags.
    return map;
}

这里是Liquibase:

<changeSet id="xxx-0.0.1-SNAPSHOT-postgres" runInTransaction="true" failOnError="true"
onValidationFail="HALT" dbms="postgresql">
<sql >
    CREATE TRIGGER t_table BEFORE UPDATE OR DELETE ON ${defaultSchema}.table
    FOR EACH ROW EXECUTE PROCEDURE ${defaultSchema}.lo_manage(content);
</sql>