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));
}
}
}
}
}
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();
}
}
}
}
Aggregations