/** Creates the metatable if it doesn't exist, upgrades it if it does. */ private void createIfNotExists() { if (table.existsNoQuotes() || table.exists()) { if (!upgraded) { new MetaDataTableTo20FormatUpgrader(dbSupport, table, migrationResolver).upgrade(); new MetaDataTableTo202FormatUpgrader(dbSupport, table).upgrade(); upgraded = true; } return; } LOG.info("Creating Metadata table: " + table); final String source = new ClassPathResource(dbSupport.getScriptLocation() + "createMetaDataTable.sql") .loadAsString("UTF-8"); Map<String, String> placeholders = new HashMap<String, String>(); placeholders.put("schema", table.getSchema().getName()); placeholders.put("table", table.getName()); final String sourceNoPlaceholders = new PlaceholderReplacer(placeholders, "${", "}").replacePlaceholders(source); SqlScript sqlScript = new SqlScript(sourceNoPlaceholders, dbSupport); sqlScript.execute(jdbcTemplate); LOG.debug("Metadata table " + table + " created."); }
public List<AppliedMigration> allAppliedMigrations() { if (!table.existsNoQuotes() && !table.exists()) { return new ArrayList<AppliedMigration>(); } createIfNotExists(); String query = "SELECT " + dbSupport.quote("version_rank") + "," + 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 + " ORDER BY " + dbSupport.quote("version_rank"); try { return jdbcTemplate.query( query, new RowMapper<AppliedMigration>() { public AppliedMigration mapRow(final ResultSet rs) throws SQLException { return new AppliedMigration( rs.getInt("version_rank"), rs.getInt("installed_rank"), new MigrationVersion(rs.getString("version")), rs.getString("description"), MigrationType.valueOf(rs.getString("type")), rs.getString("script"), toInteger((Number) rs.getObject("checksum")), rs.getTimestamp("installed_on"), rs.getString("installed_by"), toInteger((Number) rs.getObject("execution_time")), rs.getBoolean("success")); } }); } catch (SQLException e) { throw new FlywayException( "Error while retrieving the list of applied migrations from metadata table " + table, e); } }
public void repair() { if (!table.existsNoQuotes() && !table.exists()) { LOG.info( "Repair of metadata table " + table + " not necessary. No failed migration detected."); return; } createIfNotExists(); try { int failedCount = jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM " + table + " WHERE " + dbSupport.quote("success") + "=" + dbSupport.getBooleanFalse()); if (failedCount == 0) { LOG.info( "Repair of metadata table " + table + " not necessary. No failed migration detected."); return; } } catch (SQLException e) { throw new FlywayException( "Unable to check the metadata table " + table + " for failed migrations", e); } StopWatch stopWatch = new StopWatch(); stopWatch.start(); try { jdbcTemplate.execute( "DELETE FROM " + table + " WHERE " + dbSupport.quote("success") + " = " + dbSupport.getBooleanFalse()); } catch (SQLException e) { throw new FlywayException("Unable to repair metadata table " + table, e); } stopWatch.stop(); LOG.info( "Metadata table " + table + " successfully repaired (execution time " + TimeFormat.format(stopWatch.getTotalTimeMillis()) + ")."); LOG.info("Manual cleanup of the remaining effects the failed migration may still be required."); }
public MigrationVersion getCurrentSchemaVersion() { if (!table.existsNoQuotes() && !table.exists()) { return MigrationVersion.EMPTY; } try { if (jdbcTemplate.queryForInt("SELECT COUNT(*) FROM " + table) == 0) { return MigrationVersion.EMPTY; } } catch (SQLException e) { throw new FlywayException( "Error checking if the metadata table " + table + " has at least one row", e); } createIfNotExists(); // Determine the version associated with the highest version_rank String query = "SELECT t1." + dbSupport.quote("version") + " FROM " + table + " t1" + " LEFT OUTER JOIN " + table + " t2 ON" + " (t1." + dbSupport.quote("version") + " = t2." + dbSupport.quote("version") + " AND t1." + dbSupport.quote("version_rank") + " < t2." + dbSupport.quote("version_rank") + ")" + " WHERE t2." + dbSupport.quote("version") + " IS NULL"; try { String version = jdbcTemplate.queryForString(query); return new MigrationVersion(version); } catch (SQLException e) { throw new FlywayException( "Error determining current schema version from metadata table " + table, e); } }
public boolean hasInitMarker() { if (!table.existsNoQuotes() && !table.exists()) { return false; } createIfNotExists(); try { int count = jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM " + table + " WHERE " + dbSupport.quote("type") + "='INIT'"); return count > 0; } catch (SQLException e) { throw new FlywayException( "Unable to check whether the metadata table " + table + " has an init marker migration", e); } }
public boolean hasAppliedMigrations() { if (!table.existsNoQuotes() && !table.exists()) { return false; } createIfNotExists(); try { int count = jdbcTemplate.queryForInt( "SELECT COUNT(*) FROM " + table + " WHERE " + dbSupport.quote("type") + " NOT IN ('SCHEMA', 'INIT')"); return count > 0; } catch (SQLException e) { throw new FlywayException( "Unable to check whether the metadata table " + table + " has applied migrations", e); } }