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 ORCID-Source by ORCID.
the class AdministrativeChangesOptionChangeTask method execute.
@Override
public void execute(Database database) throws CustomChangeException {
LOGGER.info("Running...");
final JdbcConnection conn = (JdbcConnection) database.getConnection();
try (PreparedStatement selectStatement = conn.prepareStatement(SELECT_SQL);
PreparedStatement updateStatement = conn.prepareStatement(UPDATE_SQL)) {
boolean done = false;
conn.setAutoCommit(false);
while (!done) {
LOGGER.info("Getting next batch...");
done = true;
ResultSet resultsSet = selectStatement.executeQuery();
while (resultsSet.next()) {
done = false;
String orcid = resultsSet.getString(1);
LOGGER.debug("Processing orcid: {}", orcid);
updateStatement.setString(1, orcid);
updateStatement.addBatch();
}
updateStatement.executeBatch();
conn.commit();
}
} catch (DatabaseException | SQLException e) {
throw new CustomChangeException("Problem populating administrative changes option", e);
}
}
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 opennms by OpenNMS.
the class Migrator method migrate.
/**
* <p>migrate</p>
*
* @param migration a {@link org.opennms.core.schema.Migration} object.
* @throws org.opennms.core.schema.MigrationException if any.
*/
public void migrate(final Migration migration) throws MigrationException {
Connection connection = null;
DatabaseConnection dbConnection = null;
try {
connection = m_dataSource.getConnection();
dbConnection = new JdbcConnection(connection);
ResourceAccessor accessor = migration.getAccessor();
if (accessor == null)
accessor = new SpringResourceAccessor();
final Liquibase liquibase = new Liquibase(migration.getChangeLog(), accessor, dbConnection);
liquibase.setChangeLogParameter("install.database.admin.user", migration.getAdminUser());
liquibase.setChangeLogParameter("install.database.admin.password", migration.getAdminPassword());
liquibase.setChangeLogParameter("install.database.user", migration.getDatabaseUser());
liquibase.getDatabase().setDefaultSchemaName(migration.getSchemaName());
final String contexts = System.getProperty("opennms.contexts", "production");
liquibase.update(contexts);
} catch (final Throwable e) {
throw new MigrationException("unable to migrate the database", e);
} finally {
cleanUpDatabase(connection, dbConnection, null, null);
}
}
use of liquibase.database.jvm.JdbcConnection in project liquibase by liquibase.
the class DatabaseFactory method openConnection.
public DatabaseConnection openConnection(String url, String username, String password, String driver, String databaseClass, String driverPropertiesFile, String propertyProviderClass, ResourceAccessor resourceAccessor) throws DatabaseException {
if (url.startsWith("offline:")) {
return new OfflineConnection(url, resourceAccessor);
}
driver = StringUtils.trimToNull(driver);
if (driver == null) {
driver = DatabaseFactory.getInstance().findDefaultDriver(url);
}
try {
Driver driverObject;
DatabaseFactory databaseFactory = DatabaseFactory.getInstance();
if (databaseClass != null) {
databaseFactory.clearRegistry();
databaseFactory.register((Database) Class.forName(databaseClass, true, resourceAccessor.toClassLoader()).newInstance());
}
try {
if (driver == null) {
driver = databaseFactory.findDefaultDriver(url);
}
if (driver == null) {
throw new RuntimeException("Driver class was not specified and could not be determined from the url (" + url + ")");
}
driverObject = (Driver) Class.forName(driver, true, resourceAccessor.toClassLoader()).newInstance();
} catch (Exception e) {
throw new RuntimeException("Cannot find database driver: " + e.getMessage());
}
Properties driverProperties;
if (propertyProviderClass == null) {
driverProperties = new Properties();
} else {
driverProperties = (Properties) Class.forName(propertyProviderClass, true, resourceAccessor.toClassLoader()).newInstance();
}
if (username != null) {
driverProperties.put("user", username);
}
if (password != null) {
driverProperties.put("password", password);
}
if (null != driverPropertiesFile) {
File propertiesFile = new File(driverPropertiesFile);
if (propertiesFile.exists()) {
// System.out.println("Loading properties from the file:'" + driverPropertiesFile + "'");
FileInputStream inputStream = new FileInputStream(propertiesFile);
try {
driverProperties.load(inputStream);
} finally {
inputStream.close();
}
} else {
throw new RuntimeException("Can't open JDBC Driver specific properties from the file: '" + driverPropertiesFile + "'");
}
}
// System.out.println("Properties:");
// for (Map.Entry entry : driverProperties.entrySet()) {
// System.out.println("Key:'"+entry.getKey().toString()+"' Value:'"+entry.getValue().toString()+"'");
// }
// System.out.println("Connecting to the URL:'"+url+"' using driver:'"+driverObject.getClass().getName()+"'");
Connection connection = driverObject.connect(url, driverProperties);
// System.out.println("Connection has been created");
if (connection == null) {
throw new DatabaseException("Connection could not be created to " + url + " with driver " + driverObject.getClass().getName() + ". Possibly the wrong driver for the given database URL");
}
return new JdbcConnection(connection);
} catch (Exception e) {
throw new DatabaseException(e);
}
}
Aggregations