Search in sources :

Example 6 with ContextExpression

use of liquibase.ContextExpression in project liquibase by liquibase.

the class StandardChangeLogHistoryService method getRanChangeSets.

/**
     * Returns the ChangeSets that have been run against the current getDatabase().
     */
public List<RanChangeSet> getRanChangeSets() throws DatabaseException {
    if (this.ranChangeSetList == null) {
        Database database = getDatabase();
        String databaseChangeLogTableName = getDatabase().escapeTableName(getLiquibaseCatalogName(), getLiquibaseSchemaName(), getDatabaseChangeLogTableName());
        List<RanChangeSet> ranChangeSetList = new ArrayList<RanChangeSet>();
        if (hasDatabaseChangeLogTable()) {
            LogFactory.getLogger().info("Reading from " + databaseChangeLogTableName);
            List<Map<String, ?>> results = queryDatabaseChangeLogTable(database);
            for (Map rs : results) {
                String fileName = rs.get("FILENAME").toString();
                String author = rs.get("AUTHOR").toString();
                String id = rs.get("ID").toString();
                String md5sum = rs.get("MD5SUM") == null || !databaseChecksumsCompatible ? null : rs.get("MD5SUM").toString();
                String description = rs.get("DESCRIPTION") == null ? null : rs.get("DESCRIPTION").toString();
                String comments = rs.get("COMMENTS") == null ? null : rs.get("COMMENTS").toString();
                Object tmpDateExecuted = rs.get("DATEEXECUTED");
                Date dateExecuted = null;
                if (tmpDateExecuted instanceof Date) {
                    dateExecuted = (Date) tmpDateExecuted;
                } else {
                    DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    try {
                        dateExecuted = df.parse((String) tmpDateExecuted);
                    } catch (ParseException e) {
                    }
                }
                String tmpOrderExecuted = rs.get("ORDEREXECUTED").toString();
                Integer orderExecuted = (tmpOrderExecuted == null ? null : Integer.valueOf(tmpOrderExecuted));
                String tag = rs.get("TAG") == null ? null : rs.get("TAG").toString();
                String execType = rs.get("EXECTYPE") == null ? null : rs.get("EXECTYPE").toString();
                ContextExpression contexts = new ContextExpression((String) rs.get("CONTEXTS"));
                Labels labels = new Labels((String) rs.get("LABELS"));
                String deploymentId = (String) rs.get("DEPLOYMENT_ID");
                try {
                    RanChangeSet ranChangeSet = new RanChangeSet(fileName, id, author, CheckSum.parse(md5sum), dateExecuted, tag, ChangeSet.ExecType.valueOf(execType), description, comments, contexts, labels, deploymentId);
                    ranChangeSet.setOrderExecuted(orderExecuted);
                    ranChangeSetList.add(ranChangeSet);
                } catch (IllegalArgumentException e) {
                    LogFactory.getLogger().severe("Unknown EXECTYPE from database: " + execType);
                    throw e;
                }
            }
        }
        this.ranChangeSetList = ranChangeSetList;
    }
    return Collections.unmodifiableList(ranChangeSetList);
}
Also used : ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DB2Database(liquibase.database.core.DB2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Database(liquibase.database.Database) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Example 7 with ContextExpression

use of liquibase.ContextExpression in project liquibase by liquibase.

the class ContextChangeSetFilter method accepts.

@Override
public ChangeSetFilterResult accepts(ChangeSet changeSet) {
    List<SqlVisitor> visitorsToRemove = new ArrayList<SqlVisitor>();
    for (SqlVisitor visitor : changeSet.getSqlVisitors()) {
        if (visitor.getContexts() != null && !visitor.getContexts().matches(contexts)) {
            visitorsToRemove.add(visitor);
        }
    }
    changeSet.getSqlVisitors().removeAll(visitorsToRemove);
    if (contexts == null || contexts.isEmpty()) {
        return new ChangeSetFilterResult(true, "No runtime context specified, all contexts will run", this.getClass());
    }
    Collection<ContextExpression> inheritableContexts = changeSet.getInheritableContexts();
    if (changeSet.getContexts().isEmpty() && inheritableContexts.isEmpty()) {
        return new ChangeSetFilterResult(true, "Change set runs under all contexts", this.getClass());
    }
    if (changeSet.getContexts().matches(contexts) && ContextExpression.matchesAll(inheritableContexts, contexts)) {
        return new ChangeSetFilterResult(true, "Context matches '" + contexts.toString() + "'", this.getClass());
    } else {
        return new ChangeSetFilterResult(false, "Context does not match '" + contexts.toString() + "'", this.getClass());
    }
}
Also used : ContextExpression(liquibase.ContextExpression) SqlVisitor(liquibase.sql.visitor.SqlVisitor)

Example 8 with ContextExpression

use of liquibase.ContextExpression in project liquibase by liquibase.

the class ChangeSet method handleChildNode.

protected void handleChildNode(ParsedNode child, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    if (child.getName().equals("rollback")) {
        handleRollbackNode(child, resourceAccessor);
    } else if (child.getName().equals("validCheckSum") || child.getName().equals("validCheckSums")) {
        if (child.getValue() == null) {
            return;
        }
        if (child.getValue() instanceof Collection) {
            for (Object checksum : (Collection) child.getValue()) {
                addValidCheckSum((String) checksum);
            }
        } else {
            addValidCheckSum(child.getValue(String.class));
        }
    } else if (child.getName().equals("modifySql")) {
        String dbmsString = StringUtils.trimToNull(child.getChildValue(null, "dbms", String.class));
        String contextString = StringUtils.trimToNull(child.getChildValue(null, "context", String.class));
        String labelsString = StringUtils.trimToNull(child.getChildValue(null, "labels", String.class));
        boolean applyToRollback = child.getChildValue(null, "applyToRollback", false);
        Set<String> dbms = new HashSet<String>();
        if (dbmsString != null) {
            dbms.addAll(StringUtils.splitAndTrim(dbmsString, ","));
        }
        ContextExpression context = null;
        if (contextString != null) {
            context = new ContextExpression(contextString);
        }
        Labels labels = null;
        if (labelsString != null) {
            labels = new Labels(labelsString);
        }
        List<ParsedNode> potentialVisitors = child.getChildren();
        for (ParsedNode node : potentialVisitors) {
            SqlVisitor sqlVisitor = SqlVisitorFactory.getInstance().create(node.getName());
            if (sqlVisitor != null) {
                sqlVisitor.setApplyToRollback(applyToRollback);
                if (dbms.size() > 0) {
                    sqlVisitor.setApplicableDbms(dbms);
                }
                sqlVisitor.setContexts(context);
                sqlVisitor.setLabels(labels);
                sqlVisitor.load(node, resourceAccessor);
                addSqlVisitor(sqlVisitor);
            }
        }
    } else if (child.getName().equals("preConditions")) {
        this.preconditions = new PreconditionContainer();
        try {
            this.preconditions.load(child, resourceAccessor);
        } catch (ParsedNodeException e) {
            e.printStackTrace();
        }
    } else if (child.getName().equals("changes")) {
        for (ParsedNode changeNode : child.getChildren()) {
            handleChildNode(changeNode, resourceAccessor);
        }
    } else {
        Change change = toChange(child, resourceAccessor);
        if (change == null && child.getValue() instanceof String) {
            this.setAttribute(child.getName(), child.getValue());
        } else {
            addChange(change);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) PreconditionContainer(liquibase.precondition.core.PreconditionContainer) ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels) Change(liquibase.change.Change) EmptyChange(liquibase.change.core.EmptyChange) RawSQLChange(liquibase.change.core.RawSQLChange) DbmsTargetedChange(liquibase.change.DbmsTargetedChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor) ParsedNodeException(liquibase.parser.core.ParsedNodeException)

Example 9 with ContextExpression

use of liquibase.ContextExpression in project liquibase by liquibase.

the class ChangeLogIterator method createKey.

protected String createKey(ChangeSet changeSet) {
    Labels labels = changeSet.getLabels();
    ContextExpression contexts = changeSet.getContexts();
    return changeSet.toString(true) + ":" + (labels == null ? null : labels.toString()) + ":" + (contexts == null ? null : contexts.toString()) + ":" + StringUtils.join(changeSet.getDbmsSet(), ",");
}
Also used : ContextExpression(liquibase.ContextExpression) Labels(liquibase.Labels)

Example 10 with ContextExpression

use of liquibase.ContextExpression in project liquibase by liquibase.

the class ChangeLogParametersTest method setParameterValue_multiDatabase.

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

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