为什么我的predcondition没有序列化?

时间:2015-06-18 09:55:16

标签: java spring liquibase

我有问题。我自动生成我的更改日志。我尝试在变更集上添加一些先决条件。但是当我序列化mys变更集时,前提条件规则并没有出现在我的XML更改日志中。我只看到了addcolumn部分

这是我如何生成我的变更集:

final ChangeSet changeset = new CustomChangeSet("myId", "tutu", false, true, "", "toto", "", true, ObjectQuotingStrategy.QUOTE_ALL_OBJECTS, null);

//type
final LiquibaseDataType type = new VarcharType();
type.addParameter(255);
final AddColumnConfig addColumn = new AddColumnConfig();
addColumn.setName("MyCol");
addColumn.setType(type.toString());
final AddColumnChange columnChange = new AddColumnChange();
columnChange.setTableName("MyTable");
columnChange.addColumn(addColumn);
PreconditionContainer precondition = new PreconditionContainer();
precondition.setOnFail(PreconditionContainer.FailOption.MARK_RAN.toString());
column.getTableName()));
NotPrecondition not = new NotPrecondition();
ColumnExistsPrecondition exists = new ColumnExistsPrecondition();
exists.setColumnName("MyCol");
exists.setTableName("MyTable");
not.addNestedPrecondition(exists);
precondition.addNestedPrecondition(not);
changeset.addChange(columnChange);
changeset.setPreconditions(precondition);

1 个答案:

答案 0 :(得分:0)

好的,我找到了办法。

首先,我扩展了ChangeSet类并覆盖了一些方法:

public class CustomChangeSet extends ChangeSet {

/**
 * @param databaseChangeLog
 */
public CustomChangeSet(DatabaseChangeLog databaseChangeLog) {
    super(databaseChangeLog);
}

public CustomChangeSet(String id, String author, boolean alwaysRun,
        boolean runOnChange, String filePath, String contextList,
        String dbmsList, DatabaseChangeLog databaseChangeLog) {
    super(id, author, alwaysRun, runOnChange, filePath, contextList,
            dbmsList, databaseChangeLog);
}

public CustomChangeSet(String id, String author, boolean alwaysRun,
        boolean runOnChange, String filePath, String contextList,
        String dbmsList, boolean runInTransaction,
        DatabaseChangeLog databaseChangeLog) {
    super(id, author, alwaysRun, runOnChange, filePath, contextList,
            dbmsList, runInTransaction, databaseChangeLog);
}


public CustomChangeSet(String id, String author, boolean alwaysRun,
        boolean runOnChange, String filePath, String contextList,
        String dbmsList, ObjectQuotingStrategy quotingStrategy,
        DatabaseChangeLog databaseChangeLog) {
    super(id, author, alwaysRun, runOnChange, filePath, contextList,
            dbmsList, quotingStrategy, databaseChangeLog);
}

public CustomChangeSet(String id, String author, boolean alwaysRun,
        boolean runOnChange, String filePath, String contextList,
        String dbmsList, boolean runInTransaction,
        ObjectQuotingStrategy quotingStrategy,
        DatabaseChangeLog databaseChangeLog) {
    super(id, author, alwaysRun, runOnChange, filePath, contextList,
            dbmsList, runInTransaction, quotingStrategy, databaseChangeLog);
}

/**
 * {@inheritDoc}
 * @see liquibase.changelog.ChangeSet#getSerializedObjectName()
 */
@Override
public String getSerializedObjectName() {
    return super.getSerializedObjectName();
}

/**
 * {@inheritDoc}
 * @see liquibase.changelog.ChangeSet#getSerializableFields()
 */
@Override
public Set<String> getSerializableFields() {
    final Set<String> tables = super.getSerializableFields();
    tables.add("preconditions");
    return tables;
}

/**
 * {@inheritDoc}
 * @see liquibase.changelog.ChangeSet#getSerializableFieldType(java.lang.String)
 */
@Override
public SerializationType getSerializableFieldType(String field) {
    if ("preconditions".equals(field)){
        return SerializationType.NESTED_OBJECT;
    }
    return super.getSerializableFieldType(field);
}

/**
 * {@inheritDoc}
 * @see liquibase.changelog.ChangeSet#getSerializedObjectNamespace()
 */
@Override
public String getSerializedObjectNamespace() {
    return super.getSerializedObjectNamespace();
}

/**
 * {@inheritDoc}
 * @see liquibase.changelog.ChangeSet#getSerializableFieldNamespace(java.lang.String)
 */
@Override
public String getSerializableFieldNamespace(String field) {
    return super.getSerializableFieldNamespace(field);
}

/**
 * {@inheritDoc}
 * @see liquibase.changelog.ChangeSet#getSerializableFieldValue(java.lang.String)
 */
@Override
public Object getSerializableFieldValue(String field) {
    if (field.equals("preconditions")){
        if (getPreconditions() != null){
            return new PreconditionContainerChange(getPreconditions());
        }else{
            return null;
        }
    }
    return super.getSerializableFieldValue(field);
}

创建一个实现LiquibaseSerialization的类并将所有方法委托给PreconditionContainer之后:

public class PreconditionContainerChange implements LiquibaseSerializable {

/**
 * The precondition
 */
private PreconditionContainer preconditions;

/**
 * @param preconditions
 */
public PreconditionContainerChange(PreconditionContainer preconditions) {
    super();
    this.preconditions = preconditions;
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#getSerializedObjectName()
 */
@Override
public String getSerializedObjectName() {
    return "preConditions";
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#getSerializableFields()
 */
@Override
public Set<String> getSerializableFields() {
    return preconditions.getSerializableFields();
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#getSerializableFieldValue(java.lang.String)
 */
@Override
public Object getSerializableFieldValue(String field) {
    return preconditions.getSerializableFieldValue(field);
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#getSerializableFieldType(java.lang.String)
 */
@Override
public SerializationType getSerializableFieldType(String field) {
    return preconditions.getSerializableFieldType(field);
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#getSerializableFieldNamespace(java.lang.String)
 */
@Override
public String getSerializableFieldNamespace(String field) {
    return preconditions.getSerializableFieldNamespace(field);
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#getSerializedObjectNamespace()
 */
@Override
public String getSerializedObjectNamespace() {
    return preconditions.getName();
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#load(liquibase.parser.core.ParsedNode, liquibase.resource.ResourceAccessor)
 */
@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor)
        throws ParsedNodeException {
    preconditions.load(parsedNode, resourceAccessor);
}

/**
 * {@inheritDoc}
 * @see liquibase.serializer.LiquibaseSerializable#serialize()
 */
@Override
public ParsedNode serialize() throws ParsedNodeException {
    return preconditions.serialize();
}

享受!