liquibase.change.core.CreateIndexChange Maven / Gradle / Ivy
package liquibase.change.core;
import liquibase.change.*;
import liquibase.database.Database;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.CreateIndexStatement;
import liquibase.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Creates an index on an existing column.
*/
public class CreateIndexChange extends AbstractChange implements ChangeWithColumns {
private String schemaName;
private String tableName;
private String indexName;
private Boolean unique;
private String tablespace;
private List columns;
// Contain associations of index
// for example: foreignKey, primaryKey or uniqueConstraint
@ChangeProperty(includeInSerialization = false)
private String associatedWith;
public CreateIndexChange() {
super("createIndex", "Create Index", ChangeMetaData.PRIORITY_DEFAULT);
columns = new ArrayList();
}
public String getIndexName() {
return indexName;
}
public void setIndexName(String indexName) {
this.indexName = indexName;
}
public String getSchemaName() {
return schemaName;
}
public void setSchemaName(String schemaName) {
this.schemaName = StringUtils.trimToNull(schemaName);
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public List getColumns() {
return columns;
}
public void setColumns(List columns) {
this.columns = columns;
}
public void addColumn(ColumnConfig column) {
columns.add(column);
}
public String getTablespace() {
return tablespace;
}
public void setTablespace(String tablespace) {
this.tablespace = tablespace;
}
public SqlStatement[] generateStatements(Database database) {
List columns = new ArrayList();
for (ColumnConfig column : getColumns()) {
columns.add(column.getName());
}
return new SqlStatement[]{
new CreateIndexStatement(
getIndexName(),
getSchemaName() == null ? database.getDefaultSchemaName() : getSchemaName(),
getTableName(),
this.isUnique(),
getAssociatedWith(),
columns.toArray(new String[getColumns().size()]))
.setTablespace(getTablespace())
};
}
@Override
protected Change[] createInverses() {
DropIndexChange inverse = new DropIndexChange();
inverse.setSchemaName(getSchemaName());
inverse.setTableName(getTableName());
inverse.setIndexName(getIndexName());
return new Change[]{
inverse
};
}
public String getConfirmationMessage() {
return "Index " + getIndexName() + " created";
}
/**
* @param isUnique the isUnique to set
*/
public void setUnique(Boolean isUnique) {
this.unique = isUnique;
}
/**
* @return the isUnique
*/
public Boolean isUnique() {
return this.unique;
}
/**
* @return Index associations. Valid values:
* primaryKey
* foreignKey
* uniqueConstraint
* none
* */
public String getAssociatedWith() {
return associatedWith;
}
public void setAssociatedWith(String associatedWith) {
this.associatedWith = associatedWith;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy