2016-12-12 92 views

回答

1

看一看「DBMS」前提這裏:http://www.liquibase.org/documentation/preconditions.html。你可以把所有postgesql特定的代碼放在那裏。

+0

感謝您指出先決條件。但是,這似乎是一個曲折的解決方案,因爲我不得不手動創建所有缺少的Postgres索引。另外,對於每一個將要介紹的新外鍵,人們都必須記住也要創建這樣一個postgres特定的變更集。 – SebastianRiemer

+0

RDBMS可移植性錯覺的另一個缺點。 –

+0

@SebastianRiemer是的,我同意。剛剛看到有自定義更改標籤:http://www.liquibase.org/documentation/changes/custom_change.html。因此,如果您熟悉Java,則可以嘗試構建myForeignKey標記,以檢查數據庫供應商並根據需要生成查詢以創建索引。 – dbf

0

我打算用CustomChange命令來解決此問題:

public class AddForeignKeyConstraintWithIndex extends 
liquibase.change.core.AddForeignKeyConstraintChange implements 
liquibase.change.custom.CustomSqlChange { 

@SuppressWarnings({"UnusedDeclaration", "FieldCanBeLocal"}) 
private ResourceAccessor resourceAccessor; 

@Override 
public void setUp() throws SetupException { 
} 

@Override 
public void setFileOpener(ResourceAccessor resourceAccessor) { 
    this.resourceAccessor = resourceAccessor; 
} 

@Override 
public SqlStatement[] generateStatements(Database database) { 
    SqlStatement[] result = super.generateStatements(database); 
    // Für Postgres zusätzlich Index anlegen 
    if (database instanceof PostgresDatabase) { 
     AddColumnConfig columnConfig = new AddColumnConfig(); 
     columnConfig.setName(getBaseColumnNames()); 
     columnConfig.setComputed(Boolean.FALSE); 

     SqlStatement createIndexStatement = new CreateIndexStatement(
       "ix_" + getConstraintName(), 
       getBaseTableCatalogName(), 
       getBaseTableSchemaName(), 
       getBaseTableName(), 
       Boolean.FALSE, 
       null, 
       columnConfig 
     ); 
     List<SqlStatement> list = new ArrayList<>(Arrays.asList(result)); 
     list.add(createIndexStatement); 

     return list.toArray(new SqlStatement[list.size()]); 

    } 
    return result; 
} 

然後,您可以撥打這個CustomChange,因爲這(完全一樣PARAMS作爲AddForeignKeyConstraint語句):

<customChange 
class="eu.***.***.AddForeignKeyConstraintWithIndex" 
     baseTableName="tbl_interest_reference" 
     baseColumnNames="customer_id" 
     constraintName="finterest_ref_customer_fk" 
     referencedTableName="tbl_customer" 
     referencedColumnNames="customer_id" 
    />