Search in sources :

Example 16 with AddColumnStatement

use of liquibase.statement.core.AddColumnStatement in project liquibase by liquibase.

the class AddColumnChangeTest method generateStatements_multipleColumns.

@Test
public void generateStatements_multipleColumns() {
    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 = change.generateStatements(new MockDatabase());
    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) MockDatabase(liquibase.sdk.database.MockDatabase) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 17 with AddColumnStatement

use of liquibase.statement.core.AddColumnStatement in project liquibase by liquibase.

the class AddColumnGenerator method validate.

@Override
public ValidationErrors validate(AddColumnStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    if (statement.isMultiple()) {
        ValidationErrors validationErrors = new ValidationErrors();
        AddColumnStatement firstColumn = statement.getColumns().get(0);
        for (AddColumnStatement column : statement.getColumns()) {
            validationErrors.addAll(validateSingleColumn(column, database));
            if (firstColumn.getTableName() != null && !firstColumn.getTableName().equals(column.getTableName())) {
                validationErrors.addError("All columns must be targeted at the same table");
            }
            if (column.isMultiple()) {
                validationErrors.addError("Nested multiple add column statements are not supported");
            }
        }
        return validationErrors;
    } else {
        return validateSingleColumn(statement, database);
    }
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) AddColumnStatement(liquibase.statement.core.AddColumnStatement)

Aggregations

AddColumnStatement (liquibase.statement.core.AddColumnStatement)17 Test (org.junit.Test)13 MockSqlGeneratorChain (liquibase.sqlgenerator.MockSqlGeneratorChain)4 AddColumnChange (liquibase.change.core.AddColumnChange)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)3 SqlStatement (liquibase.statement.SqlStatement)3 ArrayList (java.util.ArrayList)2 DB2Database (liquibase.database.core.DB2Database)2 ValidationErrors (liquibase.exception.ValidationErrors)2 Sql (liquibase.sql.Sql)2 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)2 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)2 HashSet (java.util.HashSet)1 AddColumnConfig (liquibase.change.AddColumnConfig)1 DerbyDatabase (liquibase.database.core.DerbyDatabase)1 H2Database (liquibase.database.core.H2Database)1 OracleDatabase (liquibase.database.core.OracleDatabase)1 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 DatabaseException (liquibase.exception.DatabaseException)1