Search in sources :

Example 1 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class AbstractJdbcDatabaseTest method executeRollbackStatements_WithStatementsOverload_ShouldIncludeAppendTextFromApplyToRollbackTrueVisitor.

@Test
public void executeRollbackStatements_WithStatementsOverload_ShouldIncludeAppendTextFromApplyToRollbackTrueVisitor() throws Exception {
    Database database = getDatabase();
    final MockExecutor mockExecutor = new MockExecutor();
    ExecutorService.getInstance().setExecutor(database, mockExecutor);
    final List<SqlVisitor> sqlVisitors = new ArrayList<SqlVisitor>();
    final SqlStatement dropTableStatement = new DropTableStatement(null, null, "test_table", false);
    final AppendSqlVisitor appendSqlVisitor = new AppendSqlVisitor();
    appendSqlVisitor.setApplyToRollback(true);
    appendSqlVisitor.setValue(" SHOULD BE APPENDED");
    sqlVisitors.add(appendSqlVisitor);
    database.executeRollbackStatements(new SqlStatement[] { dropTableStatement }, sqlVisitors);
    assertEquals("DROP TABLE test_table SHOULD BE APPENDED;", mockExecutor.getRanSql().trim());
}
Also used : AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) SqlStatement(liquibase.statement.SqlStatement) MockExecutor(liquibase.sdk.executor.MockExecutor) ArrayList(java.util.ArrayList) DropTableStatement(liquibase.statement.core.DropTableStatement) SqlVisitor(liquibase.sql.visitor.SqlVisitor) AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) Test(org.junit.Test)

Example 2 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class AbstractJdbcDatabaseTest method executeRollbackStatements_WithChangeOverload_ShouldNotIncludeAppendTextFromApplyToRollbackFalseVisitor.

@Test
public void executeRollbackStatements_WithChangeOverload_ShouldNotIncludeAppendTextFromApplyToRollbackFalseVisitor() throws Exception {
    Database database = getDatabase();
    final MockExecutor mockExecutor = new MockExecutor();
    ExecutorService.getInstance().setExecutor(database, mockExecutor);
    final List<SqlVisitor> sqlVisitors = new ArrayList<SqlVisitor>();
    final CreateTableChange change = new CreateTableChange();
    change.setTableName("test_table");
    final AppendSqlVisitor appendSqlVisitor = new AppendSqlVisitor();
    appendSqlVisitor.setApplyToRollback(false);
    appendSqlVisitor.setValue(" SHOULD NOT BE APPENDED");
    sqlVisitors.add(appendSqlVisitor);
    database.executeRollbackStatements(change, sqlVisitors);
    assertEquals("DROP TABLE test_table;", mockExecutor.getRanSql().trim());
}
Also used : AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) MockExecutor(liquibase.sdk.executor.MockExecutor) ArrayList(java.util.ArrayList) CreateTableChange(liquibase.change.core.CreateTableChange) SqlVisitor(liquibase.sql.visitor.SqlVisitor) AppendSqlVisitor(liquibase.sql.visitor.AppendSqlVisitor) Test(org.junit.Test)

Example 3 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor 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 4 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class DbmsChangeSetFilter method accepts.

@Override
public ChangeSetFilterResult accepts(ChangeSet changeSet) {
    if (database == null) {
        return new ChangeSetFilterResult(true, "No database connection, cannot evaluate dbms attribute", this.getClass());
    }
    List<SqlVisitor> visitorsToRemove = new ArrayList<SqlVisitor>();
    for (SqlVisitor visitor : changeSet.getSqlVisitors()) {
        if (!DatabaseList.definitionMatches(visitor.getApplicableDbms(), database, true)) {
            visitorsToRemove.add(visitor);
        }
    }
    changeSet.getSqlVisitors().removeAll(visitorsToRemove);
    String dbmsList;
    if (changeSet.getDbmsSet() == null || changeSet.getDbmsSet().size() == 0) {
        dbmsList = "all databases";
    } else {
        dbmsList = "'" + StringUtils.join(changeSet.getDbmsSet(), ", ") + "'";
    }
    if (DatabaseList.definitionMatches(changeSet.getDbmsSet(), database, true)) {
        return new ChangeSetFilterResult(true, "Database '" + database.getShortName() + "' matches " + dbmsList, this.getClass());
    } else {
        return new ChangeSetFilterResult(false, "Database '" + database.getShortName() + "' does not match " + dbmsList, this.getClass());
    }
}
Also used : ArrayList(java.util.ArrayList) SqlVisitor(liquibase.sql.visitor.SqlVisitor)

Example 5 with SqlVisitor

use of liquibase.sql.visitor.SqlVisitor in project liquibase by liquibase.

the class LabelChangeSetFilter method accepts.

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

Aggregations

SqlVisitor (liquibase.sql.visitor.SqlVisitor)10 ArrayList (java.util.ArrayList)6 MockExecutor (liquibase.sdk.executor.MockExecutor)4 AppendSqlVisitor (liquibase.sql.visitor.AppendSqlVisitor)4 Test (org.junit.Test)4 ContextExpression (liquibase.ContextExpression)2 Change (liquibase.change.Change)2 DbmsTargetedChange (liquibase.change.DbmsTargetedChange)2 CreateTableChange (liquibase.change.core.CreateTableChange)2 EmptyChange (liquibase.change.core.EmptyChange)2 RawSQLChange (liquibase.change.core.RawSQLChange)2 SqlStatement (liquibase.statement.SqlStatement)2 DropTableStatement (liquibase.statement.core.DropTableStatement)2 Labels (liquibase.Labels)1 ParsedNode (liquibase.parser.core.ParsedNode)1 ParsedNodeException (liquibase.parser.core.ParsedNodeException)1 PreconditionContainer (liquibase.precondition.core.PreconditionContainer)1 Sql (liquibase.sql.Sql)1