Search in sources :

Example 6 with ChangeLogParseException

use of liquibase.exception.ChangeLogParseException in project liquibase by liquibase.

the class YamlChangeLogParser method parse.

@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
    Yaml yaml = new Yaml();
    try {
        InputStream changeLogStream = StreamUtil.singleInputStream(physicalChangeLogLocation, resourceAccessor);
        if (changeLogStream == null) {
            throw new ChangeLogParseException(physicalChangeLogLocation + " does not exist");
        }
        Map parsedYaml;
        try {
            parsedYaml = yaml.loadAs(changeLogStream, Map.class);
        } catch (Exception e) {
            throw new ChangeLogParseException("Syntax error in " + getSupportedFileExtensions()[0] + ": " + e.getMessage(), e);
        }
        if (parsedYaml == null || parsedYaml.size() == 0) {
            throw new ChangeLogParseException("Empty file " + physicalChangeLogLocation);
        }
        DatabaseChangeLog changeLog = new DatabaseChangeLog(physicalChangeLogLocation);
        Object rootList = parsedYaml.get("databaseChangeLog");
        if (rootList == null) {
            throw new ChangeLogParseException("Could not find databaseChangeLog node");
        }
        if (!(rootList instanceof List)) {
            throw new ChangeLogParseException("databaseChangeLog does not contain a list of entries. Each changeSet must begin ' - changeSet:'");
        }
        for (Object obj : (List) rootList) {
            if (obj instanceof Map && ((Map) obj).containsKey("property")) {
                Map property = (Map) ((Map) obj).get("property");
                ContextExpression context = new ContextExpression((String) property.get("context"));
                Labels labels = new Labels((String) property.get("labels"));
                Boolean global = getGlobalParam(property);
                if (property.containsKey("name")) {
                    Object value = property.get("value");
                    if (value != null) {
                        // TODO: not nice...
                        value = value.toString();
                    }
                    changeLogParameters.set((String) property.get("name"), (String) value, context, labels, (String) property.get("dbms"), global, changeLog);
                } else if (property.containsKey("file")) {
                    Properties props = new Properties();
                    InputStream propertiesStream = StreamUtil.singleInputStream((String) property.get("file"), resourceAccessor);
                    if (propertiesStream == null) {
                        log.info("Could not open properties file " + property.get("file"));
                    } else {
                        props.load(propertiesStream);
                        for (Map.Entry entry : props.entrySet()) {
                            changeLogParameters.set(entry.getKey().toString(), entry.getValue().toString(), context, labels, (String) property.get("dbms"), global, changeLog);
                        }
                    }
                }
            }
        }
        replaceParameters(parsedYaml, changeLogParameters, changeLog);
        changeLog.setChangeLogParameters(changeLogParameters);
        ParsedNode databaseChangeLogNode = new ParsedNode(null, "databaseChangeLog");
        databaseChangeLogNode.setValue(rootList);
        changeLog.load(databaseChangeLogNode, resourceAccessor);
        return changeLog;
    } catch (Throwable e) {
        if (e instanceof ChangeLogParseException) {
            throw (ChangeLogParseException) e;
        }
        throw new ChangeLogParseException("Error parsing " + physicalChangeLogLocation, e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) InputStream(java.io.InputStream) ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) Yaml(org.yaml.snakeyaml.Yaml) ChangeLogParseException(liquibase.exception.ChangeLogParseException) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) ChangeLogParseException(liquibase.exception.ChangeLogParseException)

Example 7 with ChangeLogParseException

use of liquibase.exception.ChangeLogParseException in project liquibase by liquibase.

the class AbstractIntegrationTest method invalidIncludeDoesntBreakLiquibase.

@Test
public void invalidIncludeDoesntBreakLiquibase() throws Exception {
    if (database == null) {
        return;
    }
    Liquibase liquibase = createLiquibase(invalidReferenceChangeLog);
    try {
        liquibase.update(new Contexts());
        fail("Did not fail with invalid include");
    } catch (ChangeLogParseException ignored) {
    //expected
    }
    LockService lockService = LockServiceFactory.getInstance().getLockService(database);
    assertFalse(lockService.hasChangeLogLock());
}
Also used : Liquibase(liquibase.Liquibase) LockService(liquibase.lockservice.LockService) ChangeLogParseException(liquibase.exception.ChangeLogParseException) Contexts(liquibase.Contexts) Test(org.junit.Test)

Aggregations

ChangeLogParseException (liquibase.exception.ChangeLogParseException)7 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 Labels (liquibase.Labels)2 RawSQLChange (liquibase.change.core.RawSQLChange)2 ChangeSet (liquibase.changelog.ChangeSet)2 ParsedNode (liquibase.parser.core.ParsedNode)2 BufferedReader (java.io.BufferedReader)1 ParseException (java.text.ParseException)1 SAXParser (javax.xml.parsers.SAXParser)1 ContextExpression (liquibase.ContextExpression)1 Contexts (liquibase.Contexts)1 Liquibase (liquibase.Liquibase)1 EmptyChange (liquibase.change.core.EmptyChange)1 LockService (liquibase.lockservice.LockService)1 PreconditionContainer (liquibase.precondition.core.PreconditionContainer)1 SqlPrecondition (liquibase.precondition.core.SqlPrecondition)1