Search in sources :

Example 1 with RowMapper

use of org.flywaydb.core.internal.util.jdbc.RowMapper in project flyway by flyway.

the class MetaDataTableImpl method findAppliedMigrations.

/**
     * Retrieve the applied migrations from the metadata table.
     *
     * @param migrationTypes The specific migration types to look for. (Optional) None means find all migrations.
     * @return The applied migrations.
     */
private List<AppliedMigration> findAppliedMigrations(MigrationType... migrationTypes) {
    if (!table.exists()) {
        return new ArrayList<AppliedMigration>();
    }
    createIfNotExists();
    int minInstalledRank = cache.isEmpty() ? -1 : cache.getLast().getInstalledRank();
    String query = "SELECT " + dbSupport.quote("installed_rank") + "," + dbSupport.quote("version") + "," + dbSupport.quote("description") + "," + dbSupport.quote("type") + "," + dbSupport.quote("script") + "," + dbSupport.quote("checksum") + "," + dbSupport.quote("installed_on") + "," + dbSupport.quote("installed_by") + "," + dbSupport.quote("execution_time") + "," + dbSupport.quote("success") + " FROM " + table + " WHERE " + dbSupport.quote("installed_rank") + " > " + minInstalledRank;
    if (migrationTypes.length > 0) {
        query += " AND " + dbSupport.quote("type") + " IN (";
        for (int i = 0; i < migrationTypes.length; i++) {
            if (i > 0) {
                query += ",";
            }
            query += "'" + migrationTypes[i] + "'";
        }
        query += ")";
    }
    query += " ORDER BY " + dbSupport.quote("installed_rank");
    try {
        cache.addAll(jdbcTemplate.query(query, new RowMapper<AppliedMigration>() {

            public AppliedMigration mapRow(final ResultSet rs) throws SQLException {
                Integer checksum = rs.getInt("checksum");
                if (rs.wasNull()) {
                    checksum = null;
                }
                return new AppliedMigration(rs.getInt("installed_rank"), rs.getString("version") != null ? MigrationVersion.fromVersion(rs.getString("version")) : null, rs.getString("description"), MigrationType.valueOf(rs.getString("type")), rs.getString("script"), checksum, rs.getTimestamp("installed_on"), rs.getString("installed_by"), rs.getInt("execution_time"), rs.getBoolean("success"));
            }
        }));
        return cache;
    } catch (SQLException e) {
        throw new FlywaySqlException("Error while retrieving the list of applied migrations from metadata table " + table, e);
    }
}
Also used : FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) RowMapper(org.flywaydb.core.internal.util.jdbc.RowMapper)

Aggregations

ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)1 RowMapper (org.flywaydb.core.internal.util.jdbc.RowMapper)1