Example #1
0
  /**
   * Generate the ChangeMetaData for this class. Default implementation reads from the @{@link
   * DatabaseChange } annotation and calls out to {@link #createChangeParameterMetadata(String)} for
   * each property.
   *
   * @throws UnexpectedLiquibaseException if no @DatabaseChange annotation on this Change class
   */
  @Override
  public ChangeMetaData createChangeMetaData() {
    try {
      DatabaseChange databaseChange = this.getClass().getAnnotation(DatabaseChange.class);

      if (databaseChange == null) {
        throw new UnexpectedLiquibaseException(
            "No @DatabaseChange annotation for " + getClass().getName());
      }

      Set<ChangeParameterMetaData> params = new HashSet<ChangeParameterMetaData>();
      for (PropertyDescriptor property :
          Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors()) {
        if (isInvalidProperty(property)) {
          continue;
        }
        Method readMethod = property.getReadMethod();
        Method writeMethod = property.getWriteMethod();
        if (readMethod == null) {
          try {
            readMethod =
                this.getClass().getMethod("is" + StringUtils.upperCaseFirst(property.getName()));
          } catch (Exception ignore) {
            // it was worth a try
          }
        }
        if (readMethod != null && writeMethod != null) {
          DatabaseChangeProperty annotation =
              readMethod.getAnnotation(DatabaseChangeProperty.class);
          if (annotation == null || annotation.isChangeProperty()) {
            params.add(createChangeParameterMetadata(property.getDisplayName()));
          }
        }
      }

      Map<String, String> notes = new HashMap<String, String>();
      for (DatabaseChangeNote note : databaseChange.databaseNotes()) {
        notes.put(note.database(), note.notes());
      }

      return new ChangeMetaData(
          databaseChange.name(),
          databaseChange.description(),
          databaseChange.priority(),
          databaseChange.appliesTo(),
          notes,
          params);
    } catch (Throwable e) {
      throw new UnexpectedLiquibaseException(e);
    }
  }
Example #2
0
  /**
   * Implementation delegates logic to the {@link
   * liquibase.sqlgenerator.SqlGeneratorFactory#getAffectedDatabaseObjects(liquibase.statement.SqlStatement,
   * liquibase.database.Database)} method on the {@link SqlStatement} objects returned by {@link
   * #generateStatements } Returns empty set if change is not supported for the passed database
   */
  @Override
  public Set<DatabaseObject> getAffectedDatabaseObjects(Database database) {
    if (this.generateStatementsVolatile(database)) {
      return new HashSet<DatabaseObject>();
    }
    Set<DatabaseObject> affectedObjects = new HashSet<DatabaseObject>();
    SqlStatement[] statements = generateStatements(database);

    if (statements != null) {
      for (SqlStatement statement : statements) {
        affectedObjects.addAll(
            SqlGeneratorFactory.getInstance().getAffectedDatabaseObjects(statement, database));
      }
    }

    return affectedObjects;
  }
Example #3
0
 protected void setDbms(String dbmsList) {
   if (StringUtils.trimToNull(dbmsList) != null) {
     String[] strings = dbmsList.toLowerCase().split(",");
     dbmsSet = new HashSet<String>();
     for (String string : strings) {
       dbmsSet.add(string.trim().toLowerCase());
     }
   }
 }
 @Override
 public boolean isReservedWord(final String string) {
   return reservedWords.contains(string.toUpperCase());
 }
 @Override
 public void addReservedWords(Collection<String> words) {
   reservedWords.addAll(words);
 }
Example #6
0
 public void addValidCheckSum(String text) {
   validCheckSums.add(CheckSum.parse(text));
 }
Example #7
0
  protected void handleChildNode(ParsedNode child, ResourceAccessor resourceAccessor)
      throws ParsedNodeException {
    if (child.getName().equals("rollback")) {
      handleRollbackNode(child, resourceAccessor);
    } else if (child.getName().equals("validCheckSum")
        || child.getName().equals("validCheckSums")) {
      if (child.getValue() == null) {
        return;
      }

      if (child.getValue() instanceof Collection) {
        for (Object checksum : (Collection) child.getValue()) {
          addValidCheckSum((String) checksum);
        }
      } else {
        addValidCheckSum(child.getValue(String.class));
      }
    } else if (child.getName().equals("modifySql")) {
      String dbmsString = StringUtils.trimToNull(child.getChildValue(null, "dbms", String.class));
      String contextString =
          StringUtils.trimToNull(child.getChildValue(null, "context", String.class));
      String labelsString =
          StringUtils.trimToNull(child.getChildValue(null, "labels", String.class));
      boolean applyToRollback = child.getChildValue(null, "applyToRollback", false);

      Set<String> dbms = new HashSet<String>();
      if (dbmsString != null) {
        dbms.addAll(StringUtils.splitAndTrim(dbmsString, ","));
      }
      ContextExpression context = null;
      if (contextString != null) {
        context = new ContextExpression(contextString);
      }

      Labels labels = null;
      if (labelsString != null) {
        labels = new Labels(labelsString);
      }

      List<ParsedNode> potentialVisitors = child.getChildren();
      for (ParsedNode node : potentialVisitors) {
        SqlVisitor sqlVisitor = SqlVisitorFactory.getInstance().create(node.getName());
        if (sqlVisitor != null) {
          sqlVisitor.setApplyToRollback(applyToRollback);
          if (dbms.size() > 0) {
            sqlVisitor.setApplicableDbms(dbms);
          }
          sqlVisitor.setContexts(context);
          sqlVisitor.setLabels(labels);
          sqlVisitor.load(node, resourceAccessor);

          addSqlVisitor(sqlVisitor);
        }
      }

    } else if (child.getName().equals("preConditions")) {
      this.preconditions = new PreconditionContainer();
      try {
        this.preconditions.load(child, resourceAccessor);
      } catch (ParsedNodeException e) {
        e.printStackTrace();
      }
    } else if (child.getName().equals("changes")) {
      for (ParsedNode changeNode : child.getChildren()) {
        handleChildNode(changeNode, resourceAccessor);
      }
    } else {
      addChange(toChange(child, resourceAccessor));
    }
  }