use of liquibase.change.core.ModifyDataTypeChange in project seed by euu-rocks.
the class EntityChangeLogBuilder method addModifyDataTypeChangeSet.
private void addModifyDataTypeChangeSet(Entity entity, EntityField field) {
Assert.notNull(entity, C.ENTITY);
Assert.notNull(field, C.FIELD);
final ModifyDataTypeChange modifyDataTypeChange = new ModifyDataTypeChange();
modifyDataTypeChange.setTableName(getTableName(entity));
modifyDataTypeChange.setColumnName(getColumnName(field));
modifyDataTypeChange.setNewDataType(getDBFieldType(field));
addChange(modifyDataTypeChange);
}
use of liquibase.change.core.ModifyDataTypeChange in project meveo by meveo-org.
the class CustomTableCreatorService method updateField.
/**
* Update a field of a db table. Creates a liquibase changeset to add a field to
* a table and executes it
*
* @param dbTableName DB Table name
* @param cft Field definition
*/
public void updateField(String sqlConnectionCode, String dbTableName, CustomFieldTemplate cft) {
String dbFieldname = cft.getDbFieldname();
try (Session hibernateSession = sqlConnectionProvider.getSession(sqlConnectionCode)) {
String columnExistsQueryStr = "SELECT EXISTS(\n" + " SELECT column_name\n" + " FROM information_schema.columns \n" + " WHERE table_name=:tableName and column_name=:columnName\n" + ");";
Query columnExistsQuery = hibernateSession.createNativeQuery(columnExistsQueryStr).setParameter("tableName", dbTableName).setParameter("columnName", dbFieldname);
boolean columnExists = (boolean) columnExistsQuery.getSingleResult();
if (!columnExists) {
addField(sqlConnectionCode, dbTableName, cft);
return;
}
DatabaseChangeLog dbLog = new DatabaseChangeLog("path");
// Drop not null constraint and add again if needed - a better way would be to
// check if valueRequired field value was changed
ChangeSet changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_RNN_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
changeSet.setFailOnError(false);
DropNotNullConstraintChange dropNotNullChange = new DropNotNullConstraintChange();
dropNotNullChange.setTableName(dbTableName);
dropNotNullChange.setColumnName(dbFieldname);
String type;
try {
type = getColumnType(cft);
} catch (ClassNotFoundException e1) {
throw new IllegalArgumentException("Cannot get field type for entity with class or code " + cft.getEntityClazzCetCode(), e1);
}
dropNotNullChange.setColumnDataType(type);
changeSet.addChange(dropNotNullChange);
dbLog.addChangeSet(changeSet);
// Add not null constraint if needed
if (cft.isValueRequired()) {
changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_ANN_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
AddNotNullConstraintChange addNotNullChange = new AddNotNullConstraintChange();
addNotNullChange.setTableName(dbTableName);
addNotNullChange.setColumnName(dbFieldname);
addNotNullChange.setColumnDataType(type);
changeSet.addChange(addNotNullChange);
dbLog.addChangeSet(changeSet);
}
// Default value does not apply to date type field
if (cft.getFieldType() != CustomFieldTypeEnum.DATE) {
changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_RD_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
changeSet.setFailOnError(false);
DropDefaultValueChange dropDefaultValueChange = new DropDefaultValueChange();
dropDefaultValueChange.setTableName(dbTableName);
dropDefaultValueChange.setColumnName(dbFieldname);
dropDefaultValueChange.setColumnDataType(type);
changeSet.addChange(dropDefaultValueChange);
dbLog.addChangeSet(changeSet);
// Add default value if needed
if (cft.getDefaultValue() != null) {
changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_AD_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
AddDefaultValueChange addDefaultValueChange = new AddDefaultValueChange();
addDefaultValueChange.setTableName(dbTableName);
addDefaultValueChange.setColumnName(dbFieldname);
addDefaultValueChange.setColumnDataType(type);
if (cft.getFieldType() == CustomFieldTypeEnum.DOUBLE) {
addDefaultValueChange.setDefaultValueNumeric(cft.getDefaultValue());
} else if (cft.getFieldType() == CustomFieldTypeEnum.LONG) {
addDefaultValueChange.setDefaultValueNumeric(cft.getDefaultValue());
} else if (cft.getFieldType() == CustomFieldTypeEnum.SECRET || cft.getFieldType() == CustomFieldTypeEnum.STRING || cft.getFieldType() == CustomFieldTypeEnum.LIST) {
addDefaultValueChange.setDefaultValue(cft.getDefaultValue());
} else if (cft.getFieldType() == CustomFieldTypeEnum.BOOLEAN) {
addDefaultValueChange.setColumnDataType("int");
addDefaultValueChange.setDefaultValueNumeric("1".equals(cft.getDefaultValue()) || "true".equalsIgnoreCase(cft.getDefaultValue()) ? "1" : "0");
}
changeSet.addChange(addDefaultValueChange);
dbLog.addChangeSet(changeSet);
}
}
// Update field length for String type fields.
if (cft.getFieldType() == CustomFieldTypeEnum.STRING || cft.getFieldType() == CustomFieldTypeEnum.LIST) {
changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_M_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
changeSet.setFailOnError(false);
ModifyDataTypeChange modifyDataTypeChange = new ModifyDataTypeChange();
modifyDataTypeChange.setTableName(dbTableName);
modifyDataTypeChange.setColumnName(dbFieldname);
modifyDataTypeChange.setNewDataType(type);
changeSet.addChange(modifyDataTypeChange);
dbLog.addChangeSet(changeSet);
}
createOrUpdateUniqueField(dbTableName, cft, changeSet);
hibernateSession.doWork(connection -> {
Database database;
try {
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
setSchemaName(database);
Liquibase liquibase = new Liquibase(dbLog, new ClassLoaderResourceAccessor(), database);
liquibase.update(new Contexts(), new LabelExpression());
} catch (Exception e) {
log.error("Failed to update a field {} in a custom table {}", dbTableName, dbFieldname, e);
throw new SQLException(e);
}
});
}
}
Aggregations