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

com.liquibase.ext.parser.ChangeLogErrorHandler Maven / Gradle / Ivy

The newest version!
package com.liquibase.ext.parser;

import com.liquibase.ext.change.MongoshChange;
import com.liquibase.ext.change.MongoshFileChange;
import liquibase.Scope;
import liquibase.change.Change;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.exception.ChangeLogParseException;
import liquibase.util.StringUtil;

import java.util.ResourceBundle;

class ChangeLogErrorHandler {

    private static final ResourceBundle mongoshBundle = ResourceBundle.getBundle("liquibase/i18n/liquibase-mongosh");
    private static final String CHANGELOG_ATTRIBUTE_IS_MISSING = mongoshBundle.getString("changelog.attribute.is.missing");
    private static final String CHANGELOG_ATTRIBUTE_IS_NOT_CORRECT = mongoshBundle.getString("changelog.runwith.attribute.is.not.correct");

    private ChangeLogErrorHandler() {
    }

    public static DatabaseChangeLog parse(DatabaseChangeLog databaseChangeLog, String physicalChangeLogLocation, String missingAttribute) throws ChangeLogParseException {
        if(Scope.getCurrentScope().has("modifyChangeSets")) {
            //it's inner changeSet in modifyChangeSets flow, runWith will be added later on
            return databaseChangeLog;
        }
        for (ChangeSet changeSet : databaseChangeLog.getChangeSets()) {
            for (Change change : changeSet.getChanges()) {
                if (change instanceof MongoshChange || change instanceof MongoshFileChange) {
                    if (StringUtil.isEmpty(changeSet.getRunWith()) ) {
                        throw new ChangeLogParseException(String.format(CHANGELOG_ATTRIBUTE_IS_MISSING, physicalChangeLogLocation, changeSet.getAuthor(), changeSet.getId(), missingAttribute));
                    } else if (!changeSet.getRunWith().equalsIgnoreCase("mongosh")) {
                        throw new ChangeLogParseException(String.format(CHANGELOG_ATTRIBUTE_IS_NOT_CORRECT, physicalChangeLogLocation, changeSet.getAuthor(), changeSet.getId(), getChangelogSpecificElementSyntax(changeSet.getRunWith(), missingAttribute), missingAttribute));
                    }
                }
            }
        }
        return databaseChangeLog;
    }

    private static String getChangelogSpecificElementSyntax(String actualAttribute, String missingAttribute) {
        return missingAttribute.replace("mongosh", actualAttribute);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy