Search in sources :

Example 6 with AddColumnChange

use of liquibase.change.core.AddColumnChange in project liquibase by liquibase.

the class AddColumnChangeTest method generateStatements_DB2_multipleColumns_single_reorg.

@Test
public void generateStatements_DB2_multipleColumns_single_reorg() {
    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 DB2Database());
    Assert.assertEquals(2, statements.length);
    Assert.assertTrue(statements[0] instanceof AddColumnStatement);
    AddColumnStatement stmt = (AddColumnStatement) statements[0];
    Assert.assertTrue(stmt.isMultiple());
    Assert.assertEquals(2, stmt.getColumns().size());
    Assert.assertTrue(statements[1] instanceof ReorganizeTableStatement);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 7 with AddColumnChange

use of liquibase.change.core.AddColumnChange 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.database.core.MockDatabase) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 8 with AddColumnChange

use of liquibase.change.core.AddColumnChange in project liquibase by liquibase.

the class MissingColumnChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Column column = (Column) missingObject;
    if (column.getRelation() instanceof View) {
        return null;
    }
    if (column.getRelation().getSnapshotId() == null) {
        // not an actual table, maybe an alias, maybe in a different schema. Don't fix it.
        return null;
    }
    AddColumnChange change = createAddColumnChange();
    change.setTableName(column.getRelation().getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(column.getRelation().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(column.getRelation().getSchema().getName());
    }
    AddColumnConfig columnConfig = createAddColumnConfig();
    columnConfig.setName(column.getName());
    String dataType = column.getType().toString();
    columnConfig.setType(dataType);
    MissingTableChangeGenerator.setDefaultValue(columnConfig, column, comparisonDatabase);
    Column.AutoIncrementInformation autoIncrementInfo = column.getAutoIncrementInformation();
    if (autoIncrementInfo != null) {
        columnConfig.setAutoIncrement(true);
        columnConfig.setGenerationType(autoIncrementInfo.getGenerationType());
        columnConfig.setDefaultOnNull(autoIncrementInfo.getDefaultOnNull());
    }
    if (column.getRemarks() != null) {
        columnConfig.setRemarks(column.getRemarks());
    }
    ConstraintsConfig constraintsConfig = columnConfig.getConstraints();
    if ((column.isNullable() != null) && !column.isNullable()) {
        if (constraintsConfig == null) {
            constraintsConfig = new ConstraintsConfig();
        }
        constraintsConfig.setNullable(false);
        constraintsConfig.setNotNullConstraintName(column.getAttribute("notNullConstraintName", String.class));
        if (!column.getValidateNullable()) {
            constraintsConfig.setValidateNullable(false);
        }
    }
    if (constraintsConfig != null) {
        columnConfig.setConstraints(constraintsConfig);
    }
    change.addColumn(columnConfig);
    return new Change[] { change };
}
Also used : Column(liquibase.structure.core.Column) ConstraintsConfig(liquibase.change.ConstraintsConfig) AddColumnConfig(liquibase.change.AddColumnConfig) Change(liquibase.change.Change) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnChange(liquibase.change.core.AddColumnChange) View(liquibase.structure.core.View)

Example 9 with AddColumnChange

use of liquibase.change.core.AddColumnChange in project liquibase by liquibase.

the class AddColumnGeneratorTest method testAddColumnWithNotNullConstraintAndValue.

@Test
public void testAddColumnWithNotNullConstraintAndValue() {
    AddColumnChange change = new AddColumnChange();
    change.setTableName(TABLE_NAME);
    AddColumnConfig column = new AddColumnConfig();
    column.setName("column1");
    column.setType("int8");
    column.setValueNumeric("0");
    column.setConstraints(new ConstraintsConfig().setNullable(false));
    change.addColumn(column);
    AddColumnConfig column2 = new AddColumnConfig();
    column2.setName("column2");
    column2.setType("boolean");
    column2.setValueBoolean("true");
    column2.setConstraints(new ConstraintsConfig().setNullable(false));
    change.addColumn(column2);
    SqlStatement[] statements = change.generateStatements(new MySQLDatabase());
    SqlGeneratorFactory instance = SqlGeneratorFactory.getInstance();
    Sql[] sql = instance.generateSql(statements, new MySQLDatabase());
    assertEquals(5, sql.length);
    assertEquals("ALTER TABLE table_name ADD column1 BIGINT NULL, ADD column2 BIT(1) NULL", sql[0].toSql());
    assertEquals("UPDATE table_name SET column1 = 0", sql[1].toSql());
    assertEquals("UPDATE table_name SET column2 = 1", sql[2].toSql());
    assertEquals("ALTER TABLE table_name MODIFY column1 BIGINT NOT NULL", sql[3].toSql());
    assertEquals("ALTER TABLE table_name MODIFY column2 BIT(1) NOT NULL", sql[4].toSql());
}
Also used : SqlStatement(liquibase.statement.SqlStatement) SqlGeneratorFactory(liquibase.sqlgenerator.SqlGeneratorFactory) ConstraintsConfig(liquibase.change.ConstraintsConfig) AddColumnConfig(liquibase.change.AddColumnConfig) AddColumnChange(liquibase.change.core.AddColumnChange) Sql(liquibase.sql.Sql) Test(org.junit.Test) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest)

Aggregations

AddColumnChange (liquibase.change.core.AddColumnChange)9 Test (org.junit.Test)8 SqlStatement (liquibase.statement.SqlStatement)6 AddColumnStatement (liquibase.statement.core.AddColumnStatement)5 AddColumnConfig (liquibase.change.AddColumnConfig)4 MockDatabase (liquibase.database.core.MockDatabase)3 ConstraintsConfig (liquibase.change.ConstraintsConfig)2 ChangeSet (liquibase.changelog.ChangeSet)2 Calendar (java.util.Calendar)1 Change (liquibase.change.Change)1 DB2Database (liquibase.database.core.DB2Database)1 DatabaseException (liquibase.exception.DatabaseException)1 Sql (liquibase.sql.Sql)1 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)1 SqlGeneratorFactory (liquibase.sqlgenerator.SqlGeneratorFactory)1 DatabaseFunction (liquibase.statement.DatabaseFunction)1 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)1 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)1 Column (liquibase.structure.core.Column)1 View (liquibase.structure.core.View)1