use of liquibase.change.ConstraintsConfig in project liquibase by liquibase.
the class AddColumnGeneratorSQLite method generateSql.
@Override
public Sql[] generateSql(final AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
// Workaround implemented by replacing a table with a new one (duplicate)
// with a new column added
final List<AddColumnStatement> columns = new ArrayList<>(statement.getColumns());
if (columns.size() == 0) {
columns.add(statement);
}
Set<String> newColumnNames = columns.stream().map(AddColumnStatement::getColumnName).collect(Collectors.toSet());
Sql[] generatedSqls;
SQLiteDatabase.AlterTableVisitor alterTableVisitor = new SQLiteDatabase.AlterTableVisitor() {
@Override
public ColumnConfig[] getColumnsToAdd() {
ColumnConfig[] columnConfigs = new ColumnConfig[columns.size()];
int i = 0;
for (AddColumnStatement column : columns) {
ColumnConfig newColumn = new ColumnConfig();
newColumn.setName(column.getColumnName());
newColumn.setType(column.getColumnType());
newColumn.setAutoIncrement(column.isAutoIncrement());
ConstraintsConfig constraintsConfig = new ConstraintsConfig();
if (column.isPrimaryKey()) {
constraintsConfig.setPrimaryKey(true);
}
if (column.isNullable()) {
constraintsConfig.setNullable(true);
}
if (column.isUnique()) {
constraintsConfig.setUnique(true);
}
newColumn.setConstraints(constraintsConfig);
for (ColumnConstraint constraint : column.getConstraints()) {
if (constraint instanceof ForeignKeyConstraint) {
final ForeignKeyConstraint fkConstraint = (ForeignKeyConstraint) constraint;
constraintsConfig.setReferencedTableCatalogName(fkConstraint.getReferencedTableCatalogName());
constraintsConfig.setReferencedTableSchemaName(fkConstraint.getReferencedTableSchemaName());
constraintsConfig.setReferencedTableName(fkConstraint.getReferencedTableName());
constraintsConfig.setReferencedColumnNames(fkConstraint.getReferencedColumnNames());
constraintsConfig.setReferences(fkConstraint.getReferences());
constraintsConfig.setForeignKeyName(fkConstraint.getForeignKeyName());
if (fkConstraint.isDeleteCascade()) {
constraintsConfig.setDeleteCascade(true);
}
}
}
columnConfigs[i++] = newColumn;
}
return columnConfigs;
}
@Override
public boolean copyThisColumn(ColumnConfig column) {
return !newColumnNames.contains(column.getName());
}
@Override
public boolean createThisColumn(ColumnConfig column) {
return true;
}
@Override
public boolean createThisIndex(Index index) {
return true;
}
};
final String catalogName = columns.get(0).getCatalogName();
final String schemaName = columns.get(0).getSchemaName();
final String tableName = columns.get(0).getTableName();
generatedSqls = SQLiteDatabase.getAlterTableSqls(database, alterTableVisitor, catalogName, schemaName, tableName);
return generatedSqls;
}
use of liquibase.change.ConstraintsConfig in project liquibase by liquibase.
the class XMLChangeLogSerializerTest method createNode_CreateTableChange.
@Test
public void createNode_CreateTableChange() throws Exception {
CreateTableChange change = new CreateTableChange();
change.setTableName("TABLE_NAME");
ColumnConfig column1 = new ColumnConfig();
column1.setName("id");
column1.setType("int");
ConstraintsConfig column1constraints = new ConstraintsConfig();
column1constraints.setPrimaryKey(true);
column1constraints.setNullable(false);
column1.setConstraints(column1constraints);
change.addColumn(column1);
ColumnConfig column2 = new ColumnConfig();
column2.setName("name");
column2.setType("varchar(255)");
change.addColumn(column2);
ColumnConfig column3 = new ColumnConfig();
column3.setName("state_id");
ConstraintsConfig column3constraints = new ConstraintsConfig();
column3constraints.setNullable(false);
column3constraints.setInitiallyDeferred(true);
column3constraints.setDeferrable(true);
column3constraints.setForeignKeyName("fk_tab_ref");
column3constraints.setReferences("state(id)");
column3.setConstraints(column3constraints);
change.addColumn(column3);
ColumnConfig column4 = new ColumnConfig();
column4.setName("phone");
column4.setType("varchar(255)");
column4.setDefaultValue("NOPHONE");
change.addColumn(column4);
ColumnConfig column5 = new ColumnConfig();
column5.setName("phone2");
column5.setType("varchar(255)");
ConstraintsConfig column5constraints = new ConstraintsConfig();
column5constraints.setUnique(true);
column5.setConstraints(column5constraints);
change.addColumn(column5);
Element element = new XMLChangeLogSerializer(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()).createNode(change);
assertEquals("createTable", element.getTagName());
assertEquals(5, element.getChildNodes().getLength());
Element columnElement = ((Element) element.getChildNodes().item(0));
assertEquals("column", columnElement.getTagName());
assertEquals("id", columnElement.getAttribute("name"));
assertEquals("int", columnElement.getAttribute("type"));
Element constraintsElement = (Element) columnElement.getChildNodes().item(0);
assertEquals("constraints", constraintsElement.getTagName());
assertEquals(2, constraintsElement.getAttributes().getLength());
assertEquals("true", constraintsElement.getAttribute("primaryKey"));
assertEquals("false", constraintsElement.getAttribute("nullable"));
columnElement = ((Element) element.getChildNodes().item(1));
assertEquals("column", columnElement.getTagName());
assertEquals("name", columnElement.getAttribute("name"));
assertEquals("varchar(255)", columnElement.getAttribute("type"));
columnElement = ((Element) element.getChildNodes().item(2));
assertEquals("column", columnElement.getTagName());
assertEquals("state_id", columnElement.getAttribute("name"));
constraintsElement = (Element) columnElement.getChildNodes().item(0);
assertEquals("constraints", constraintsElement.getTagName());
assertEquals(5, constraintsElement.getAttributes().getLength());
assertEquals("false", constraintsElement.getAttribute("nullable"));
assertEquals("true", constraintsElement.getAttribute("deferrable"));
assertEquals("true", constraintsElement.getAttribute("initiallyDeferred"));
assertEquals("fk_tab_ref", constraintsElement.getAttribute("foreignKeyName"));
assertEquals("state(id)", constraintsElement.getAttribute("references"));
columnElement = ((Element) element.getChildNodes().item(3));
assertEquals("column", columnElement.getTagName());
assertEquals("phone", columnElement.getAttribute("name"));
assertEquals("varchar(255)", columnElement.getAttribute("type"));
columnElement = ((Element) element.getChildNodes().item(4));
assertEquals("column", columnElement.getTagName());
assertEquals("phone2", columnElement.getAttribute("name"));
assertEquals("varchar(255)", columnElement.getAttribute("type"));
constraintsElement = (Element) columnElement.getChildNodes().item(0);
assertEquals("constraints", constraintsElement.getTagName());
assertEquals(1, constraintsElement.getAttributes().getLength());
assertEquals("true", constraintsElement.getAttribute("unique"));
}
use of liquibase.change.ConstraintsConfig in project liquibase by liquibase.
the class YamlSerializer method toMap.
protected Object toMap(LiquibaseSerializable object) {
Comparator<String> comparator;
comparator = getComparator(object);
Map<String, Object> objectMap = new TreeMap<String, Object>(comparator);
for (String field : object.getSerializableFields()) {
Object value = object.getSerializableFieldValue(field);
if (value != null) {
if (value instanceof DataType) {
value = ((Map) toMap((DataType) value)).values().iterator().next();
}
if (value instanceof Column.AutoIncrementInformation) {
value = ((Map) toMap((Column.AutoIncrementInformation) value)).values().iterator().next();
}
if (value instanceof ConstraintsConfig) {
value = ((Map) toMap((ConstraintsConfig) value)).values().iterator().next();
}
if (value instanceof LiquibaseSerializable) {
value = toMap((LiquibaseSerializable) value);
}
if (value instanceof Collection) {
List valueAsList = new ArrayList((Collection) value);
if (valueAsList.size() == 0) {
continue;
}
for (int i = 0; i < valueAsList.size(); i++) {
if (valueAsList.get(i) instanceof LiquibaseSerializable) {
valueAsList.set(i, toMap((LiquibaseSerializable) valueAsList.get(i)));
}
}
value = valueAsList;
}
if (value instanceof Map) {
if (((Map) value).size() == 0) {
continue;
}
for (Object key : ((Map) value).keySet()) {
Object mapValue = ((Map) value).get(key);
if (mapValue instanceof LiquibaseSerializable) {
((Map) value).put(key, toMap((LiquibaseSerializable) mapValue));
} else if (mapValue instanceof Collection) {
List valueAsList = new ArrayList((Collection) mapValue);
if (valueAsList.size() == 0) {
continue;
}
for (int i = 0; i < valueAsList.size(); i++) {
if (valueAsList.get(i) instanceof LiquibaseSerializable) {
valueAsList.set(i, toMap((LiquibaseSerializable) valueAsList.get(i)));
}
}
((Map) value).put(key, valueAsList);
}
}
}
objectMap.put(field, value);
}
}
Map<String, Object> containerMap = new HashMap<String, Object>();
containerMap.put(object.getSerializedObjectName(), objectMap);
return containerMap;
}
use of liquibase.change.ConstraintsConfig in project liquibase by liquibase.
the class XMLChangeLogSerializer method createNode.
public Element createNode(ColumnConfig columnConfig) {
Element element = currentChangeLogFileDOM.createElementNS(columnConfig.getSerializedObjectNamespace(), "column");
if (columnConfig.getName() != null) {
element.setAttribute("name", columnConfig.getName());
}
if (columnConfig.getType() != null) {
element.setAttribute("type", columnConfig.getType());
}
if (columnConfig.getDefaultValue() != null) {
element.setAttribute("defaultValue", columnConfig.getDefaultValue());
}
if (columnConfig.getDefaultValueNumeric() != null) {
element.setAttribute("defaultValueNumeric", columnConfig.getDefaultValueNumeric().toString());
}
if (columnConfig.getDefaultValueDate() != null) {
element.setAttribute("defaultValueDate", new ISODateFormat().format(columnConfig.getDefaultValueDate()));
}
if (columnConfig.getDefaultValueBoolean() != null) {
element.setAttribute("defaultValueBoolean", columnConfig.getDefaultValueBoolean().toString());
}
if (columnConfig.getDefaultValueComputed() != null) {
element.setAttribute("defaultValueComputed", columnConfig.getDefaultValueComputed().toString());
}
if (columnConfig.getDefaultValueSequenceNext() != null) {
element.setAttribute("defaultValueSequenceNext", columnConfig.getDefaultValueSequenceNext().toString());
}
if (columnConfig.getValue() != null) {
element.setAttribute("value", columnConfig.getValue());
}
if (columnConfig.getValueNumeric() != null) {
element.setAttribute("valueNumeric", columnConfig.getValueNumeric().toString());
}
if (columnConfig.getValueBoolean() != null) {
element.setAttribute("valueBoolean", columnConfig.getValueBoolean().toString());
}
if (columnConfig.getValueDate() != null) {
element.setAttribute("valueDate", new ISODateFormat().format(columnConfig.getValueDate()));
}
if (columnConfig.getValueComputed() != null) {
element.setAttribute("valueComputed", columnConfig.getValueComputed().toString());
}
if (columnConfig.getValueSequenceNext() != null) {
element.setAttribute("valueSequenceNext", columnConfig.getValueSequenceNext().toString());
}
if (columnConfig.getValueSequenceCurrent() != null) {
element.setAttribute("valueSequenceNext", columnConfig.getValueSequenceCurrent().toString());
}
if (StringUtil.trimToNull(columnConfig.getRemarks()) != null) {
element.setAttribute("remarks", columnConfig.getRemarks());
}
if ((columnConfig.isAutoIncrement() != null) && columnConfig.isAutoIncrement()) {
element.setAttribute("autoIncrement", "true");
}
ConstraintsConfig constraints = columnConfig.getConstraints();
if (constraints != null) {
Element constraintsElement = currentChangeLogFileDOM.createElementNS(columnConfig.getSerializedObjectNamespace(), "constraints");
if (constraints.getCheckConstraint() != null) {
constraintsElement.setAttribute("checkConstraint", constraints.getCheckConstraint());
}
if (constraints.getForeignKeyName() != null) {
constraintsElement.setAttribute("foreignKeyName", constraints.getForeignKeyName());
}
if (constraints.getReferences() != null) {
constraintsElement.setAttribute("references", constraints.getReferences());
}
if (constraints.getReferencedTableName() != null) {
constraintsElement.setAttribute("referencedTableName", constraints.getReferencedTableName());
}
if (constraints.getReferencedColumnNames() != null) {
constraintsElement.setAttribute("referencedColumnNames", constraints.getReferencedColumnNames());
}
if (constraints.isDeferrable() != null) {
constraintsElement.setAttribute("deferrable", constraints.isDeferrable().toString());
}
if (constraints.getValidateNullable() != null) {
constraintsElement.setAttribute("validateNullable", constraints.getValidateNullable().toString());
}
if (constraints.getValidateUnique() != null) {
constraintsElement.setAttribute("validateUnique", constraints.getValidateUnique().toString());
}
if (constraints.getValidatePrimaryKey() != null) {
constraintsElement.setAttribute("validatePrimaryKey", constraints.getValidatePrimaryKey().toString());
}
if (constraints.getValidateForeignKey() != null) {
constraintsElement.setAttribute("validateForeignKey", constraints.getValidateForeignKey().toString());
}
if (constraints.isDeleteCascade() != null) {
constraintsElement.setAttribute("deleteCascade", constraints.isDeleteCascade().toString());
}
if (constraints.isInitiallyDeferred() != null) {
constraintsElement.setAttribute("initiallyDeferred", constraints.isInitiallyDeferred().toString());
}
if (constraints.isNullable() != null) {
constraintsElement.setAttribute("nullable", constraints.isNullable().toString());
}
if (constraints.isPrimaryKey() != null) {
constraintsElement.setAttribute("primaryKey", constraints.isPrimaryKey().toString());
}
if (constraints.isUnique() != null) {
constraintsElement.setAttribute("unique", constraints.isUnique().toString());
}
if (constraints.getUniqueConstraintName() != null) {
constraintsElement.setAttribute("uniqueConstraintName", constraints.getUniqueConstraintName());
}
if (constraints.getPrimaryKeyName() != null) {
constraintsElement.setAttribute("primaryKeyName", constraints.getPrimaryKeyName());
}
if (constraints.getPrimaryKeyTablespace() != null) {
constraintsElement.setAttribute("primaryKeyTablespace", constraints.getPrimaryKeyTablespace());
}
if (constraints.getNotNullConstraintName() != null) {
constraintsElement.setAttribute("notNullConstraintName", constraints.getNotNullConstraintName());
}
if (constraints.getReferencedTableCatalogName() != null) {
constraintsElement.setAttribute("referencedTableCatalogName", constraints.getReferencedTableCatalogName());
}
if (constraints.getReferencedTableSchemaName() != null) {
constraintsElement.setAttribute("referencedTableSchemaName", constraints.getReferencedTableSchemaName());
}
element.appendChild(constraintsElement);
}
return element;
}
use of liquibase.change.ConstraintsConfig in project liquibase by liquibase.
the class MissingColumnChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Column column = (Column) missingObject;
if (column.getRelation() instanceof View) {
return null;
}
if (column.getRelation().getSnapshotId() == null) {
// not an actual table, maybe an alias, maybe in a different schema. Don't fix it.
return null;
}
AddColumnChange change = createAddColumnChange();
change.setTableName(column.getRelation().getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(column.getRelation().getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(column.getRelation().getSchema().getName());
}
AddColumnConfig columnConfig = createAddColumnConfig();
columnConfig.setName(column.getName());
String dataType = column.getType().toString();
columnConfig.setType(dataType);
MissingTableChangeGenerator.setDefaultValue(columnConfig, column, comparisonDatabase);
Column.AutoIncrementInformation autoIncrementInfo = column.getAutoIncrementInformation();
if (autoIncrementInfo != null) {
columnConfig.setAutoIncrement(true);
columnConfig.setGenerationType(autoIncrementInfo.getGenerationType());
columnConfig.setDefaultOnNull(autoIncrementInfo.getDefaultOnNull());
}
if (column.getRemarks() != null) {
columnConfig.setRemarks(column.getRemarks());
}
ConstraintsConfig constraintsConfig = columnConfig.getConstraints();
if ((column.isNullable() != null) && !column.isNullable()) {
if (constraintsConfig == null) {
constraintsConfig = new ConstraintsConfig();
}
constraintsConfig.setNullable(false);
constraintsConfig.setNotNullConstraintName(column.getAttribute("notNullConstraintName", String.class));
if (!column.getValidateNullable()) {
constraintsConfig.setValidateNullable(false);
}
}
if (constraintsConfig != null) {
columnConfig.setConstraints(constraintsConfig);
}
change.addColumn(columnConfig);
return new Change[] { change };
}
Aggregations