Search in sources :

Example 1 with JdbcExecutor

use of liquibase.executor.jvm.JdbcExecutor in project liquibase by liquibase.

the class CreateProcedureGenerator method surroundWithSchemaSets.

/**
 * Convenience method for when the schemaName is set but we don't want to parse the body
 */
public static void surroundWithSchemaSets(List<Sql> sql, String schemaName, Database database) {
    if ((StringUtil.trimToNull(schemaName) != null) && !ChangeLogParserConfiguration.USE_PROCEDURE_SCHEMA.getCurrentValue()) {
        String defaultSchema = database.getDefaultSchemaName();
        if (database instanceof OracleDatabase) {
            sql.add(0, new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(schemaName, Schema.class)));
            sql.add(new UnparsedSql("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(defaultSchema, Schema.class)));
        } else if (database instanceof AbstractDb2Database) {
            sql.add(0, new UnparsedSql("SET CURRENT SCHEMA " + schemaName));
            sql.add(new UnparsedSql("SET CURRENT SCHEMA " + defaultSchema));
        } else if (database instanceof PostgresDatabase) {
            final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", database);
            String originalSearchPath = null;
            if (executor instanceof JdbcExecutor) {
                try {
                    originalSearchPath = executor.queryForObject(new RawSqlStatement("SHOW SEARCH_PATH"), String.class);
                } catch (Throwable e) {
                    Scope.getCurrentScope().getLog(CreateProcedureGenerator.class).warning("Cannot get search_path", e);
                }
            }
            if (originalSearchPath == null) {
                originalSearchPath = defaultSchema;
            }
            if (!originalSearchPath.equals(schemaName) && !originalSearchPath.startsWith(schemaName + ",") && !originalSearchPath.startsWith("\"" + schemaName + "\",")) {
                if (database instanceof EnterpriseDBDatabase) {
                    sql.add(0, new UnparsedSql("ALTER SESSION SET SEARCH_PATH TO " + database.escapeObjectName(defaultSchema, Schema.class) + ", " + originalSearchPath));
                    sql.add(new UnparsedSql("ALTER SESSION SET CURRENT SCHEMA " + originalSearchPath));
                } else {
                    sql.add(0, new UnparsedSql("SET SEARCH_PATH TO " + database.escapeObjectName(schemaName, Schema.class) + ", " + originalSearchPath));
                    sql.add(new UnparsedSql("SET CURRENT SCHEMA " + originalSearchPath));
                }
            }
        }
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) JdbcExecutor(liquibase.executor.jvm.JdbcExecutor) Executor(liquibase.executor.Executor) UnparsedSql(liquibase.sql.UnparsedSql) Schema(liquibase.structure.core.Schema) ExecutorService(liquibase.executor.ExecutorService) JdbcExecutor(liquibase.executor.jvm.JdbcExecutor)

Example 2 with JdbcExecutor

use of liquibase.executor.jvm.JdbcExecutor in project liquibase by liquibase.

the class DatabaseTestTemplate method test.

private void test(DatabaseTest test, Set<Database> databasesToTestOn) throws Exception {
    for (Database database : databasesToTestOn) {
        if (database instanceof SQLiteDatabase) {
            //todo: find how to get tests to run correctly on SQLite
            continue;
        }
        JdbcExecutor writeExecutor = new JdbcExecutor();
        writeExecutor.setDatabase(database);
        ExecutorService.getInstance().setExecutor(database, writeExecutor);
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        lockService.reset();
        if (database.getConnection() != null) {
            lockService.forceReleaseLock();
        }
        try {
            test.performTest(database);
        } catch (ComparisonFailure e) {
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            ComparisonFailure newError = new ComparisonFailure(newMessage, e.getExpected(), e.getActual());
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (AssertionError e) {
            e.printStackTrace();
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            AssertionError newError = new AssertionError(newMessage);
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (MigrationFailedException e) {
            e.printStackTrace();
            String newMessage = "Database Test Failure on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            AssertionError newError = new AssertionError(newMessage);
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        } catch (Exception e) {
            e.printStackTrace();
            String newMessage = "Database Test Exception on " + database;
            if (e.getMessage() != null) {
                newMessage += ": " + e.getMessage();
            }
            Exception newError = e.getClass().getConstructor(String.class).newInstance(newMessage);
            if (e.getCause() == null) {
                newError.setStackTrace(e.getStackTrace());
            } else {
                newError.setStackTrace(e.getCause().getStackTrace());
            }
            throw newError;
        } finally {
            if (database.getConnection() != null && !database.getAutoCommitMode()) {
                database.rollback();
            }
        }
    }
}
Also used : LockService(liquibase.lockservice.LockService) MigrationFailedException(liquibase.exception.MigrationFailedException) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) ComparisonFailure(org.junit.ComparisonFailure) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) Database(liquibase.database.Database) JdbcExecutor(liquibase.executor.jvm.JdbcExecutor) MigrationFailedException(liquibase.exception.MigrationFailedException)

Aggregations

JdbcExecutor (liquibase.executor.jvm.JdbcExecutor)2 Database (liquibase.database.Database)1 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)1 MigrationFailedException (liquibase.exception.MigrationFailedException)1 Executor (liquibase.executor.Executor)1 ExecutorService (liquibase.executor.ExecutorService)1 LockService (liquibase.lockservice.LockService)1 UnparsedSql (liquibase.sql.UnparsedSql)1 RawSqlStatement (liquibase.statement.core.RawSqlStatement)1 Schema (liquibase.structure.core.Schema)1 ComparisonFailure (org.junit.ComparisonFailure)1