use of liquibase.database.jvm.JdbcConnection in project dropwizard by dropwizard.
the class AbstractLiquibaseCommand method createDatabase.
private Database createDatabase(ManagedDataSource dataSource, Namespace namespace) throws SQLException, LiquibaseException {
final DatabaseConnection conn = new JdbcConnection(dataSource.getConnection());
final Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(conn);
final String catalogName = namespace.getString("catalog");
final String schemaName = namespace.getString("schema");
if (database.supportsCatalogs() && catalogName != null) {
database.setDefaultCatalogName(catalogName);
database.setOutputDefaultCatalog(true);
}
if (database.supportsSchemas() && schemaName != null) {
database.setDefaultSchemaName(schemaName);
database.setOutputDefaultSchema(true);
}
return database;
}
use of liquibase.database.jvm.JdbcConnection in project spring-boot by spring-projects.
the class LiquibaseEndpoint method invoke.
@Override
public List<LiquibaseReport> invoke() {
List<LiquibaseReport> reports = new ArrayList<>();
DatabaseFactory factory = DatabaseFactory.getInstance();
StandardChangeLogHistoryService service = new StandardChangeLogHistoryService();
for (Map.Entry<String, SpringLiquibase> entry : this.liquibases.entrySet()) {
try {
DataSource dataSource = entry.getValue().getDataSource();
JdbcConnection connection = new JdbcConnection(dataSource.getConnection());
try {
Database database = factory.findCorrectDatabaseImplementation(connection);
reports.add(new LiquibaseReport(entry.getKey(), service.queryDatabaseChangeLogTable(database)));
} finally {
connection.close();
}
} catch (Exception ex) {
throw new IllegalStateException("Unable to get Liquibase changelog", ex);
}
}
return reports;
}
use of liquibase.database.jvm.JdbcConnection 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) {
LogFactory.getLogger().info("Could not read current schema name: " + e.getMessage());
} finally {
JdbcUtils.close(resultSet, statement);
}
}
}
super.setConnection(conn);
}
use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class LiquibaseServletListener method executeUpdate.
/**
* Executes the Liquibase update.
*/
private void executeUpdate(ServletContext servletContext, InitialContext ic) throws NamingException, SQLException, LiquibaseException {
setDataSource((String) servletValueContainer.getValue(LIQUIBASE_DATASOURCE));
if (getDataSource() == null) {
throw new RuntimeException("Cannot run Liquibase, " + LIQUIBASE_DATASOURCE + " is not set");
}
setChangeLogFile((String) servletValueContainer.getValue(LIQUIBASE_CHANGELOG));
if (getChangeLogFile() == null) {
throw new RuntimeException("Cannot run Liquibase, " + LIQUIBASE_CHANGELOG + " is not set");
}
setContexts((String) servletValueContainer.getValue(LIQUIBASE_CONTEXTS));
setLabels((String) servletValueContainer.getValue(LIQUIBASE_LABELS));
this.defaultSchema = StringUtils.trimToNull((String) servletValueContainer.getValue(LIQUIBASE_SCHEMA_DEFAULT));
Connection connection = null;
Database database = null;
try {
DataSource dataSource = (DataSource) ic.lookup(this.dataSourceName);
connection = dataSource.getConnection();
Thread currentThread = Thread.currentThread();
ClassLoader contextClassLoader = currentThread.getContextClassLoader();
ResourceAccessor threadClFO = new ClassLoaderResourceAccessor(contextClassLoader);
ResourceAccessor clFO = new ClassLoaderResourceAccessor();
ResourceAccessor fsFO = new FileSystemResourceAccessor();
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
database.setDefaultSchemaName(getDefaultSchema());
Liquibase liquibase = new Liquibase(getChangeLogFile(), new CompositeResourceAccessor(clFO, fsFO, threadClFO), database);
@SuppressWarnings("unchecked") Enumeration<String> initParameters = servletContext.getInitParameterNames();
while (initParameters.hasMoreElements()) {
String name = initParameters.nextElement().trim();
if (name.startsWith(LIQUIBASE_PARAMETER + ".")) {
liquibase.setChangeLogParameter(name.substring(LIQUIBASE_PARAMETER.length() + 1), servletValueContainer.getValue(name));
}
}
liquibase.update(new Contexts(getContexts()), new LabelExpression(getLabels()));
} finally {
if (database != null) {
database.close();
} else if (connection != null) {
connection.close();
}
}
}
use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class SpringLiquibase method getDatabaseProductName.
public String getDatabaseProductName() throws DatabaseException {
Connection connection = null;
Database database = null;
String name = "unknown";
try {
connection = getDataSource().getConnection();
database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
name = database.getDatabaseProductName();
} catch (SQLException e) {
throw new DatabaseException(e);
} finally {
if (database != null) {
database.close();
} else if (connection != null) {
try {
if (!connection.getAutoCommit()) {
connection.rollback();
}
connection.close();
} catch (Exception e) {
log.warning("problem closing connection", e);
}
}
}
return name;
}
Aggregations