我正在使用Liquibase Java API从databaseChangeLog文件更新数据库。我还使用以下代码设置默认数据库架构:

    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(conn));
        database.setDefaultSchemaName(CustomerPortalServiceBeanFactory.getInstance().getServiceConfigurationData().getSchemaName());


除非我有一个更改日志创建一个视图,如下所示,否则此方法工作正常:

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
    <changeSet author="mhills" id="customerUser-view">
        <createView
               replaceIfExists="true"
               viewName="CUSTOMER_USER_VW">
               select
                customeruser.id,
                customeruser.status,
                customeruser.customer_id,
                customeruser.contact_id,
                customeruser.email_address,
                customeruser.online_name,
                customeruser.date_created
                FROM customer_user customeruser
        </createView>
    </changeSet>
</databaseChangeLog>


schemaName正确地为视图名称添加了前缀,但我还需要为from子句中使用的表添加前缀。是将模式名称用作占位符,还是有另一种方法可以实现此目的?

最佳答案

阅读文档后,我可以通过添加以下代码来访问集合“ schemaName”。

// set the schema read from the properties file.
System.setProperty("schemaName", CustomerPortalServiceBeanFactory.getInstance().getServiceConfigurationData().getSchemaName());

// Establish the Liquibase database connection
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(conn));
                database.setDefaultSchemaName(CustomerPortalServiceBeanFactory.getInstance().getServiceConfigurationData().getSchemaName());


通过将“ schemaName”值添加为系统属性,可以将其用作变更集中的占位符。下面的例子。

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
  xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
         http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
    <changeSet author="mhills" id="customerUser-view">
        <createView
               replaceIfExists="true"
               viewName="CUSTOMER_USER_VW">
               select
                customeruser.id,
                customeruser.status,
                customeruser.customer_id,
                customeruser.contact_id,
                customeruser.email_address,
                customeruser.online_name,
                customeruser.date_created
                FROM "${schemaName}".customer_user customeruser
        </createView>
    </changeSet>
</databaseChangeLog>

07-24 09:26