예제 #1
0
  public void addRollBackSQL(String sql) {
    if (StringUtils.trimToNull(sql) == null) {
      if (rollback.getChanges().size() == 0) {
        rollback.getChanges().add(new EmptyChange());
      }
      return;
    }

    for (String statment : StringUtils.splitSQL(sql, null)) {
      rollback.getChanges().add(new RawSQLChange(statment.trim()));
    }
  }
예제 #2
0
  public boolean supportsRollback(Database database) {
    if (rollback != null && rollback.getChanges() != null && rollback.getChanges().size() > 0) {
      return true;
    }

    for (Change change : getChanges()) {
      if (!change.supportsRollback(database)) {
        return false;
      }
    }
    return true;
  }
예제 #3
0
 public void addRollbackChange(Change change) {
   if (change == null) {
     return;
   }
   rollback.getChanges().add(change);
   change.setChangeSet(this);
 }
예제 #4
0
 /**
  * Returns whether custom rollback steps are specified for this changeSet, or whether
  * auto-generated ones should be used
  */
 protected boolean hasCustomRollbackChanges() {
   return rollback != null && rollback.getChanges() != null && rollback.getChanges().size() > 0;
 }
예제 #5
0
  public void rollback(Database database) throws RollbackFailedException {
    try {
      Executor executor = ExecutorService.getInstance().getExecutor(database);
      executor.comment("Rolling Back ChangeSet: " + toString());

      database.setObjectQuotingStrategy(objectQuotingStrategy);

      // set auto-commit based on runInTransaction if database supports DDL in transactions
      if (database.supportsDDLInTransaction()) {
        database.setAutoCommit(!runInTransaction);
      }

      RanChangeSet ranChangeSet = database.getRanChangeSet(this);
      if (hasCustomRollbackChanges()) {

        final List<SqlStatement> statements = new LinkedList<SqlStatement>();
        for (Change change : rollback.getChanges()) {
          if (((change instanceof DbmsTargetedChange))
              && !DatabaseList.definitionMatches(
                  ((DbmsTargetedChange) change).getDbms(), database, true)) {
            continue;
          }
          SqlStatement[] changeStatements = change.generateStatements(database);
          if (changeStatements != null) {
            statements.addAll(Arrays.asList(changeStatements));
          }
        }
        if (!statements.isEmpty()) {
          database.executeRollbackStatements(
              statements.toArray(new SqlStatement[] {}), sqlVisitors);
        }

      } else {
        List<Change> changes = getChanges();
        for (int i = changes.size() - 1; i >= 0; i--) {
          Change change = changes.get(i);
          database.executeRollbackStatements(change, sqlVisitors);
        }
      }

      if (runInTransaction) {
        database.commit();
      }
      log.debug("ChangeSet " + toString() + " has been successfully rolled back.");
    } catch (Exception e) {
      try {
        database.rollback();
      } catch (DatabaseException e1) {
        // ok
      }
      throw new RollbackFailedException(e);
    } finally {
      // restore auto-commit to false if this ChangeSet was not run in a transaction,
      // but only if the database supports DDL in transactions
      if (!runInTransaction && database.supportsDDLInTransaction()) {
        try {
          database.setAutoCommit(false);
        } catch (DatabaseException e) {
          throw new RollbackFailedException("Could not resetInternalState autocommit", e);
        }
      }
    }
  }
예제 #6
0
  protected void handleRollbackNode(ParsedNode rollbackNode, ResourceAccessor resourceAccessor)
      throws ParsedNodeException {
    String changeSetId = rollbackNode.getChildValue(null, "changeSetId", String.class);
    if (changeSetId != null) {
      String changeSetAuthor = rollbackNode.getChildValue(null, "changeSetAuthor", String.class);
      String changeSetPath = rollbackNode.getChildValue(null, "changeSetPath", getFilePath());

      ChangeSet changeSet =
          this.getChangeLog().getChangeSet(changeSetPath, changeSetAuthor, changeSetId);
      if (changeSet == null) { // check from root
        changeSet =
            getChangeLog()
                .getRootChangeLog()
                .getChangeSet(changeSetPath, changeSetAuthor, changeSetId);
        if (changeSet == null) {
          throw new ParsedNodeException(
              "Change set "
                  + new ChangeSet(
                          changeSetId,
                          changeSetAuthor,
                          false,
                          false,
                          changeSetPath,
                          null,
                          null,
                          null)
                      .toString(false)
                  + " does not exist");
        }
      }
      for (Change change : changeSet.getChanges()) {
        rollback.getChanges().add(change);
      }
      return;
    }

    boolean foundValue = false;
    for (ParsedNode childNode : rollbackNode.getChildren()) {
      Change rollbackChange = toChange(childNode, resourceAccessor);
      if (rollbackChange != null) {
        addRollbackChange(rollbackChange);
        foundValue = true;
      }
    }

    Object value = rollbackNode.getValue();
    if (value != null) {
      if (value instanceof String) {
        String finalValue = StringUtils.trimToNull((String) value);
        if (finalValue != null) {
          String[] strings = StringUtils.processMutliLineSQL(finalValue, true, true, ";");
          for (String string : strings) {
            addRollbackChange(new RawSQLChange(string));
            foundValue = true;
          }
        }
      } else {
        throw new ParsedNodeException(
            "Unexpected object: " + value.getClass().getName() + " '" + value.toString() + "'");
      }
    }
    if (!foundValue) {
      addRollbackChange(new EmptyChange());
    }
  }
예제 #7
0
  @Override
  public Object getSerializableFieldValue(String field) {
    if (field.equals("id")) {
      return this.getId();
    }
    if (field.equals("author")) {
      return this.getAuthor();
    }

    if (field.equals("runAlways")) {
      if (this.isAlwaysRun()) {
        return true;
      } else {
        return null;
      }
    }

    if (field.equals("runOnChange")) {
      if (this.isRunOnChange()) {
        return true;
      } else {
        return null;
      }
    }

    if (field.equals("failOnError")) {
      return this.getFailOnError();
    }

    if (field.equals("context")) {
      if (!this.getContexts().isEmpty()) {
        return this.getContexts().toString().replaceFirst("^\\(", "").replaceFirst("\\)$", "");
      } else {
        return null;
      }
    }

    if (field.equals("labels")) {
      if (this.getLabels() != null && !this.getLabels().isEmpty()) {
        return StringUtils.join(this.getLabels().getLabels(), ", ");
      } else {
        return null;
      }
    }

    if (field.equals("dbms")) {
      if (this.getDbmsSet() != null && this.getDbmsSet().size() > 0) {
        StringBuffer dbmsString = new StringBuffer();
        for (String dbms : this.getDbmsSet()) {
          dbmsString.append(dbms).append(",");
        }
        return dbmsString.toString().replaceFirst(",$", "");
      } else {
        return null;
      }
    }

    if (field.equals("comment")) {
      return StringUtils.trimToNull(this.getComments());
    }

    if (field.equals("objectQuotingStrategy")) {
      if (this.getObjectQuotingStrategy() == null) {
        return null;
      }
      return this.getObjectQuotingStrategy().toString();
    }

    if (field.equals("preconditions")) {
      if (this.getPreconditions() != null
          && this.getPreconditions().getNestedPreconditions().size() > 0) {
        return this.getPreconditions();
      } else {
        return null;
      }
    }

    if (field.equals("changes")) {
      return getChanges();
    }

    if (field.equals("created")) {
      return getCreated();
    }

    if (field.equals("rollback")) {
      if (rollback != null && rollback.getChanges() != null && rollback.getChanges().size() > 0) {
        return rollback;
      } else {
        return null;
      }
    }

    throw new UnexpectedLiquibaseException("Unexpected field request on changeSet: " + field);
  }