コード例 #1
0
 @Override
 public String escapeColumnNameList(final String columnNames) {
   StringBuffer sb = new StringBuffer();
   for (String columnName : columnNames.split(",")) {
     if (sb.length() > 0) {
       sb.append(", ");
     }
     sb.append(escapeObjectName(columnName.trim(), Column.class));
   }
   return sb.toString();
 }
コード例 #2
0
ファイル: ChangeSet.java プロジェクト: mbocek/liquibase
  public CheckSum generateCheckSum() {
    StringBuffer stringToMD5 = new StringBuffer();
    for (Change change : getChanges()) {
      stringToMD5.append(change.generateCheckSum()).append(":");
    }

    for (SqlVisitor visitor : this.getSqlVisitors()) {
      stringToMD5.append(visitor.generateCheckSum()).append(";");
    }

    return CheckSum.compute(stringToMD5.toString());
  }
コード例 #3
0
  @Override
  public String getConcatSql(final String... values) {
    StringBuffer returnString = new StringBuffer();
    for (String value : values) {
      returnString.append(value).append(" || ");
    }

    return returnString.toString().replaceFirst(" \\|\\| $", "");
  }
コード例 #4
0
ファイル: ChangeSet.java プロジェクト: mbocek/liquibase
  /**
   * This method will actually execute each of the changes in the list against the specified
   * database.
   *
   * @return should change set be marked as ran
   */
  public ExecType execute(
      DatabaseChangeLog databaseChangeLog, ChangeExecListener listener, Database database)
      throws MigrationFailedException {
    if (validationFailed) {
      return ExecType.MARK_RAN;
    }

    long startTime = new Date().getTime();

    ExecType execType = null;

    boolean skipChange = false;

    Executor executor = ExecutorService.getInstance().getExecutor(database);
    try {
      // set object quoting strategy
      database.setObjectQuotingStrategy(objectQuotingStrategy);

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

      executor.comment("Changeset " + toString(false));
      if (StringUtils.trimToNull(getComments()) != null) {
        String comments = getComments();
        String[] lines = comments.split("\\n");
        for (int i = 0; i < lines.length; i++) {
          if (i > 0) {
            lines[i] = database.getLineComment() + " " + lines[i];
          }
        }
        executor.comment(StringUtils.join(Arrays.asList(lines), "\n"));
      }

      try {
        if (preconditions != null) {
          preconditions.check(database, databaseChangeLog, this);
        }
      } catch (PreconditionFailedException e) {
        if (listener != null) {
          listener.preconditionFailed(e, preconditions.getOnFail());
        }
        StringBuffer message = new StringBuffer();
        message.append(StreamUtil.getLineSeparator());
        for (FailedPrecondition invalid : e.getFailedPreconditions()) {
          message.append("          ").append(invalid.toString());
          message.append(StreamUtil.getLineSeparator());
        }

        if (preconditions.getOnFail().equals(PreconditionContainer.FailOption.HALT)) {
          throw new MigrationFailedException(this, message.toString(), e);
        } else if (preconditions.getOnFail().equals(PreconditionContainer.FailOption.CONTINUE)) {
          skipChange = true;
          execType = ExecType.SKIPPED;

          LogFactory.getLogger()
              .info(
                  "Continuing past: "
                      + toString()
                      + " despite precondition failure due to onFail='CONTINUE': "
                      + message);
        } else if (preconditions.getOnFail().equals(PreconditionContainer.FailOption.MARK_RAN)) {
          execType = ExecType.MARK_RAN;
          skipChange = true;

          log.info(
              "Marking ChangeSet: "
                  + toString()
                  + " ran despite precondition failure due to onFail='MARK_RAN': "
                  + message);
        } else if (preconditions.getOnFail().equals(PreconditionContainer.FailOption.WARN)) {
          execType = null; // already warned
        } else {
          throw new UnexpectedLiquibaseException(
              "Unexpected precondition onFail attribute: " + preconditions.getOnFail(), e);
        }
      } catch (PreconditionErrorException e) {
        if (listener != null) {
          listener.preconditionErrored(e, preconditions.getOnError());
        }

        StringBuffer message = new StringBuffer();
        message.append(StreamUtil.getLineSeparator());
        for (ErrorPrecondition invalid : e.getErrorPreconditions()) {
          message.append("          ").append(invalid.toString());
          message.append(StreamUtil.getLineSeparator());
        }

        if (preconditions.getOnError().equals(PreconditionContainer.ErrorOption.HALT)) {
          throw new MigrationFailedException(this, message.toString(), e);
        } else if (preconditions.getOnError().equals(PreconditionContainer.ErrorOption.CONTINUE)) {
          skipChange = true;
          execType = ExecType.SKIPPED;

        } else if (preconditions.getOnError().equals(PreconditionContainer.ErrorOption.MARK_RAN)) {
          execType = ExecType.MARK_RAN;
          skipChange = true;

          log.info(
              "Marking ChangeSet: " + toString() + " ran despite precondition error: " + message);
        } else if (preconditions.getOnError().equals(PreconditionContainer.ErrorOption.WARN)) {
          execType = null; // already logged
        } else {
          throw new UnexpectedLiquibaseException(
              "Unexpected precondition onError attribute: " + preconditions.getOnError(), e);
        }

        database.rollback();
      } finally {
        database.rollback();
      }

      if (!skipChange) {
        for (Change change : changes) {
          try {
            change.finishInitialization();
          } catch (SetupException se) {
            throw new MigrationFailedException(this, se);
          }
        }

        log.debug("Reading ChangeSet: " + toString());
        for (Change change : getChanges()) {
          if ((!(change instanceof DbmsTargetedChange))
              || DatabaseList.definitionMatches(
                  ((DbmsTargetedChange) change).getDbms(), database, true)) {
            if (listener != null) {
              listener.willRun(change, this, changeLog, database);
            }
            if (change.generateStatementsVolatile(database)) {
              executor.comment(
                  "WARNING The following SQL is possibly incorrect, invalid, and/or may change on each run:");
            }

            database.executeStatements(change, databaseChangeLog, sqlVisitors);
            log.info(change.getConfirmationMessage());
            if (listener != null) {
              listener.ran(change, this, changeLog, database);
            }
          } else {
            log.debug(
                "Change "
                    + change.getSerializedObjectName()
                    + " not included for database "
                    + database.getShortName());
          }
        }

        if (runInTransaction) {
          database.commit();
        }
        log.info(
            "ChangeSet "
                + toString(false)
                + " ran successfully in "
                + (new Date().getTime() - startTime + "ms"));
        if (execType == null) {
          execType = ExecType.EXECUTED;
        }
      } else {
        log.debug("Skipping ChangeSet: " + toString());
      }

    } catch (Exception e) {
      try {
        database.rollback();
      } catch (Exception e1) {
        throw new MigrationFailedException(this, e);
      }
      if (getFailOnError() != null && !getFailOnError()) {
        log.info(
            "Change set "
                + toString(false)
                + " failed, but failOnError was false.  Error: "
                + e.getMessage());
        log.debug("Failure Stacktrace", e);
        execType = ExecType.FAILED;
      } else {
        // just log the message, dont log the stacktrace by appending exception. Its logged anyway
        // to stdout
        log.severe("Change Set " + toString(false) + " failed.  Error: " + e.getMessage());
        if (e instanceof MigrationFailedException) {
          throw ((MigrationFailedException) e);
        } else {
          throw new MigrationFailedException(this, 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 MigrationFailedException(this, "Could not resetInternalState autocommit", e);
        }
      }
    }
    return execType;
  }
コード例 #5
0
ファイル: ChangeSet.java プロジェクト: mbocek/liquibase
  @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);
  }