Why my predcondition is not serialized?

176 views Asked by At

I have a problem. I autogenerate my changelog. I try to add some precondition on a changeset. But when i serialize mys changeset, the precondition rule doesn't appear in my XML changelog. I see only the addcolumn section

Here how i generate my changeset:

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

There are 1 answers

0
lecogiteur On BEST ANSWER

Ok I find a way.

First I extend the class ChangeSet and override some methods:

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);
}

After I create a class which implement LiquibaseSerialization and delegates all method to 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();
}

Enjoy!