use of liquibase.database.core.MockDatabase in project liquibase by liquibase.
the class DropColumnChangeTest method generateStatements_multipleColumns.
@Test
public void generateStatements_multipleColumns() {
DropColumnChange change = new DropColumnChange();
ColumnConfig column1 = new ColumnConfig();
column1.setName("column1");
change.addColumn(column1);
ColumnConfig column2 = new ColumnConfig();
column2.setName("column2");
change.addColumn(column2);
SqlStatement[] statements = change.generateStatements(new MockDatabase());
Assert.assertEquals(1, statements.length);
Assert.assertTrue(statements[0] instanceof DropColumnStatement);
DropColumnStatement stmt = (DropColumnStatement) statements[0];
Assert.assertTrue(stmt.isMultiple());
Assert.assertEquals(2, stmt.getColumns().size());
}
use of liquibase.database.core.MockDatabase 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());
}
use of liquibase.database.core.MockDatabase in project liquibase by liquibase.
the class VerifyChangeClassesTest method compareGeneratedSqlWithExpectedSqlForMinimalChangesets.
@Test
public void compareGeneratedSqlWithExpectedSqlForMinimalChangesets() throws Exception {
ChangeFactory changeFactory = Scope.getCurrentScope().getSingleton(ChangeFactory.class);
for (String changeName : changeFactory.getDefinedChanges()) {
if ("addDefaultValue".equals(changeName)) {
// need to better handle strange "one of defaultValue* is required" logic
continue;
}
if ("createProcedure".equals(changeName)) {
// need to better handle strange "one of path or body is required" logic
continue;
}
if ("changeWithNestedTags".equals(changeName) || "sampleChange".equals(changeName) || "output".equals(changeName) || "tagDatabase".equals(changeName)) {
// not a real change
continue;
}
for (Database database : DatabaseFactory.getInstance().getImplementedDatabases()) {
if (database.getShortName() == null || database instanceof MockDatabase) {
continue;
}
TestState state = new TestState(name.getMethodName(), changeName, database.getShortName(), TestState.Type.SQL);
state.addComment("Database: " + database.getShortName());
Change change = changeFactory.create(changeName);
if (!change.supports(database)) {
continue;
}
if (change.generateStatementsVolatile(database)) {
continue;
}
ChangeMetaData changeMetaData = Scope.getCurrentScope().getSingleton(ChangeFactory.class).getChangeMetaData(change);
// Prepare a list of required parameters, plus a few extra for complicated cases (e.g. where at least
// one of two parameters in a group is required.
TreeSet<String> requiredParams = new TreeSet<String>(changeMetaData.getRequiredParameters(database).keySet());
/* dropColumn allows either one column or a list of columns; we choose to test with a single column. */
if ("dropColumn".equalsIgnoreCase(changeName))
requiredParams.add("columnName");
/* When testing table and column remarks, do not test with an empty remark. */
if ("setColumnRemarks".equalsIgnoreCase(changeName))
requiredParams.add("remarks");
if ("setTableRemarks".equalsIgnoreCase(changeName))
requiredParams.add("remarks");
if ("createView".equalsIgnoreCase(changeName))
requiredParams.add("selectQuery");
/* ALTER SEQUENCE should change at least one property
* hsqldb/h2 do not support incrementBy. */
if ("alterSequence".equalsIgnoreCase(changeName)) {
if ("h2".equalsIgnoreCase(database.getShortName())) {
requiredParams.add("ordered");
} else if ("hsqldb".equalsIgnoreCase(database.getShortName())) {
requiredParams.add("minValue");
} else {
requiredParams.add("incrementBy");
}
}
if ("dropNotNullConstraint".equalsIgnoreCase(changeName)) {
if ("oracle".equalsIgnoreCase(database.getShortName())) {
// oracle must have either columnName or constraintName
requiredParams.add("columnName");
}
}
if (database instanceof Db2zDatabase && "addLookupTable".equalsIgnoreCase(changeName)) {
requiredParams.add("newColumnDataType");
}
// For every required parameter of the change, fetch an example value.
for (String paramName : requiredParams) {
ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName);
Object paramValue = param.getExampleValue(database);
String serializedValue;
serializedValue = formatParameter(paramValue);
state.addComment("Change Parameter: " + param.getParameterName() + "=" + serializedValue);
param.setValue(change, paramValue);
}
ValidationErrors errors = change.validate(database);
assertFalse("Validation errors for " + changeMetaData.getName() + " on " + database.getShortName() + ": " + errors.toString(), errors.hasErrors());
SqlStatement[] sqlStatements = {};
try {
sqlStatements = change.generateStatements(database);
} catch (UnexpectedLiquibaseException ex) {
if (ex.getCause() instanceof IOException) {
// Do nothing. I/O exceptions at this point come from the example value "my/path/file.sql"
// for the <sql> type change and can be safely ignored.
}
}
for (SqlStatement statement : sqlStatements) {
Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, database);
if (sql == null) {
Scope.getCurrentScope().getLog(getClass()).severe("Null sql for " + statement + " on " + database.getShortName());
} else {
for (Sql line : sql) {
String sqlLine = line.toSql();
assertFalse("Change " + changeMetaData.getName() + " contains 'null' for " + database.getShortName() + ": " + sqlLine, sqlLine.contains(" null "));
state.addValue(sqlLine + ";");
}
}
}
state.test();
}
}
}
use of liquibase.database.core.MockDatabase in project liquibase by liquibase.
the class MarkChangeSetRanGeneratorTest method generateSql_markRan.
@Test
public void generateSql_markRan() {
Sql[] sqls = new MarkChangeSetRanGenerator().generateSql(new MarkChangeSetRanStatement(new ChangeSet("1", "a", false, false, "c", null, null, null), ChangeSet.ExecType.MARK_RAN), new MockDatabase(), new MockSqlGeneratorChain());
assertEquals(1, sqls.length);
assertTrue(sqls[0].toSql(), sqls[0].toSql().contains("MARK_RAN"));
}
use of liquibase.database.core.MockDatabase in project liquibase by liquibase.
the class AddPrimaryKeyGenerator method validate.
@Override
public ValidationErrors validate(AddPrimaryKeyStatement addPrimaryKeyStatement, Database database, SqlGeneratorChain sqlGeneratorChain) {
ValidationErrors validationErrors = new ValidationErrors();
validationErrors.checkRequiredField("columnNames", addPrimaryKeyStatement.getColumnNames());
validationErrors.checkRequiredField("tableName", addPrimaryKeyStatement.getTableName());
if (addPrimaryKeyStatement.isClustered() != null) {
if (database instanceof PostgresDatabase) {
if (addPrimaryKeyStatement.isClustered() && addPrimaryKeyStatement.getConstraintName() == null) {
validationErrors.addError("Postgresql requires constraintName on addPrimaryKey when clustered=true");
}
} else if (database instanceof MSSQLDatabase || database instanceof MockDatabase) {
// clustered is fine
} else if (addPrimaryKeyStatement.isClustered()) {
validationErrors.addError("Cannot specify clustered=true on " + database.getShortName());
}
}
if (!((database instanceof OracleDatabase) || (database instanceof AbstractDb2Database))) {
validationErrors.checkDisallowedField("forIndexName", addPrimaryKeyStatement.getForIndexName(), database);
}
return validationErrors;
}
Aggregations