gpt4 book ai didi

java - Liquibase - createView 语句中的自定义参数

转载 作者:行者123 更新时间:2023-12-02 10:35:14 29 4
gpt4 key购买 nike

我已将 liquibase 配置为在 postgresql 中使用架构的 Multi-Tenancy 。当变更日志仅创建仅依赖于 xml 语法的表或结构时,它工作正常。但是,我需要创建一些依赖于 defaultSchemaName 参数的 View ,因此我尝试了这种方式,但没有成功:

<changeSet id="00000000000002_blog_views" author="hudson" dbms="postgresql">
<createView viewName="blog_post_management_view" replaceIfExists="true">
SELECT
p.id,
p.title,
ba.name as author,
p.status,
p.created_date
FROM ${defaultSchemaName}.blog_post p
INNER JOIN ${defaultSchemaName}.blog_author ba ON p.author_id = ba.id
</createView>
</changeSet>

执行上述变更日志时出现此错误:

Caused by: liquibase.exception.DatabaseException: ERROR: syntax error at or near "$"
Position: 258 [Failed SQL: CREATE VIEW "c7a53124-0ca9-4ce4-a25d-34b1764ee9df".blog_post_management_view AS SELECT
p.id,
p.title,
ba.name as author,
p.status,
p.created_date
FROM ${defaultSchemaName}.blog_post p
INNER JOIN ${defaultSchemaName}.blog_author ba ON p.author_id = ba.id]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:309)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:55)
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:113)
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1277)
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1259)
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:582)
... 29 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near "$"

重要的是要注意我的 liquibase 运行程序已经配置了 defaultSchemaName:

@Bean
@DependsOn("liquibase")
public MultiTenantSpringLiquibase multiTenantLiquibase(DataSource dataSource, LiquibaseProperties liquibaseProperties) {
MultiTenantSpringLiquibase liquibase = new CustomMultiTenantSpringLiquibase();
liquibase.setDataSource(dataSource);
liquibase.setChangeLog("classpath:config/liquibase/master.xml");
liquibase.setContexts(liquibaseProperties.getContexts());
liquibase.setDefaultSchema(liquibaseProperties.getDefaultSchema());
liquibase.setDropFirst(liquibaseProperties.isDropFirst());
liquibase.setSchemas(getSchemas(dataSource));
if (env.acceptsProfiles(JHipsterConstants.SPRING_PROFILE_NO_LIQUIBASE)) {
liquibase.setShouldRun(false);
} else {
liquibase.setShouldRun(liquibaseProperties.isEnabled());
log.debug("Configuring Multi Tenant Liquibase");
}
liquibase.setParameters(DatabaseUtil.defaultParams(liquibaseProperties.getDefaultSchema()));
return liquibase;
}

有没有办法在 createView 标记内容中使用属性或任何类型的系统变量?

最佳答案

我意识到我需要手动添加参数,在我看来,我无法仅设置 defaultSchema 属性来访问“defaultSchema”参数(liquibase.setDefaultSchema("something");):

 Map<String, String> parameters = new HashMap<>();
parameters.put("schemaName", schema);
liquibase.setChangeLogParameters(parameters);

关于java - Liquibase - createView 语句中的自定义参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53341383/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com