All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.sdk.supplier.change.AbstractChangeSupplier Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
package liquibase.sdk.supplier.change;

import liquibase.change.*;
import liquibase.change.core.supplier.AddColumnConfigSupplier;
import liquibase.database.Database;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.util.CollectionUtil;

import java.util.*;

public abstract class AbstractChangeSupplier implements ChangeSupplier {

    private final String changeName;

    protected AbstractChangeSupplier(Class changeClass) {
        try {
            changeName = changeClass.newInstance().getSerializedObjectName();
        } catch (Exception e) {
            throw new UnexpectedLiquibaseException(e);
        }
    }

    public String getChangeName() {
           return changeName;
    }

    @Override
    public Change[] revertDatabase(T change) throws Exception {
        return null;
    }

    @Override
    public Collection getAllParameterPermutations(Database database) throws Exception {
        ChangeMetaData changeMetaData = ChangeFactory.getInstance().getChangeMetaData(getChangeName());
        Set> parameterSets = CollectionUtil.powerSet(changeMetaData.getParameters().keySet());

        List changes = new ArrayList();
        for (Collection params : parameterSets) {
            Map> parameterValues = new HashMap>();
            for (String param : params) {
                ChangeParameterMetaData changeParam = changeMetaData.getParameters().get(param);
                parameterValues.put(param, new ArrayList());
                parameterValues.get(param).addAll(getTestValues(changeParam, database));
            }

            for (Map valuePermutation : CollectionUtil.permutations(parameterValues)) {
                Change change = ChangeFactory.getInstance().create(getChangeName());
                for (Map.Entry entry : valuePermutation.entrySet()) {
                    ChangeParameterMetaData changeParam = changeMetaData.getParameters().get(entry.getKey());
                    changeParam.setValue(change, entry.getValue());
                }
                changes.add(change);
            }
        }

        return changes;
    }

    protected List getTestValues(ChangeParameterMetaData changeParam, Database database) throws Exception {
        List values = new ArrayList();

        if (changeParam.getDataType().equals("list of addColumnConfig")) {
            for (AddColumnConfig config : getAddColumnConfigSupplier().getStandardPermutations(database)) {
                values.add(new ArrayList(Arrays.asList(config)));
            }

        } else {
            Object exampleValue = changeParam.getExampleValue(database);
            values.add(exampleValue);
        }
        return values;
    }

    protected AddColumnConfigSupplier getAddColumnConfigSupplier() {
        return new AddColumnConfigSupplier();
    }

    @Override
    public boolean isValid(Change change, Database database) {
        return !change.validate(database).hasErrors();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy