Example #1
0
  public void addAppliedMigration(AppliedMigration appliedMigration) {
    createIfNotExists();

    MigrationVersion version = appliedMigration.getVersion();
    try {
      int versionRank = calculateVersionRank(version);

      jdbcTemplate.update(
          "UPDATE "
              + table
              + " SET "
              + dbSupport.quote("version_rank")
              + " = "
              + dbSupport.quote("version_rank")
              + " + 1 WHERE "
              + dbSupport.quote("version_rank")
              + " >= ?",
          versionRank);
      jdbcTemplate.update(
          "INSERT INTO "
              + table
              + " ("
              + 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_by")
              + ","
              + dbSupport.quote("execution_time")
              + ","
              + dbSupport.quote("success")
              + ")"
              + " VALUES (?, ?, ?, ?, ?, ?, ?, "
              + dbSupport.getCurrentUserFunction()
              + ", ?, ?)",
          versionRank,
          calculateInstalledRank(),
          version.toString(),
          appliedMigration.getDescription(),
          appliedMigration.getType().name(),
          appliedMigration.getScript(),
          appliedMigration.getChecksum(),
          appliedMigration.getExecutionTime(),
          appliedMigration.isSuccess());
      LOG.debug("MetaData table " + table + " successfully updated to reflect changes");
    } catch (SQLException e) {
      throw new FlywayException(
          "Unable to insert row for version '" + version + "' in metadata table " + table, e);
    }
  }
Example #2
0
  /**
   * Calculate the rank for this new version about to be inserted.
   *
   * @param version The version to calculated for.
   * @return The rank.
   */
  private int calculateVersionRank(MigrationVersion version) throws SQLException {
    List<String> versions =
        jdbcTemplate.queryForStringList("select " + dbSupport.quote("version") + " from " + table);

    List<MigrationVersion> migrationVersions = new ArrayList<MigrationVersion>();
    for (String versionStr : versions) {
      migrationVersions.add(new MigrationVersion(versionStr));
    }

    Collections.sort(migrationVersions);

    for (int i = 0; i < migrationVersions.size(); i++) {
      if (version.compareTo(migrationVersions.get(i)) < 0) {
        return i + 1;
      }
    }

    return migrationVersions.size() + 1;
  }