Search in sources :

Example 1 with AddColumnConfig

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)) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) AddColumnConfig(liquibase.change.AddColumnConfig) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 2 with AddColumnConfig

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)) };
    }
}
Also used : SybaseASADatabase(liquibase.database.core.SybaseASADatabase) DB2Database(liquibase.database.core.DB2Database) InformixDatabase(liquibase.database.core.InformixDatabase) UnparsedSql(liquibase.sql.UnparsedSql) AddColumnConfig(liquibase.change.AddColumnConfig) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 3 with AddColumnConfig

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());
}
Also used : SqlStatement(liquibase.statement.SqlStatement) AddColumnConfig(liquibase.change.AddColumnConfig) AddColumnChange(liquibase.change.core.AddColumnChange) DatabaseException(liquibase.exception.DatabaseException) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 4 with AddColumnConfig

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"));
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) AddColumnConfig(liquibase.change.AddColumnConfig) Test(org.junit.Test)

Example 5 with AddColumnConfig

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));
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) AddColumnConfig(liquibase.change.AddColumnConfig) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) AddColumnChange(liquibase.change.core.AddColumnChange) ChangeSet(liquibase.changelog.ChangeSet) Date(java.util.Date) Test(org.junit.Test)

Aggregations

AddColumnConfig (liquibase.change.AddColumnConfig)11 Test (org.junit.Test)4 Change (liquibase.change.Change)3 AddColumnChange (liquibase.change.core.AddColumnChange)3 CreateIndexChange (liquibase.change.core.CreateIndexChange)2 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)2 Difference (liquibase.diff.Difference)2 Sql (liquibase.sql.Sql)2 UnparsedSql (liquibase.sql.UnparsedSql)2 SqlStatement (liquibase.statement.SqlStatement)2 Column (liquibase.structure.core.Column)2 Index (liquibase.structure.core.Index)2 Element (org.w3c.dom.Element)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 CatalogAndSchema (liquibase.CatalogAndSchema)1 ColumnConfig (liquibase.change.ColumnConfig)1 ConstraintsConfig (liquibase.change.ConstraintsConfig)1 CreateTableChange (liquibase.change.core.CreateTableChange)1