Search in sources :

Example 1 with AddColumnStatement

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

the class AddColumnChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    List<SqlStatement> sql = new ArrayList<SqlStatement>();
    List<AddColumnStatement> addColumnStatements = new ArrayList<AddColumnStatement>();
    List<UpdateStatement> addColumnUpdateStatements = new ArrayList<UpdateStatement>();
    if (getColumns().size() == 0) {
        return new SqlStatement[] { new AddColumnStatement(catalogName, schemaName, tableName, null, null, null) };
    }
    for (AddColumnConfig column : getColumns()) {
        Set<ColumnConstraint> constraints = new HashSet<ColumnConstraint>();
        ConstraintsConfig constraintsConfig = column.getConstraints();
        if (constraintsConfig != null) {
            if (constraintsConfig.isNullable() != null && !constraintsConfig.isNullable()) {
                constraints.add(new NotNullConstraint());
            }
            if (constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
                constraints.add(new UniqueConstraint());
            }
            if (constraintsConfig.isPrimaryKey() != null && constraintsConfig.isPrimaryKey()) {
                constraints.add(new PrimaryKeyConstraint(constraintsConfig.getPrimaryKeyName()));
            }
            if (constraintsConfig.getReferences() != null || (constraintsConfig.getReferencedColumnNames() != null && constraintsConfig.getReferencedTableName() != null)) {
                constraints.add(new ForeignKeyConstraint(constraintsConfig.getForeignKeyName(), constraintsConfig.getReferences(), constraintsConfig.getReferencedTableName(), constraintsConfig.getReferencedColumnNames()));
            }
        }
        if (column.isAutoIncrement() != null && column.isAutoIncrement()) {
            constraints.add(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy()));
        }
        AddColumnStatement addColumnStatement = new AddColumnStatement(getCatalogName(), getSchemaName(), getTableName(), column.getName(), column.getType(), column.getDefaultValueObject(), column.getRemarks(), constraints.toArray(new ColumnConstraint[constraints.size()]));
        addColumnStatement.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
        if ((database instanceof MySQLDatabase) && (column.getAfterColumn() != null)) {
            addColumnStatement.setAddAfterColumn(column.getAfterColumn());
        } else if (((database instanceof HsqlDatabase) || (database instanceof H2Database)) && (column.getBeforeColumn() != null)) {
            addColumnStatement.setAddBeforeColumn(column.getBeforeColumn());
        } else if ((database instanceof FirebirdDatabase) && (column.getPosition() != null)) {
            addColumnStatement.setAddAtPosition(column.getPosition());
        }
        addColumnStatements.add(addColumnStatement);
        if (column.getValueObject() != null) {
            UpdateStatement updateStatement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
            updateStatement.addNewColumnValue(column.getName(), column.getValueObject());
            if (database instanceof DB2Database) {
                // Cannot update until table is reorganized in DB2
                addColumnUpdateStatements.add(updateStatement);
            } else {
                sql.add(updateStatement);
            }
        }
    }
    if (database instanceof DB2Database) {
        sql.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
        // Add all the update statements after the reorg table in DB2
        sql.addAll(addColumnUpdateStatements);
    }
    if (addColumnStatements.size() == 1) {
        sql.add(0, addColumnStatements.get(0));
    } else {
        sql.add(0, new AddColumnStatement(addColumnStatements));
    }
    for (ColumnConfig column : getColumns()) {
        String columnRemarks = StringUtils.trimToNull(column.getRemarks());
        if (columnRemarks != null) {
            SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks);
            if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
                sql.add(remarksStatement);
            }
        }
    }
    return sql.toArray(new SqlStatement[sql.size()]);
}
Also used : ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList) SetColumnRemarksStatement(liquibase.statement.core.SetColumnRemarksStatement) HashSet(java.util.HashSet) UpdateStatement(liquibase.statement.core.UpdateStatement) AddColumnStatement(liquibase.statement.core.AddColumnStatement)

Example 2 with AddColumnStatement

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

the class AddColumnGenerator method generateMultipleColumns.

private Sql[] generateMultipleColumns(List<AddColumnStatement> columns, Database database) {
    List<Sql> result = new ArrayList<Sql>();
    if (database instanceof MySQLDatabase) {
        String alterTable = generateSingleColumBaseSQL(columns.get(0), database);
        for (int i = 0; i < columns.size(); i++) {
            alterTable += generateSingleColumnSQL(columns.get(i), database);
            if (i < columns.size() - 1) {
                alterTable += ",";
            }
        }
        result.add(new UnparsedSql(alterTable, getAffectedColumns(columns)));
        for (AddColumnStatement statement : columns) {
            addUniqueConstrantStatements(statement, database, result);
            addForeignKeyStatements(statement, database, result);
        }
    } else {
        for (AddColumnStatement column : columns) {
            result.addAll(Arrays.asList(generateSingleColumn(column, database)));
        }
    }
    return result.toArray(new Sql[result.size()]);
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) ArrayList(java.util.ArrayList) ForeignKeyConstraint(liquibase.statement.ForeignKeyConstraint) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) ColumnConstraint(liquibase.statement.ColumnConstraint) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Sql(liquibase.sql.Sql) UnparsedSql(liquibase.sql.UnparsedSql)

Example 3 with AddColumnStatement

use of liquibase.statement.core.AddColumnStatement 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 AddColumnStatement

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

the class AddColumnGeneratorTest method testAddColumnAfter.

@Test
public void testAddColumnAfter() {
    AddColumnStatement statement = new AddColumnStatement(null, null, TABLE_NAME, COLUMN_NAME, COLUMN_TYPE, null);
    statement.setAddAfterColumn("column_after");
    assertFalse(generatorUnderTest.validate(statement, new MySQLDatabase(), new MockSqlGeneratorChain()).hasErrors());
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) AddColumnStatement(liquibase.statement.core.AddColumnStatement) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Test(org.junit.Test) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest)

Example 5 with AddColumnStatement

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

the class AddColumnGeneratorTest method testAddMultipleColumnsMySql.

@Test
public void testAddMultipleColumnsMySql() {
    AddColumnStatement columns = new AddColumnStatement(new AddColumnStatement(null, null, TABLE_NAME, "column1", "INT", null, new NotNullConstraint()), new AddColumnStatement(null, null, TABLE_NAME, "column2", "INT", null, new NotNullConstraint()));
    assertFalse(generatorUnderTest.validate(columns, new MySQLDatabase(), new MockSqlGeneratorChain()).hasErrors());
    Sql[] sql = generatorUnderTest.generateSql(columns, new MySQLDatabase(), new MockSqlGeneratorChain());
    assertEquals(1, sql.length);
    assertEquals("ALTER TABLE " + TABLE_NAME + " ADD column1 INT NOT NULL, ADD column2 INT NOT NULL", sql[0].toSql());
    assertEquals("[DEFAULT, table_name, table_name.column1, table_name.column2]", String.valueOf(sql[0].getAffectedDatabaseObjects()));
}
Also used : MySQLDatabase(liquibase.database.core.MySQLDatabase) AddColumnStatement(liquibase.statement.core.AddColumnStatement) NotNullConstraint(liquibase.statement.NotNullConstraint) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Sql(liquibase.sql.Sql) Test(org.junit.Test) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest)

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