liquibase.change.core.RenameColumnChange Maven / Gradle / Ivy
package liquibase.change.core;
import liquibase.change.AbstractChange;
import liquibase.change.Change;
import liquibase.change.ChangeMetaData;
import liquibase.change.ColumnConfig;
import liquibase.database.Database;
import liquibase.database.core.SQLiteDatabase;
import liquibase.database.core.SQLiteDatabase.AlterTableVisitor;
import liquibase.database.structure.Index;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.RenameColumnStatement;
import liquibase.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Renames an existing column.
*/
public class RenameColumnChange extends AbstractChange {
private String schemaName;
private String tableName;
private String oldColumnName;
private String newColumnName;
private String columnDataType;
public RenameColumnChange() {
super("renameColumn", "Rename Column", ChangeMetaData.PRIORITY_DEFAULT);
}
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 String getOldColumnName() {
return oldColumnName;
}
public void setOldColumnName(String oldColumnName) {
this.oldColumnName = oldColumnName;
}
public String getNewColumnName() {
return newColumnName;
}
public void setNewColumnName(String newColumnName) {
this.newColumnName = newColumnName;
}
public String getColumnDataType() {
return columnDataType;
}
public void setColumnDataType(String columnDataType) {
this.columnDataType = columnDataType;
}
public SqlStatement[] generateStatements(Database database) {
//todo if (database instanceof SQLiteDatabase) {
// // return special statements for SQLite databases
// return generateStatementsForSQLiteDatabase(database);
// }
return new SqlStatement[] { new RenameColumnStatement(
getSchemaName() == null?database.getDefaultSchemaName():getSchemaName(),
getTableName(), getOldColumnName(), getNewColumnName(),
getColumnDataType())
};
}
private SqlStatement[] generateStatementsForSQLiteDatabase(Database database) {
// SQLite does not support this ALTER TABLE operation until now.
// For more information see: http://www.sqlite.org/omitted.html.
// This is a small work around...
List statements = new ArrayList();
// define alter table logic
AlterTableVisitor rename_alter_visitor =
new AlterTableVisitor() {
public ColumnConfig[] getColumnsToAdd() {
return new ColumnConfig[0];
}
public boolean copyThisColumn(ColumnConfig column) {
return true;
}
public boolean createThisColumn(ColumnConfig column) {
if (column.getName().equals(getOldColumnName())) {
column.setName(getNewColumnName());
}
return true;
}
public boolean createThisIndex(Index index) {
if (index.getColumns().contains(getOldColumnName())) {
index.getColumns().remove(getOldColumnName());
index.getColumns().add(getNewColumnName());
}
return true;
}
};
try {
// alter table
statements.addAll(SQLiteDatabase.getAlterTableStatements(
rename_alter_visitor,
database,getSchemaName(),getTableName()));
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
return statements.toArray(new SqlStatement[statements.size()]);
}
@Override
protected Change[] createInverses() {
RenameColumnChange inverse = new RenameColumnChange();
inverse.setSchemaName(getSchemaName());
inverse.setTableName(getTableName());
inverse.setOldColumnName(getNewColumnName());
inverse.setNewColumnName(getOldColumnName());
inverse.setColumnDataType(getColumnDataType());
return new Change[]{
inverse
};
}
public String getConfirmationMessage() {
return "Column "+tableName+"."+ oldColumnName + " renamed to " + newColumnName;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy