use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AbstractSQLChangeTest method generateStatements_willCallNativeSqlIfPossible.
@Test
public void generateStatements_willCallNativeSqlIfPossible() throws DatabaseException {
ExampleAbstractSQLChange change = new ExampleAbstractSQLChange("SOME SQL");
Database database = mock(Database.class);
DatabaseConnection connection = mock(DatabaseConnection.class);
when(database.getConnection()).thenReturn(connection);
when(connection.nativeSQL("SOME SQL")).thenReturn("SOME NATIVE SQL");
SqlStatement[] statements = change.generateStatements(database);
assertEquals(1, statements.length);
assertEquals("SOME NATIVE SQL", ((RawSqlStatement) statements[0]).getSql());
//If there is an error, it falls back to passed SQL
when(connection.nativeSQL("SOME SQL")).thenThrow(new DatabaseException("Testing exception"));
statements = change.generateStatements(database);
assertEquals(1, statements.length);
assertEquals("SOME SQL", ((RawSqlStatement) statements[0]).getSql());
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class UpdateChangeSetChecksumGenerator method generateSql.
@Override
public Sql[] generateSql(UpdateChangeSetChecksumStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
ChangeSet changeSet = statement.getChangeSet();
SqlStatement runStatement = null;
runStatement = new UpdateStatement(database.getLiquibaseCatalogName(), database.getLiquibaseSchemaName(), database.getDatabaseChangeLogTableName()).addNewColumnValue("MD5SUM", changeSet.generateCheckSum().toString()).setWhereClause(database.escapeObjectName("ID", Column.class) + " = ? " + "AND " + database.escapeObjectName("AUTHOR", Column.class) + " = ? " + "AND " + database.escapeObjectName("FILENAME", Column.class) + " = ?").addWhereParameters(changeSet.getId(), changeSet.getAuthor(), changeSet.getFilePath());
return SqlGeneratorFactory.getInstance().generateSql(runStatement, database);
}
use of liquibase.statement.SqlStatement 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.statement.SqlStatement 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());
}
use of liquibase.statement.SqlStatement in project liquibase by liquibase.
the class AbstractJdbcDatabaseTest method executeRollbackStatements_WithStatementsOverload_ShouldIncludeAppendTextFromApplyToRollbackTrueVisitor.
@Test
public void executeRollbackStatements_WithStatementsOverload_ShouldIncludeAppendTextFromApplyToRollbackTrueVisitor() throws Exception {
Database database = getDatabase();
final MockExecutor mockExecutor = new MockExecutor();
ExecutorService.getInstance().setExecutor(database, mockExecutor);
final List<SqlVisitor> sqlVisitors = new ArrayList<SqlVisitor>();
final SqlStatement dropTableStatement = new DropTableStatement(null, null, "test_table", false);
final AppendSqlVisitor appendSqlVisitor = new AppendSqlVisitor();
appendSqlVisitor.setApplyToRollback(true);
appendSqlVisitor.setValue(" SHOULD BE APPENDED");
sqlVisitors.add(appendSqlVisitor);
database.executeRollbackStatements(new SqlStatement[] { dropTableStatement }, sqlVisitors);
assertEquals("DROP TABLE test_table SHOULD BE APPENDED;", mockExecutor.getRanSql().trim());
}
Aggregations