use of liquibase.change.ColumnConfig in project liquibase by liquibase.
the class XMLChangeLogSerializerTest method serialize_pretty_nestedNodeWithAttributes.
@Test
public void serialize_pretty_nestedNodeWithAttributes() {
CreateTableChange change = new CreateTableChange();
change.setCatalogName("a");
change.setSchemaName("b");
change.setTableName("c");
change.addColumn(new ColumnConfig().setName("x").setDefaultValue("x1"));
change.addColumn(new ColumnConfig().setName("y").setDefaultValue("y1"));
String out = new XMLChangeLogSerializer().serialize(change, true);
assertEquals("<createTable catalogName=\"a\"\n" + " schemaName=\"b\"\n" + " tableName=\"c\">\n" + " <column defaultValue=\"x1\" name=\"x\"/>\n" + " <column defaultValue=\"y1\" name=\"y\"/>\n" + "</createTable>", out);
}
use of liquibase.change.ColumnConfig in project liquibase by liquibase.
the class XMLChangeLogSerializerTest method createNode_ColumnConfig.
@Test
public void createNode_ColumnConfig() throws Exception {
ColumnConfig column = new ColumnConfig();
column.setName("id");
column.setType("varchar(255)");
column.setDefaultValue("test Value");
column.setValue("some value here");
ConstraintsConfig constraints = new ConstraintsConfig();
constraints.setDeferrable(Boolean.TRUE);
constraints.setDeleteCascade(true);
constraints.setForeignKeyName("FK_NAME");
constraints.setInitiallyDeferred(true);
constraints.setNullable(false);
constraints.setPrimaryKey(true);
constraints.setReferences("state(id)");
constraints.setUnique(true);
column.setConstraints(constraints);
Element element = new XMLChangeLogSerializer(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()).createNode(column);
assertEquals("column", element.getTagName());
assertEquals("id", element.getAttribute("name"));
assertEquals("varchar(255)", element.getAttribute("type"));
assertEquals("test Value", element.getAttribute("defaultValue"));
assertEquals("some value here", element.getAttribute("value"));
Element constraintsElement = (Element) element.getChildNodes().item(0);
assertEquals(8, constraintsElement.getAttributes().getLength());
assertEquals("true", constraintsElement.getAttribute("deferrable"));
assertEquals("true", constraintsElement.getAttribute("deleteCascade"));
assertEquals("FK_NAME", constraintsElement.getAttribute("foreignKeyName"));
assertEquals("true", constraintsElement.getAttribute("initiallyDeferred"));
assertEquals("false", constraintsElement.getAttribute("nullable"));
assertEquals("true", constraintsElement.getAttribute("primaryKey"));
assertEquals("state(id)", constraintsElement.getAttribute("references"));
assertEquals("true", constraintsElement.getAttribute("unique"));
}
use of liquibase.change.ColumnConfig in project liquibase by liquibase.
the class CopyRowsGenerator method generateSql.
@Override
public Sql[] generateSql(CopyRowsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
StringBuffer sql = new StringBuffer();
if (statement.getCopyColumns().size() == 0) {
return new Sql[] {};
}
if (database instanceof SQLiteDatabase) {
sql.append("INSERT INTO `").append(statement.getTargetTable()).append("` (");
for (int i = 0; i < statement.getCopyColumns().size(); i++) {
ColumnConfig column = statement.getCopyColumns().get(i);
if (i > 0) {
sql.append(",");
}
sql.append("`").append(column.getName()).append("`");
}
sql.append(") SELECT ");
for (int i = 0; i < statement.getCopyColumns().size(); i++) {
ColumnConfig column = statement.getCopyColumns().get(i);
if (i > 0) {
sql.append(",");
}
sql.append("`").append(column.getName()).append("`");
}
sql.append(" FROM `").append(statement.getSourceTable()).append("`");
}
return new Sql[] { new UnparsedSql(sql.toString(), getAffectedTable(statement)) };
}
use of liquibase.change.ColumnConfig in project liquibase by liquibase.
the class MissingTableChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
Table missingTable = (Table) missingObject;
PrimaryKey primaryKey = missingTable.getPrimaryKey();
// if (control.diffResult.getReferenceSnapshot().getDatabase().isLiquibaseTable(missingTable.getSchema().toCatalogAndSchema(), missingTable.getName())) {
// continue;
// }
CreateTableChange change = createCreateTableChange();
change.setTableName(missingTable.getName());
if (control.getIncludeCatalog()) {
change.setCatalogName(missingTable.getSchema().getCatalogName());
}
if (control.getIncludeSchema()) {
change.setSchemaName(missingTable.getSchema().getName());
}
if (missingTable.getRemarks() != null) {
change.setRemarks(missingTable.getRemarks());
}
for (Column column : missingTable.getColumns()) {
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setName(column.getName());
LiquibaseDataType ldt = DataTypeFactory.getInstance().from(column.getType(), referenceDatabase);
DatabaseDataType ddt = ldt.toDatabaseDataType(comparisonDatabase);
String typeString = ddt.toString();
if (comparisonDatabase instanceof MSSQLDatabase) {
typeString = comparisonDatabase.unescapeDataTypeString(typeString);
}
columnConfig.setType(typeString);
if (column.isAutoIncrement()) {
columnConfig.setAutoIncrement(true);
}
ConstraintsConfig constraintsConfig = null;
// In MySQL, the primary key must be specified at creation for an autoincrement column
if (column.isAutoIncrement() && primaryKey != null && primaryKey.getColumns().size() == 1 && primaryKey.getColumnNamesAsList().contains(column.getName())) {
if (referenceDatabase instanceof MSSQLDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && !primaryKey.getBackingIndex().getClustered()) {
// have to handle PK as a separate statement
} else if (referenceDatabase instanceof PostgresDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && primaryKey.getBackingIndex().getClustered()) {
// have to handle PK as a separate statement
} else {
constraintsConfig = new ConstraintsConfig();
if (shouldAddPrimarykeyToConstraints(missingObject, control, referenceDatabase, comparisonDatabase)) {
constraintsConfig.setPrimaryKey(true);
constraintsConfig.setPrimaryKeyTablespace(primaryKey.getTablespace());
// MySQL sets some primary key names as PRIMARY which is invalid
if (comparisonDatabase instanceof MySQLDatabase && "PRIMARY".equals(primaryKey.getName())) {
constraintsConfig.setPrimaryKeyName(null);
} else {
constraintsConfig.setPrimaryKeyName(primaryKey.getName());
}
control.setAlreadyHandledMissing(primaryKey);
control.setAlreadyHandledMissing(primaryKey.getBackingIndex());
} else {
constraintsConfig.setNullable(false);
}
}
} else if (column.isNullable() != null && !column.isNullable()) {
constraintsConfig = new ConstraintsConfig();
constraintsConfig.setNullable(false);
}
if (constraintsConfig != null) {
columnConfig.setConstraints(constraintsConfig);
}
setDefaultValue(columnConfig, column, referenceDatabase);
if (column.getRemarks() != null) {
columnConfig.setRemarks(column.getRemarks());
}
if (column.getAutoIncrementInformation() != null) {
BigInteger startWith = column.getAutoIncrementInformation().getStartWith();
BigInteger incrementBy = column.getAutoIncrementInformation().getIncrementBy();
if (startWith != null && !startWith.equals(BigInteger.ONE)) {
columnConfig.setStartWith(startWith);
}
if (incrementBy != null && !incrementBy.equals(BigInteger.ONE)) {
columnConfig.setIncrementBy(incrementBy);
}
}
change.addColumn(columnConfig);
control.setAlreadyHandledMissing(column);
}
return new Change[] { change };
}
use of liquibase.change.ColumnConfig in project liquibase by liquibase.
the class SelectFromDatabaseChangeLogLockExecutorTest method generateSql_count.
@Test
public void generateSql_count() throws Exception {
this.statementUnderTest = new SelectFromDatabaseChangeLogLockStatement(new ColumnConfig().setName("COUNT(*)", true));
assertCorrect("select count(*) from [databasechangeloglock] where [id]=1", MSSQLDatabase.class, SybaseDatabase.class);
assertCorrect("select count(*) from [databasechangeloglock] where [id]=1", MSSQLDatabase.class, SybaseASADatabase.class);
assertCorrect("select count(*) from [databasechangeloglock] where [id]=1 for update", OracleDatabase.class);
assertCorrectOnRest("select count(*) from [databasechangeloglock] where [id]=1");
}
Aggregations