Search in sources :

Example 21 with SqlStatement

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());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) SqlStatement(liquibase.statement.SqlStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) DatabaseException(liquibase.exception.DatabaseException) Test(org.junit.Test)

Example 22 with SqlStatement

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);
}
Also used : SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) Column(liquibase.structure.core.Column) ChangeSet(liquibase.changelog.ChangeSet)

Example 23 with SqlStatement

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());
}
Also used : DropColumnChange(liquibase.change.core.DropColumnChange) SqlStatement(liquibase.statement.SqlStatement) MockDatabase(liquibase.sdk.database.MockDatabase) DropColumnStatement(liquibase.statement.core.DropColumnStatement) Test(org.junit.Test)

Example 24 with SqlStatement

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());
}
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 25 with SqlStatement

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());
}
Also used : AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) SqlStatement(liquibase.statement.SqlStatement) MockExecutor(liquibase.sdk.executor.MockExecutor) ArrayList(java.util.ArrayList) DropTableStatement(liquibase.statement.core.DropTableStatement) SqlVisitor(liquibase.sql.visitor.SqlVisitor) AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) Test(org.junit.Test)

Aggregations

SqlStatement (liquibase.statement.SqlStatement)49 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)9 Column (liquibase.structure.core.Column)8 DB2Database (liquibase.database.core.DB2Database)7 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)7 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 DatabaseException (liquibase.exception.DatabaseException)6 Database (liquibase.database.Database)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 Sql (liquibase.sql.Sql)5 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UpdateStatement (liquibase.statement.core.UpdateStatement)5 Index (liquibase.structure.core.Index)5 Change (liquibase.change.Change)4 ColumnConfig (liquibase.change.ColumnConfig)4 AddColumnChange (liquibase.change.core.AddColumnChange)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)3