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

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

There is a newer version: 1.6.0
Show newest version
package com.liquibase.ext.parser;

import com.liquibase.ext.change.MongoshChange;
import liquibase.Scope;
import liquibase.change.AbstractSQLChange;
import liquibase.changelog.ChangeLogParameters;
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.exception.ChangeLogParseException;
import liquibase.parser.AbstractFormattedChangeLogParser;
import liquibase.resource.ResourceAccessor;
import liquibase.util.StreamUtil;
import liquibase.util.StringUtil;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FormattedMongoshChangeLogParser extends AbstractFormattedChangeLogParser {

    private static final ResourceBundle mongoshBundle = ResourceBundle.getBundle("liquibase/i18n/liquibase-mongosh");
    private static final String PRECONDITIONS_ARE_NOT_SUPPORTED = mongoshBundle.getString("preconditions.are.not.supported");

    private final String FIRST_LINE_MONGOSH_REGEX = String.format("^\\s*%s\\s*liquibase\\s*formatted\\s*mongodb|^\\s*%s\\s*liquibase\\s*formatted\\s*mongo", getSingleLineCommentSequence(), getSingleLineCommentSequence());
    private final Pattern FIRST_LINE_MONGOSH_PATTERN = Pattern.compile(FIRST_LINE_MONGOSH_REGEX, Pattern.CASE_INSENSITIVE);


    @Override
    protected String getSingleLineCommentOneCharacter() {
        return "/";
    }

    @Override
    protected String getSingleLineCommentSequence() {
        return "//";
    }

    @Override
    protected String getStartMultiLineCommentSequence() {
        return "/\\*";
    }

    @Override
    protected String getEndMultiLineCommentSequence() {
        return "\\*/";
    }

    @Override
    public boolean supports(String changeLogFile, ResourceAccessor resourceAccessor) {
        try (BufferedReader reader = new BufferedReader(StreamUtil.readStreamWithReader(openChangeLogFile(changeLogFile, resourceAccessor), null));) {
            if (supportsExtension(changeLogFile)) {

                String firstLine = reader.readLine();

                while (firstLine != null && firstLine.trim().isEmpty() && reader.ready()) {
                    firstLine = reader.readLine();
                }

                //
                // Handle empty files with a WARNING message
                //
                if (StringUtil.isEmpty(firstLine)) {
                    Scope.getCurrentScope().getLog(getClass()).warning(String.format("Skipping empty file '%s'", changeLogFile));
                    return false;
                }
                return FIRST_LINE_MONGOSH_PATTERN.matcher(firstLine).matches();
            } else {
                return false;
            }
        } catch (IOException e) {
            Scope.getCurrentScope().getLog(getClass()).fine("Exception reading " + changeLogFile, e);
            return false;
        }
    }

    @Override
    protected boolean supportsExtension(String changelogFile) {
        return changelogFile.toLowerCase().endsWith(".js");
    }

    @Override
    protected void handlePreconditionCase(ChangeLogParameters changeLogParameters, ChangeSet changeSet, Matcher preconditionMatcher) throws ChangeLogParseException {
        if (preconditionMatcher.groupCount() == 2) {
            String name = StringUtil.trimToNull(preconditionMatcher.group(1));
            if (name != null) {
                throw new ChangeLogParseException(String.format(PRECONDITIONS_ARE_NOT_SUPPORTED));
            }
        }
    }

    @Override
    protected void handlePreconditionsCase(ChangeSet changeSet, int count, Matcher preconditionsMatcher) throws ChangeLogParseException {
        if (preconditionsMatcher.groupCount() == 1) {
            String name = StringUtil.trimToNull(preconditionsMatcher.group(1));
            if (name != null) {
                throw new ChangeLogParseException(String.format(PRECONDITIONS_ARE_NOT_SUPPORTED));
            }
        }
    }

    @Override
    protected AbstractSQLChange getChange() {
        return new MongoshChange();
    }

    @Override
    protected String getDocumentationLink() {
        return "https://docs.liquibase.com/mongodb";
    }

    @Override
    protected String getSequenceName() {
        return "Mongosh";
    }

    @Override
    protected void setChangeSequence(AbstractSQLChange change, String finalCurrentSequence) {
        if (change instanceof MongoshChange) {
            ((MongoshChange) change).setMongo(finalCurrentSequence);
            change.setSql(finalCurrentSequence);
        }
    }

    @Override
    protected boolean isNotEndDelimiter(AbstractSQLChange change) {
        if (change instanceof MongoshChange) {
            return (change.getEndDelimiter() == null) && StringUtil.trimToEmpty(((MongoshChange) change).getMongo()).endsWith("\n/");
        }
        return false;
    }

    @Override
    protected void setChangeSequence(ChangeLogParameters changeLogParameters, StringBuilder currentSequence, ChangeSet changeSet, AbstractSQLChange change) {
        if (change instanceof MongoshChange) {
            String mongoshSequence = changeLogParameters.expandExpressions(StringUtil.trimToNull(currentSequence.toString()), changeSet.getChangeLog());
            ((MongoshChange) change).setMongo(mongoshSequence);
            change.setSql(mongoshSequence);
        }
    }

    @Override
    public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
        DatabaseChangeLog databaseChangeLog = super.parse(physicalChangeLogLocation, changeLogParameters, resourceAccessor);

        return ChangeLogErrorHandler.parse(databaseChangeLog, physicalChangeLogLocation, "runWith:mongosh");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy