Search in sources :

Example 11 with JdbcConnection

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;
}
Also used : Database(liquibase.database.Database) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 12 with JdbcConnection

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);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) CustomChangeException(liquibase.exception.CustomChangeException) JdbcConnection(liquibase.database.jvm.JdbcConnection) PreparedStatement(java.sql.PreparedStatement) DatabaseException(liquibase.exception.DatabaseException)

Example 13 with JdbcConnection

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;
}
Also used : LiquibaseReport(org.springframework.boot.actuate.endpoint.LiquibaseEndpoint.LiquibaseReport) ArrayList(java.util.ArrayList) JdbcConnection(liquibase.database.jvm.JdbcConnection) StandardChangeLogHistoryService(liquibase.changelog.StandardChangeLogHistoryService) DataSource(javax.sql.DataSource) DatabaseFactory(liquibase.database.DatabaseFactory) SpringLiquibase(liquibase.integration.spring.SpringLiquibase) Database(liquibase.database.Database) Map(java.util.Map)

Example 14 with JdbcConnection

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);
    }
}
Also used : Liquibase(liquibase.Liquibase) ResourceAccessor(liquibase.resource.ResourceAccessor) Connection(java.sql.Connection) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection) DatabaseConnection(liquibase.database.DatabaseConnection) JdbcConnection(liquibase.database.jvm.JdbcConnection)

Example 15 with JdbcConnection

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);
    }
}
Also used : Connection(java.sql.Connection) JdbcConnection(liquibase.database.jvm.JdbcConnection) Driver(java.sql.Driver) JdbcConnection(liquibase.database.jvm.JdbcConnection) File(java.io.File) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) FileInputStream(java.io.FileInputStream)

Aggregations

JdbcConnection (liquibase.database.jvm.JdbcConnection)26 DatabaseException (liquibase.exception.DatabaseException)15 SQLException (java.sql.SQLException)13 OfflineConnection (liquibase.database.OfflineConnection)11 DatabaseConnection (liquibase.database.DatabaseConnection)10 Connection (java.sql.Connection)8 Database (liquibase.database.Database)8 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)6 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 Driver (java.sql.Driver)3 ArrayList (java.util.ArrayList)3 Liquibase (liquibase.Liquibase)3 DatabaseFactory (liquibase.database.DatabaseFactory)3 Method (java.lang.reflect.Method)2 CallableStatement (java.sql.CallableStatement)2 PreparedStatement (java.sql.PreparedStatement)2 DataSource (javax.sql.DataSource)2 LiquibaseException (liquibase.exception.LiquibaseException)2