use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class SqlPrecondition method check.
@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
DatabaseConnection connection = database.getConnection();
try {
String result = (String) ExecutorService.getInstance().getExecutor(database).queryForObject(new RawSqlStatement(getSql().replaceFirst(";$", "")), String.class);
if (result == null) {
throw new PreconditionFailedException("No rows returned from SQL Precondition", changeLog, this);
}
String expectedResult = getExpectedResult();
if (!expectedResult.equals(result)) {
throw new PreconditionFailedException("SQL Precondition failed. Expected '" + expectedResult + "' got '" + result + "'", changeLog, this);
}
} catch (DatabaseException e) {
throw new PreconditionErrorException(e, changeLog, this);
}
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class DatabaseType method createDatabase.
public Database createDatabase(ClassLoader classLoader) {
logParameters();
validateParameters();
try {
DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
if (databaseClass != null) {
Database databaseInstance = (Database) ClasspathUtils.newInstance(databaseClass, classLoader, Database.class);
databaseFactory.register(databaseInstance);
}
DatabaseConnection jdbcConnection;
if (getUrl().startsWith("offline:")) {
jdbcConnection = new OfflineConnection(getUrl(), new ClassLoaderResourceAccessor(classLoader));
} else {
Driver driver = (Driver) ClasspathUtils.newInstance(getDriver(), classLoader, Driver.class);
if (driver == null) {
throw new BuildException("Unable to create Liquibase Database instance. Could not instantiate the JDBC driver.");
}
Properties connectionProps = new Properties();
String user = getUser();
if (user != null && !user.isEmpty()) {
connectionProps.setProperty(USER, user);
}
String password = getPassword();
if (password != null && !password.isEmpty()) {
connectionProps.setProperty(PASSWORD, password);
}
ConnectionProperties connectionProperties = getConnectionProperties();
if (connectionProperties != null) {
connectionProps.putAll(connectionProperties.buildProperties());
}
Connection connection = driver.connect(getUrl(), connectionProps);
if (connection == null) {
throw new BuildException("Unable to create Liquibase Database instance. Could not connect to the database.");
}
jdbcConnection = new JdbcConnection(connection);
}
Database database = databaseFactory.findCorrectDatabaseImplementation(jdbcConnection);
String schemaName = getDefaultSchemaName();
if (schemaName != null) {
database.setDefaultSchemaName(schemaName);
}
String catalogName = getDefaultCatalogName();
if (catalogName != null) {
database.setDefaultCatalogName(catalogName);
}
String currentDateTimeFunction = getCurrentDateTimeFunction();
if (currentDateTimeFunction != null) {
database.setCurrentDateTimeFunction(currentDateTimeFunction);
}
database.setOutputDefaultSchema(isOutputDefaultSchema());
database.setOutputDefaultCatalog(isOutputDefaultCatalog());
String liquibaseSchemaName = getLiquibaseSchemaName();
if (liquibaseSchemaName != null) {
database.setLiquibaseSchemaName(liquibaseSchemaName);
}
String liquibaseCatalogName = getLiquibaseCatalogName();
if (liquibaseCatalogName != null) {
database.setLiquibaseCatalogName(liquibaseCatalogName);
}
String databaseChangeLogTableName = getDatabaseChangeLogTableName();
if (databaseChangeLogTableName != null) {
database.setDatabaseChangeLogTableName(databaseChangeLogTableName);
}
String databaseChangeLogLockTableName = getDatabaseChangeLogLockTableName();
if (databaseChangeLogLockTableName != null) {
database.setDatabaseChangeLogLockTableName(databaseChangeLogLockTableName);
}
String liquibaseTablespaceName = getLiquibaseTablespaceName();
if (liquibaseTablespaceName != null) {
database.setLiquibaseTablespaceName(liquibaseTablespaceName);
}
return database;
} catch (SQLException e) {
throw new BuildException("Unable to create Liquibase database instance. A JDBC error occurred. " + e.toString(), e);
} catch (DatabaseException e) {
throw new BuildException("Unable to create Liquibase database instance. " + e.toString(), e);
}
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class PostgresDatabase method getSearchPaths.
/*
* Get the current search paths
*/
private List<String> getSearchPaths() {
List<String> searchPaths = null;
try {
DatabaseConnection con = getConnection();
if (con != null) {
String searchPathResult = (String) ExecutorService.getInstance().getExecutor(this).queryForObject(new RawSqlStatement("SHOW search_path"), String.class);
if (searchPathResult != null) {
String[] dirtySearchPaths = searchPathResult.split("\\,");
searchPaths = new ArrayList<String>();
for (String searchPath : dirtySearchPaths) {
searchPath = searchPath.trim();
// Ensure there is consistency ..
if (searchPath.equals("\"$user\"")) {
searchPath = "$user";
}
searchPaths.add(searchPath);
}
}
}
} catch (Exception e) {
// TODO: Something?
e.printStackTrace();
LogFactory.getLogger().severe("Failed to get default catalog name from postgres", e);
}
return searchPaths;
}
use of liquibase.database.DatabaseConnection 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.database.DatabaseConnection in project liquibase by liquibase.
the class HsqlDatabaseTest method testUsingOracleSyntax.
public void testUsingOracleSyntax() {
HsqlDatabase database = new HsqlDatabase();
DatabaseConnection conn = mock(DatabaseConnection.class);
when(conn.getURL()).thenReturn("jdbc:hsqldb:mem:testdb;sql.syntax_ora=true;sql.enforce_names=true");
database.setConnection(conn);
assertTrue("Using oracle syntax", database.isUsingOracleSyntax());
}
Aggregations