Search in sources :

Example 11 with ContextExpression

use of liquibase.ContextExpression in project liquibase by liquibase.

the class ChangeLogParametersTest method setParameterValue_doubleSetButSecondWrongDatabase.

@Test
public void setParameterValue_doubleSetButSecondWrongDatabase() {
    ChangeLogParameters changeLogParameters = new ChangeLogParameters(new H2Database());
    changeLogParameters.set("doubleSet", "originalValue", new ContextExpression(), new Labels(), "baddb", true, null);
    changeLogParameters.set("doubleSet", "newValue");
    assertEquals("newValue", changeLogParameters.getValue("doubleSet", null));
}
Also used : ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) H2Database(liquibase.database.core.H2Database) Test(org.junit.Test)

Example 12 with ContextExpression

use of liquibase.ContextExpression 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)

Aggregations

ContextExpression (liquibase.ContextExpression)12 Labels (liquibase.Labels)8 ParsedNode (liquibase.parser.core.ParsedNode)4 InputStream (java.io.InputStream)2 H2Database (liquibase.database.core.H2Database)2 LiquibaseException (liquibase.exception.LiquibaseException)2 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)2 ParsedNodeException (liquibase.parser.core.ParsedNodeException)2 PreconditionContainer (liquibase.precondition.core.PreconditionContainer)2 SqlVisitor (liquibase.sql.visitor.SqlVisitor)2 Test (org.junit.Test)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Change (liquibase.change.Change)1 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)1 EmptyChange (liquibase.change.core.EmptyChange)1 RawSQLChange (liquibase.change.core.RawSQLChange)1