我已经配置了liquibase以在PostgreSQL中使用模式的多租户工作。当changelog仅创建仅依赖xml语法的表或结构时,它工作正常。但是,我需要创建一些依赖于 defaultSchemaName 参数的视图,因此我已经尝试过这种方法,但是没有成功:
<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标签内容中使用属性或任何类型的系统变量?
答案 0 :(得分:0)
您可以使用属性文件中的架构名称:
@Value("${spring.jpa.properties.hibernate.default_schema}")
private String schemaName;
现在,您可以在代码中使用schemaName
变量了。
答案 1 :(得分:0)
我意识到我需要手动添加参数,在我看来,仅设置defaultSchema属性(liquibase.setDefaultSchema("something");
才能访问“ defaultSchema”参数:
Map<String, String> parameters = new HashMap<>();
parameters.put("schemaName", schema);
liquibase.setChangeLogParameters(parameters);