use of liquibase.change.AddColumnConfig in project liquibase by liquibase.
the class CreateIndexGenerator method generateSql.
@Override
public Sql[] generateSql(CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
if (database instanceof OracleDatabase) {
// Oracle don't create index when creates foreignKey
// It means that all indexes associated with foreignKey should be created manualy
List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT))) {
return new Sql[0];
}
} else {
// Default filter of index creation:
// creation of all indexes with associations are switched off.
List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT) || associatedWith.contains(Index.MARK_FOREIGN_KEY))) {
return new Sql[0];
}
}
StringBuffer buffer = new StringBuffer();
buffer.append("CREATE ");
if (statement.isUnique() != null && statement.isUnique()) {
buffer.append("UNIQUE ");
}
if (database instanceof MSSQLDatabase) {
if (statement.isClustered() != null) {
if (statement.isClustered()) {
buffer.append("CLUSTERED ");
} else {
buffer.append("NONCLUSTERED ");
}
}
}
buffer.append("INDEX ");
if (statement.getIndexName() != null) {
String indexSchema = statement.getTableSchemaName();
buffer.append(database.escapeIndexName(statement.getTableCatalogName(), indexSchema, statement.getIndexName())).append(" ");
}
buffer.append("ON ");
if (database instanceof OracleDatabase && statement.isClustered() != null && statement.isClustered()) {
buffer.append("CLUSTER ");
}
buffer.append(database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName())).append("(");
Iterator<AddColumnConfig> iterator = Arrays.asList(statement.getColumns()).iterator();
while (iterator.hasNext()) {
AddColumnConfig column = iterator.next();
if (column.getComputed() == null) {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName(), false));
} else {
if (column.getComputed()) {
buffer.append(column.getName());
} else {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName()));
}
}
if (column.getDescending() != null && column.getDescending()) {
buffer.append(" DESC");
}
if (iterator.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) {
if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) {
buffer.append(" ON ").append(statement.getTablespace());
} else if (database instanceof DB2Database || database instanceof InformixDatabase) {
buffer.append(" IN ").append(statement.getTablespace());
} else {
buffer.append(" TABLESPACE ").append(statement.getTablespace());
}
}
if (database instanceof DB2Database && statement.isClustered() != null && statement.isClustered()) {
buffer.append(" CLUSTER");
}
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)) };
}
use of liquibase.change.AddColumnConfig in project liquibase by liquibase.
the class CreateIndexGeneratorPostgres method generateSql.
@Override
public Sql[] generateSql(CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
// Default filter of index creation:
// creation of all indexes with associations are switched off.
List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
if (associatedWith != null && (associatedWith.contains(Index.MARK_PRIMARY_KEY) || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT) || associatedWith.contains(Index.MARK_FOREIGN_KEY))) {
return new Sql[0];
}
StringBuilder buffer = new StringBuilder();
buffer.append("CREATE ");
if (statement.isUnique() != null && statement.isUnique()) {
buffer.append("UNIQUE ");
}
buffer.append("INDEX ");
if (statement.getIndexName() != null) {
// for postgres setting the schema name for the index name is invalid
buffer.append(database.escapeObjectName(statement.getIndexName(), Index.class)).append(" ");
}
buffer.append("ON ");
buffer.append(database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName())).append("(");
Iterator<AddColumnConfig> iterator = Arrays.asList(statement.getColumns()).iterator();
while (iterator.hasNext()) {
AddColumnConfig column = iterator.next();
if (column.getComputed() == null) {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName(), false));
} else {
if (column.getComputed()) {
buffer.append(column.getName());
} else {
buffer.append(database.escapeColumnName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName(), column.getName()));
}
}
if (iterator.hasNext()) {
buffer.append(", ");
}
}
buffer.append(")");
if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) {
if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) {
buffer.append(" ON ").append(statement.getTablespace());
} else if (database instanceof DB2Database || database instanceof InformixDatabase) {
buffer.append(" IN ").append(statement.getTablespace());
} else {
buffer.append(" TABLESPACE ").append(statement.getTablespace());
}
}
if (statement.isClustered() != null && statement.isClustered()) {
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)), new UnparsedSql("CLUSTER " + database.escapeTableName(statement.getTableCatalogName(), statement.getTableSchemaName(), statement.getTableName()) + " USING " + database.escapeObjectName(statement.getIndexName(), Index.class)) };
} else {
return new Sql[] { new UnparsedSql(buffer.toString(), getAffectedIndex(statement)) };
}
}
use of liquibase.change.AddColumnConfig in project liquibase by liquibase.
the class OfflineDatabaseTest method canOutputSQLFromOfflineOracleDB.
/**
* Check if it it's possible to output SQL from an OfflineConnection
* set to Oracle (offline:oracle).
*
* @see <a href="https://liquibase.jira.com/browse/CORE-2192">CORE-2192</a>
*/
@Test
public void canOutputSQLFromOfflineOracleDB() throws Exception {
AddColumnChange change = new AddColumnChange();
AddColumnConfig column1 = new AddColumnConfig();
column1.setName("column1");
column1.setType("INT");
change.addColumn(column1);
AddColumnConfig column2 = new AddColumnConfig();
column2.setName("column2");
column2.setType("INT");
change.addColumn(column2);
SqlStatement[] statements = new SqlStatement[0];
try {
statements = change.generateStatements(createOfflineDatabase("offline:oracle"));
} catch (DatabaseException e) {
Assert.fail("Can't generate statements from an Offline Oracle database.");
}
Assert.assertEquals(1, statements.length);
Assert.assertTrue(statements[0] instanceof AddColumnStatement);
AddColumnStatement stmt = (AddColumnStatement) statements[0];
Assert.assertTrue(stmt.isMultiple());
Assert.assertEquals(2, stmt.getColumns().size());
}
use of liquibase.change.AddColumnConfig in project liquibase by liquibase.
the class XMLChangeLogSerializerTest method createNode_addColumnChange.
@Test
public void createNode_addColumnChange() throws Exception {
AddColumnChange refactoring = new AddColumnChange();
refactoring.setTableName("TAB");
AddColumnConfig column = new AddColumnConfig();
column.setName("NEWCOL");
column.setType("TYP");
refactoring.addColumn(column);
Element node = new XMLChangeLogSerializer(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()).createNode(refactoring);
assertEquals("addColumn", node.getTagName());
assertEquals("TAB", node.getAttribute("tableName"));
NodeList columns = node.getElementsByTagName("column");
assertEquals(1, columns.getLength());
assertEquals("column", ((Element) columns.item(0)).getTagName());
assertEquals("NEWCOL", ((Element) columns.item(0)).getAttribute("name"));
assertEquals("TYP", ((Element) columns.item(0)).getAttribute("type"));
}
use of liquibase.change.AddColumnConfig in project liquibase by liquibase.
the class JsonChangeLogSerializerTest method serialize_changeSet.
@Test
public void serialize_changeSet() {
AddColumnChange addColumnChange = new AddColumnChange();
addColumnChange.setCatalogName("cat");
addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col1").setDefaultValueNumeric(3));
addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col2").setDefaultValueComputed(new DatabaseFunction("NOW()")));
addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col3").setDefaultValueBoolean(true));
addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col2").setDefaultValueDate(new Date(0)));
addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col2").setDefaultValueSequenceNext(new SequenceNextValueFunction("seq_me")));
ChangeSet changeSet = new ChangeSet("1", "nvoxland", false, false, "path/to/file.json", null, null, null);
changeSet.addChange(addColumnChange);
assertEquals("{\n" + " \"changeSet\": {\n" + " \"id\": \"1\",\n" + " \"author\": \"nvoxland\",\n" + " \"objectQuotingStrategy\": \"LEGACY\",\n" + " \"changes\": [\n" + " {\n" + " \"addColumn\": {\n" + " \"catalogName\": \"cat\",\n" + " \"columns\": [\n" + " {\n" + " \"column\": {\n" + " \"defaultValueNumeric\": 3,\n" + " \"name\": \"col1\"\n" + " }\n" + " },\n" + " {\n" + " \"column\": {\n" + " \"defaultValueComputed\": \"NOW()\",\n" + " \"name\": \"col2\"\n" + " }\n" + " },\n" + " {\n" + " \"column\": {\n" + " \"defaultValueBoolean\": true,\n" + " \"name\": \"col3\"\n" + " }\n" + " },\n" + " {\n" + " \"column\": {\n" + " \"defaultValueDate\": 1970-01-01T00:00:00Z,\n" + " \"name\": \"col2\"\n" + " }\n" + " },\n" + " {\n" + " \"column\": {\n" + " \"defaultValueSequenceNext\": \"seq_me\",\n" + " \"name\": \"col2\"\n" + " }\n" + " }]\n" + " \n" + " }\n" + " }]\n" + " \n" + " }\n" + "}\n", new JsonChangeLogSerializer().serialize(changeSet, true));
}
Aggregations