Esempio n. 1
0
  public static synchronized boolean applyScript(
      boolean runScript, String moduleKey, VirtualFile evolutionsDirectory) {
    try {
      Connection connection = EvolutionQuery.getNewConnection(Evolutions.autoCommit());
      int applying = -1;
      try {
        for (Evolution evolution : getEvolutionScript(moduleKey, evolutionsDirectory)) {
          applying = evolution.revision;
          EvolutionQuery.apply(connection, runScript, evolution, moduleKey);
        }
        return true;
      } catch (Exception e) {
        String message = e.getMessage();
        if (e instanceof SQLException) {
          SQLException ex = (SQLException) e;
          message += " [ERROR:" + ex.getErrorCode() + ", SQLSTATE:" + ex.getSQLState() + "]";
        }

        EvolutionQuery.setProblem(connection, applying, moduleKey, message);
        EvolutionQuery.closeConnection(connection);
        Logger.error(e, "Can't apply evolution");
        return false;
      }
    } catch (Exception e) {
      throw new UnexpectedException(e);
    }
  }
Esempio n. 2
0
  public static synchronized Stack<Evolution> listDatabaseEvolutions(String moduleKey) {
    Stack<Evolution> evolutions = new Stack<Evolution>();
    evolutions.add(new Evolution("", 0, "", "", false));
    Connection connection = null;
    try {
      connection = EvolutionQuery.getNewConnection();
      String tableName = "play_evolutions";
      boolean tableExists = true;
      ResultSet rs = connection.getMetaData().getTables(null, null, tableName, null);

      if (!rs.next()) {

        // Table in lowercase does not exist
        // oracle gives table names in upper case
        tableName = tableName.toUpperCase();
        Logger.trace("Checking " + tableName);
        rs.close();
        rs = connection.getMetaData().getTables(null, null, tableName, null);
        // Does it exist?
        if (!rs.next()) {
          // did not find it in uppercase either
          tableExists = false;
        }
      }

      // Do we have a
      if (tableExists) {

        checkAndUpdateEvolutionsForMultiModuleSupport(connection);

        ResultSet databaseEvolutions = EvolutionQuery.getEvolutions(connection, moduleKey);

        while (databaseEvolutions.next()) {
          Evolution evolution =
              new Evolution(
                  moduleKey,
                  databaseEvolutions.getInt(1),
                  databaseEvolutions.getString(3),
                  databaseEvolutions.getString(4),
                  false);
          evolutions.add(evolution);
        }

      } else {
        EvolutionQuery.createTable();
      }
    } catch (SQLException e) {
      Logger.error(e, "SQL error while checking play evolutions");
    } finally {
      EvolutionQuery.closeConnection(connection);
    }
    Collections.sort(evolutions);
    return evolutions;
  }
Esempio n. 3
0
  public static synchronized void checkEvolutionsState() {

    for (Entry<String, VirtualFile> moduleRoot : modulesWithEvolutions.entrySet()) {

      if (EvolutionQuery.getDatasource() != null) {
        List<Evolution> evolutionScript =
            getEvolutionScript(moduleRoot.getKey(), moduleRoot.getValue());
        Connection connection = null;
        try {
          connection = EvolutionQuery.getNewConnection();
          ResultSet rs = EvolutionQuery.getEvolutionsToApply(connection, moduleRoot.getKey());
          if (rs.next()) {
            int revision = rs.getInt("id");
            String state = rs.getString("state");
            String hash = rs.getString("hash").substring(0, 7);
            String script = "";
            if (state.equals("applying_up")) {
              script = rs.getString("apply_script");
            } else {
              script = rs.getString("revert_script");
            }
            script =
                "# --- Rev:"
                    + revision
                    + ","
                    + (state.equals("applying_up") ? "Ups" : "Downs")
                    + " - "
                    + hash
                    + "\n\n"
                    + script;
            String error = rs.getString("last_problem");
            throw new InconsistentDatabase(script, error, revision, moduleRoot.getKey());
          }
        } catch (SQLException e) {
          throw new UnexpectedException(e);
        } finally {
          EvolutionQuery.closeConnection(connection);
        }

        if (!evolutionScript.isEmpty()) {
          throw new InvalidDatabaseRevision(toHumanReadableScript(evolutionScript));
        }
      }
    }
  }