Search in sources :

Example 1 with AddColumnChange

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

the class JsonChangeLogSerializerTest method serialize_changeSet.

@Test
public void serialize_changeSet() {
    // given
    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));
    // Get the Date object for 1970-01-01T00:00:00 in the current time zone.
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(0);
    cal.set(1970, 0, 1, 0, 0, 0);
    addColumnChange.addColumn((AddColumnConfig) new AddColumnConfig().setName("col2").setDefaultValueDate(cal.getTime()));
    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.setPreconditions(newSamplePreconditions());
    changeSet.addChange(addColumnChange);
    // when
    String serializedJson = new JsonChangeLogSerializer().serialize(changeSet, true);
    // then
    assertEquals("{\n" + "  \"changeSet\": {\n" + "    \"id\": \"1\",\n" + "    \"author\": \"nvoxland\",\n" + "    \"objectQuotingStrategy\": \"LEGACY\",\n" + "    \"preconditions\": {\n" + "      \"preConditions\": {\n" + "        \"nestedPreconditions\": [\n" + "          {\n" + "            \"preConditions\": {\n" + "              \"onError\": \"WARN\",\n" + "              \"onFail\": \"CONTINUE\",\n" + "              \"onSqlOutput\": \"TEST\"\n" + "            }\n" + "          }]\n" + "        ,\n" + "        \"onError\": \"CONTINUE\",\n" + "        \"onFail\": \"MARK_RAN\",\n" + "        \"onSqlOutput\": \"FAIL\"\n" + "      }\n" + "    },\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:00\",\n" + "                \"name\": \"col2\"\n" + "              }\n" + "            },\n" + "            {\n" + "              \"column\": {\n" + "                \"defaultValueSequenceNext\": \"seq_me\",\n" + "                \"name\": \"col2\"\n" + "              }\n" + "            }]\n" + "          \n" + "        }\n" + "      }]\n" + "    \n" + "  }\n" + "}\n", serializedJson);
}
Also used : DatabaseFunction(liquibase.statement.DatabaseFunction) Calendar(java.util.Calendar) AddColumnConfig(liquibase.change.AddColumnConfig) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) AddColumnChange(liquibase.change.core.AddColumnChange) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 2 with AddColumnChange

use of liquibase.change.core.AddColumnChange 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 3 with AddColumnChange

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

the class AddColumnChangeTest method generateStatements_singleColumn_uniqueConstraintName.

@Test
public void generateStatements_singleColumn_uniqueConstraintName() {
    String myUniqueConstraintName = "my_unique_constraint";
    AddColumnChange change = new AddColumnChange();
    change.setTableName("my_table");
    AddColumnConfig column = new AddColumnConfig();
    column.setName("column1");
    column.setType("integer");
    ConstraintsConfig constraintsConfig = new ConstraintsConfig();
    constraintsConfig.setUnique(true);
    constraintsConfig.setUniqueConstraintName(myUniqueConstraintName);
    column.setConstraints(constraintsConfig);
    change.addColumn(column);
    SqlStatement[] statements = change.generateStatements(new MockDatabase());
    Assert.assertEquals(1, statements.length);
    Assert.assertTrue(statements[0] instanceof AddColumnStatement);
    AddColumnStatement stmt = (AddColumnStatement) statements[0];
    Assert.assertEquals(myUniqueConstraintName, stmt.getUniqueStatementName());
}
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 4 with AddColumnChange

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

the class AddColumnChangeTest method generateStatements_singleColumn_null_uniqueConstraintName.

@Test
public void generateStatements_singleColumn_null_uniqueConstraintName() {
    AddColumnChange change = new AddColumnChange();
    change.setTableName("my_table");
    AddColumnConfig column = new AddColumnConfig();
    column.setName("column1");
    column.setType("integer");
    ConstraintsConfig constraintsConfig = new ConstraintsConfig();
    constraintsConfig.setUnique(true);
    column.setConstraints(constraintsConfig);
    change.addColumn(column);
    SqlStatement[] statements = change.generateStatements(new MockDatabase());
    Assert.assertEquals(1, statements.length);
    Assert.assertTrue(statements[0] instanceof AddColumnStatement);
    AddColumnStatement stmt = (AddColumnStatement) statements[0];
    Assert.assertNull(stmt.getUniqueStatementName());
}
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 5 with AddColumnChange

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

the class LiquibaseHelperTest method testNotIgnoreAddColumn.

@Test
public void testNotIgnoreAddColumn() {
    final AddColumnChange change = new AddColumnChange();
    final ChangeSet changeSet = new ChangeSet(null);
    changeSet.addChange(change);
    assertFalse(LiquibaseHelper.isIgnoredChangeSet(changeSet));
}
Also used : AddColumnChange(liquibase.change.core.AddColumnChange) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

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