Search in sources :

Example 21 with ParsedNode

use of liquibase.parser.core.ParsedNode in project liquibase by liquibase.

the class AbstractChangeLogParser method parse.

@Override
public DatabaseChangeLog parse(String physicalChangeLogLocation, ChangeLogParameters changeLogParameters, ResourceAccessor resourceAccessor) throws ChangeLogParseException {
    ParsedNode parsedNode = parseToNode(physicalChangeLogLocation, changeLogParameters, resourceAccessor);
    if (parsedNode == null) {
        return null;
    }
    DatabaseChangeLog changeLog = new DatabaseChangeLog(physicalChangeLogLocation);
    changeLog.setChangeLogParameters(changeLogParameters);
    try {
        changeLog.load(parsedNode, resourceAccessor);
    } catch (Exception e) {
        throw new ChangeLogParseException(e);
    }
    return changeLog;
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) ChangeLogParseException(liquibase.exception.ChangeLogParseException) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) ChangeLogParseException(liquibase.exception.ChangeLogParseException) ParseException(java.text.ParseException)

Example 22 with ParsedNode

use of liquibase.parser.core.ParsedNode 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 23 with ParsedNode

use of liquibase.parser.core.ParsedNode in project liquibase by liquibase.

the class YamlSnapshotParser method parse.

@Override
public DatabaseSnapshot parse(String path, ResourceAccessor resourceAccessor) throws LiquibaseParseException {
    Yaml yaml = new Yaml();
    try {
        InputStream stream = StreamUtil.singleInputStream(path, resourceAccessor);
        if (stream == null) {
            throw new LiquibaseParseException(path + " does not exist");
        }
        Map parsedYaml;
        try {
            parsedYaml = yaml.loadAs(new InputStreamReader(stream, LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding()), Map.class);
        } catch (Exception e) {
            throw new LiquibaseParseException("Syntax error in " + getSupportedFileExtensions()[0] + ": " + e.getMessage(), e);
        } finally {
            try {
                stream.close();
            } catch (IOException ioe) {
            }
        }
        Map rootList = (Map) parsedYaml.get("snapshot");
        if (rootList == null) {
            throw new LiquibaseParseException("Could not find root snapshot node");
        }
        String shortName = (String) ((Map) rootList.get("database")).get("shortName");
        Database database = DatabaseFactory.getInstance().getDatabase(shortName).getClass().newInstance();
        DatabaseSnapshot snapshot = new RestoredDatabaseSnapshot(database);
        ParsedNode snapshotNode = new ParsedNode(null, "snapshot");
        snapshotNode.setValue(rootList);
        Map metadata = (Map) rootList.get("metadata");
        if (metadata != null) {
            snapshot.getMetadata().putAll(metadata);
        }
        snapshot.load(snapshotNode, resourceAccessor);
        return snapshot;
    } catch (Throwable e) {
        if (e instanceof LiquibaseParseException) {
            throw (LiquibaseParseException) e;
        }
        throw new LiquibaseParseException(e);
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) IOException(java.io.IOException) LiquibaseParseException(liquibase.exception.LiquibaseParseException) LiquibaseParseException(liquibase.exception.LiquibaseParseException) Database(liquibase.database.Database) Map(java.util.Map) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) RestoredDatabaseSnapshot(liquibase.snapshot.RestoredDatabaseSnapshot) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) RestoredDatabaseSnapshot(liquibase.snapshot.RestoredDatabaseSnapshot)

Aggregations

ParsedNode (liquibase.parser.core.ParsedNode)23 ParsedNodeException (liquibase.parser.core.ParsedNodeException)12 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)5 ContextExpression (liquibase.ContextExpression)4 DatabaseObject (liquibase.structure.DatabaseObject)4 Labels (liquibase.Labels)3 InputStream (java.io.InputStream)2 ParseException (java.text.ParseException)2 Change (liquibase.change.Change)2 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)2 EmptyChange (liquibase.change.core.EmptyChange)2 RawSQLChange (liquibase.change.core.RawSQLChange)2 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)2 ChangeLogParseException (liquibase.exception.ChangeLogParseException)2 Yaml (org.yaml.snakeyaml.Yaml)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BigInteger (java.math.BigInteger)1 Date (java.util.Date)1