use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class H2Database method setConnection.
@Override
public void setConnection(DatabaseConnection conn) {
Connection sqlConn = null;
if (!(conn instanceof OfflineConnection)) {
try {
if (conn instanceof JdbcConnection) {
Method wrappedConn = conn.getClass().getMethod("getWrappedConnection");
wrappedConn.setAccessible(true);
sqlConn = (Connection) wrappedConn.invoke(conn);
}
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
}
if (sqlConn != null) {
Statement statement = null;
ResultSet resultSet = null;
try {
statement = sqlConn.createStatement();
resultSet = statement.executeQuery("SELECT SCHEMA()");
String schemaName = null;
if (resultSet.next()) {
schemaName = resultSet.getString(1);
}
if (schemaName != null) {
this.connectionSchemaName = schemaName;
}
} catch (SQLException e) {
Scope.getCurrentScope().getLog(getClass()).info("Could not read current schema name: " + e.getMessage());
} finally {
JdbcUtil.close(resultSet, statement);
}
}
}
super.setConnection(conn);
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class CreateSequenceGeneratorTest method postgresDatabaseSupportAsStructureByVersion.
@Test
public void postgresDatabaseSupportAsStructureByVersion() throws Exception {
DatabaseConnection dbConnection = mock(DatabaseConnection.class);
ValidationErrors errors;
PostgresDatabase postgresDatabase = spy(new PostgresDatabase());
postgresDatabase.setConnection(dbConnection);
doReturn(SEQUENCE_NAME).when(postgresDatabase).escapeSequenceName(CATALOG_NAME, SCHEMA_NAME, SEQUENCE_NAME);
// verify that for version < 10 validate() method returns error
when(dbConnection.getDatabaseMajorVersion()).thenReturn(9);
when(dbConnection.getDatabaseMinorVersion()).thenReturn(6);
CreateSequenceStatement createSequenceStatement = createSampleSqlStatement();
createSequenceStatement.setStartValue(new BigInteger("1"));
createSequenceStatement.setDataType("int");
errors = new CreateSequenceGenerator().validate(createSequenceStatement, postgresDatabase, new MockSqlGeneratorChain());
assertThat(errors.getErrorMessages()).contains("AS is not allowed on postgresql");
// verify that if no version is available the validate() method passes
reset(dbConnection);
when(dbConnection.getDatabaseMajorVersion()).thenThrow(DatabaseException.class);
errors = new CreateSequenceGenerator().validate(createSequenceStatement, postgresDatabase, new MockSqlGeneratorChain());
assertThat(errors.getErrorMessages()).isEmpty();
// verify that for version >= 10 the validate() method passes
reset(dbConnection);
when(dbConnection.getDatabaseMajorVersion()).thenReturn(10);
when(dbConnection.getDatabaseMinorVersion()).thenReturn(0);
errors = new CreateSequenceGenerator().validate(createSequenceStatement, postgresDatabase, new MockSqlGeneratorChain());
assertThat(errors.getErrorMessages()).isEmpty();
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class Liquibase method outputHeader.
public void outputHeader(String message) throws DatabaseException {
Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("logging", database);
executor.comment("*********************************************************************");
executor.comment(message);
executor.comment("*********************************************************************");
executor.comment("Change Log: " + changeLogFile);
executor.comment("Ran at: " + DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date()));
DatabaseConnection connection = getDatabase().getConnection();
if (connection != null) {
executor.comment("Against: " + connection.getConnectionUserName() + "@" + connection.getURL());
}
executor.comment("Liquibase version: " + LiquibaseUtil.getBuildVersionInfo());
executor.comment("*********************************************************************" + StreamUtil.getLineSeparator());
if ((database instanceof MSSQLDatabase) && (database.getDefaultCatalogName() != null)) {
executor.execute(new RawSqlStatement("USE " + database.escapeObjectName(database.getDefaultCatalogName(), Catalog.class) + ";"));
}
}
use of liquibase.database.DatabaseConnection in project liquibase by liquibase.
the class LiquibaseTest method constructor_createDatabaseInstanceFromConnection.
@Test
public void constructor_createDatabaseInstanceFromConnection() throws LiquibaseException {
DatabaseConnection databaseConnection = mock(DatabaseConnection.class);
Database database = mockDatabase;
try {
DatabaseFactory.setInstance(mock(DatabaseFactory.class));
when(DatabaseFactory.getInstance().findCorrectDatabaseImplementation(databaseConnection)).thenReturn(database);
Liquibase liquibase = new Liquibase("com/example/test.xml", mockResourceAccessor, databaseConnection);
assertSame("Liquibase constructor passing connection did not find the correct database implementation", database, liquibase.getDatabase());
} finally {
DatabaseFactory.reset();
}
}
use of liquibase.database.DatabaseConnection in project libresonic by Libresonic.
the class SpringLiquibase method createDatabase.
@Override
protected Database createDatabase(Connection c, ResourceAccessor resourceAccessor) throws DatabaseException {
DatabaseConnection liquibaseConnection;
if (c == null) {
log.warning("Null connection returned by liquibase datasource. Using offline unknown database");
liquibaseConnection = new OfflineConnection("offline:unknown", resourceAccessor);
} else {
liquibaseConnection = new JdbcConnection(c);
}
DatabaseFactory factory = DatabaseFactory.getInstance();
overrideHsqlDbImplementation(factory);
Database database = factory.findCorrectDatabaseImplementation(liquibaseConnection);
if (StringUtils.trimToNull(this.defaultSchema) != null) {
database.setDefaultSchemaName(this.defaultSchema);
}
return database;
}
Aggregations