Esempio n. 1
0
  /** 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.");
  }
Esempio n. 2
0
  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);
    }
  }
Esempio n. 3
0
  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.");
  }
Esempio n. 4
0
  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);
    }
  }
Esempio n. 5
0
  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);
    }
  }
Esempio n. 6
0
  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);
    }
  }
Esempio n. 7
0
 @Override
 public String toString() {
   return table.toString();
 }
Esempio n. 8
0
 public void lock() {
   createIfNotExists();
   table.lock();
 }
Esempio n. 9
0
  @Override
  protected void doClean() throws SQLException {
    if ("SYSTEM".equals(name.toUpperCase())) {
      throw new FlywayException(
          "Clean not supported on Oracle for user 'SYSTEM'! You should NEVER add your own objects to the SYSTEM schema!");
    }

    jdbcTemplate.execute("PURGE RECYCLEBIN");

    for (String statement : generateDropStatementsForSpatialExtensions()) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForQueueTables()) {
      // for dropping queue tables, a special grant is required:
      // GRANT EXECUTE ON DBMS_AQADM TO flyway;
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("SEQUENCE", "")) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("FUNCTION", "")) {
      jdbcTemplate.execute(statement);
    }

    for (String statement :
        generateDropStatementsForObjectType("MATERIALIZED VIEW", "PRESERVE TABLE")) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("PACKAGE", "")) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("PROCEDURE", "")) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("SYNONYM", "")) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("TRIGGER", "")) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("VIEW", "CASCADE CONSTRAINTS")) {
      jdbcTemplate.execute(statement);
    }

    for (Table table : allTables()) {
      table.drop();
    }

    for (String statement : generateDropStatementsForXmlTables()) {
      jdbcTemplate.execute(statement);
    }

    for (String statement : generateDropStatementsForObjectType("TYPE", "FORCE")) {
      jdbcTemplate.execute(statement);
    }
  }