Search in sources :

Example 1 with SqlGeneratorFactory

use of liquibase.sqlgenerator.SqlGeneratorFactory 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

AddColumnConfig (liquibase.change.AddColumnConfig)1 ConstraintsConfig (liquibase.change.ConstraintsConfig)1 AddColumnChange (liquibase.change.core.AddColumnChange)1 Sql (liquibase.sql.Sql)1 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)1 SqlGeneratorFactory (liquibase.sqlgenerator.SqlGeneratorFactory)1 SqlStatement (liquibase.statement.SqlStatement)1 Test (org.junit.Test)1