Example #1
0
  /**
   * Create the {@link ChangeParameterMetaData} "example" value. Uses the value on the
   * DatabaseChangeProperty annotation or returns null as a default. Returns map with key=database
   * short name, value=example. Use short-name "all" as the fallback.
   */
  @SuppressWarnings("UnusedParameters")
  protected Map<String, Object> createExampleValueMetaData(
      String parameterName, DatabaseChangeProperty changePropertyAnnotation) {
    if (changePropertyAnnotation == null) {
      return null;
    }

    Map<String, Object> examples = new HashMap<String, Object>();
    examples.put("all", StringUtils.trimToNull(changePropertyAnnotation.exampleValue()));

    return examples;
  }
Example #2
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);
    }
  }
 private void configureCustomChange() throws CustomChangeException {
   try {
     for (String param : params) {
       ObjectUtil.setProperty(customChange, param, paramValues.get(param));
     }
     customChange.setFileOpener(getResourceAccessor());
     customChange.setUp();
   } catch (Exception e) {
     throw new CustomChangeException(e);
   }
 }
 /**
  * Get the value of a parameter set by {@link #setParam(String, String)}. If the parameter was not
  * set, null will be returned.
  */
 public String getParamValue(String key) {
   return paramValues.get(key);
 }