Esempio n. 1
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));
        }
      }
    }
  }
Esempio n. 2
0
  public static void main(String[] args) throws SQLException {

    /** Start the DB plugin * */
    Play.id = System.getProperty("play.id");
    Play.applicationPath = new File(System.getProperty("application.path"));
    Play.guessFrameworkPath();
    Play.readConfiguration();
    Play.javaPath = new ArrayList<VirtualFile>();
    Play.classes = new ApplicationClasses();
    Play.classloader = new ApplicationClassloader();

    Play.templatesPath = new ArrayList<VirtualFile>();
    Play.modulesRoutes = new HashMap<String, VirtualFile>();
    Play.loadModules();

    if (System.getProperty("modules") != null) {
      populateModulesWithSpecificModules();
    } else {
      populateModulesWithEvolutions();
    }

    if (modulesWithEvolutions.isEmpty()) {
      System.out.println("~ Nothing has evolutions, go away and think again.");
      return;
    }

    Logger.init();
    Logger.setUp("ERROR");
    new DBPlugin().onApplicationStart();

    /** Connected * */
    System.out.println(
        "~ Connected to " + EvolutionQuery.getDatasource().getConnection().getMetaData().getURL());

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

      /** Sumary * */
      Evolution database = listDatabaseEvolutions(moduleRoot.getKey()).peek();
      Evolution application =
          listApplicationEvolutions(moduleRoot.getKey(), moduleRoot.getValue()).peek();

      if ("resolve".equals(System.getProperty("mode"))) {
        try {
          checkEvolutionsState();
          System.out.println("~");
          System.out.println("~ Nothing to resolve for " + moduleRoot.getKey() + "...");
          System.out.println("~");
          return;
        } catch (InconsistentDatabase e) {
          resolve(moduleRoot.getKey(), e.getRevision());
          System.out.println("~");
          System.out.println(
              "~ Revision "
                  + e.getRevision()
                  + " for "
                  + moduleRoot.getKey()
                  + " has been resolved;");
          System.out.println("~");
        } catch (InvalidDatabaseRevision e) {
          // see later
        }
      }

      /** Check inconsistency * */
      try {
        checkEvolutionsState();
      } catch (InconsistentDatabase e) {
        System.out.println("~");
        System.out.println("~ Your database is in an inconsistent state!");
        System.out.println("~");
        System.out.println("~ While applying this script part:");
        System.out.println("");
        System.out.println(e.getEvolutionScript());
        System.out.println("");
        System.out.println("~ The following error occured:");
        System.out.println("");
        System.out.println(e.getError());
        System.out.println("");
        System.out.println(
            "~ Please correct it manually, and mark it resolved by running `play evolutions:resolve`");
        System.out.println("~");
        return;
      } catch (InvalidDatabaseRevision e) {
        // see later
      }

      System.out.print(
          "~ '"
              + moduleRoot.getKey()
              + "' Application revision is "
              + application.revision
              + " ["
              + application.hash.substring(0, 7)
              + "]");
      System.out.println(
          " and '"
              + moduleRoot.getKey()
              + "' Database revision is "
              + database.revision
              + " ["
              + database.hash.substring(0, 7)
              + "]");
      System.out.println("~");

      /** Evolution script * */
      List<Evolution> evolutions = getEvolutionScript(moduleRoot.getKey(), moduleRoot.getValue());
      if (evolutions.isEmpty()) {
        System.out.println("~ Your database is up to date for " + moduleRoot.getKey());
        System.out.println("~");
      } else {
        if ("apply".equals(System.getProperty("mode"))) {
          System.out.println("~ Applying evolutions for " + moduleRoot.getKey() + ":");
          System.out.println("");
          System.out.println(
              "# ------------------------------------------------------------------------------");
          System.out.println("");
          System.out.println(toHumanReadableScript(evolutions));
          System.out.println("");
          System.out.println(
              "# ------------------------------------------------------------------------------");
          System.out.println("");
          if (applyScript(true, moduleRoot.getKey(), moduleRoot.getValue())) {
            System.out.println("~");
            System.out.println(
                "~ Evolutions script successfully applied for " + moduleRoot.getKey() + "!");
            System.out.println("~");
          } else {
            System.out.println("~");
            System.out.println("~ Can't apply evolutions for " + moduleRoot.getKey() + "...");
            System.out.println("~");
          }

        } else if ("markApplied".equals(System.getProperty("mode"))) {

          if (applyScript(false, moduleRoot.getKey(), moduleRoot.getValue())) {
            System.out.println(
                "~ Evolutions script marked as applied for " + moduleRoot.getKey() + "!");
            System.out.println("~");
          } else {
            System.out.println("~ Can't apply evolutions for " + moduleRoot.getKey() + "...");
            System.out.println("~");
          }

        } else {

          System.out.println("~ Your database needs evolutions for " + moduleRoot.getKey() + "!");
          System.out.println("");
          System.out.println(
              "# ------------------------------------------------------------------------------");
          System.out.println("");
          System.out.println(toHumanReadableScript(evolutions));
          System.out.println("");
          System.out.println(
              "# ------------------------------------------------------------------------------");
          System.out.println("");
          System.out.println(
              "~ Run `play evolutions:apply` to automatically apply this script to the database");
          System.out.println(
              "~ or apply it yourself and mark it done using `play evolutions:markApplied`");
          System.out.println("~");
          System.exit(-1);
        }
      }
    }
  }