Search in sources :

Example 6 with PreconditionContainer

use of liquibase.precondition.core.PreconditionContainer in project liquibase by liquibase.

the class DatabaseChangeLog method include.

public boolean include(String fileName, boolean isRelativePath, ResourceAccessor resourceAccessor, ContextExpression includeContexts) throws LiquibaseException {
    if (fileName.equalsIgnoreCase(".svn") || fileName.equalsIgnoreCase("cvs")) {
        return false;
    }
    String relativeBaseFileName = this.getPhysicalFilePath();
    if (isRelativePath) {
        // workaround for FilenameUtils.normalize() returning null for relative paths like ../conf/liquibase.xml
        String tempFile = FilenameUtils.concat(FilenameUtils.getFullPath(relativeBaseFileName), fileName);
        if (tempFile != null && new File(tempFile).exists() == true) {
            fileName = tempFile;
        } else {
            fileName = FilenameUtils.getFullPath(relativeBaseFileName) + fileName;
        }
    }
    DatabaseChangeLog changeLog;
    try {
        DatabaseChangeLog rootChangeLog = ROOT_CHANGE_LOG.get();
        if (rootChangeLog == null) {
            ROOT_CHANGE_LOG.set(this);
        }
        DatabaseChangeLog parentChangeLog = PARENT_CHANGE_LOG.get();
        PARENT_CHANGE_LOG.set(this);
        try {
            ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser(fileName, resourceAccessor);
            changeLog = parser.parse(fileName, changeLogParameters, resourceAccessor);
            changeLog.setIncludeContexts(includeContexts);
        } finally {
            if (rootChangeLog == null) {
                ROOT_CHANGE_LOG.remove();
            }
            if (parentChangeLog == null) {
                PARENT_CHANGE_LOG.remove();
            } else {
                PARENT_CHANGE_LOG.set(parentChangeLog);
            }
        }
    } catch (UnknownChangelogFormatException e) {
        if (StringUtils.trimToEmpty(fileName).matches("\\.\\w+$")) {
            LogFactory.getInstance().getLog().warning("included file " + relativeBaseFileName + "/" + fileName + " is not a recognized file type");
        }
        return false;
    }
    PreconditionContainer preconditions = changeLog.getPreconditions();
    if (preconditions != null) {
        if (null == this.getPreconditions()) {
            this.setPreconditions(new PreconditionContainer());
        }
        this.getPreconditions().addNestedPrecondition(preconditions);
    }
    for (ChangeSet changeSet : changeLog.getChangeSets()) {
        addChangeSet(changeSet);
    }
    return true;
}
Also used : PreconditionContainer(liquibase.precondition.core.PreconditionContainer) ChangeLogParser(liquibase.parser.ChangeLogParser) UnknownChangelogFormatException(liquibase.exception.UnknownChangelogFormatException) File(java.io.File)

Example 7 with PreconditionContainer

use of liquibase.precondition.core.PreconditionContainer in project liquibase by liquibase.

the class ValidatingVisitorPreConditionsTest method testPreConditionsForOracleOnMSSQLWithPreconditionTag.

/**
     * Test only the precondition tag with a precondition requiring oracle but
     * giving a MSSQL database.
     */
@Test
public void testPreConditionsForOracleOnMSSQLWithPreconditionTag() {
    // create the pre condition
    PreconditionContainer preCondition = new PreconditionContainer();
    preCondition.setOnFail(PreconditionContainer.FailOption.MARK_RAN.toString());
    DBMSPrecondition dbmsPrecondition = new DBMSPrecondition();
    dbmsPrecondition.setType("oracle");
    preCondition.addNestedPrecondition(dbmsPrecondition);
    changeSet1.setPreconditions(preCondition);
    MSSQLDatabase mssqlDb = new MSSQLDatabase() {

        @Override
        public List<RanChangeSet> getRanChangeSetList() throws DatabaseException {
            return new ArrayList<RanChangeSet>();
        }

        @Override
        public void rollback() throws DatabaseException {
        //super.rollback();
        }
    };
    boolean failedExceptionThrown = false;
    boolean errorExceptionThrown = false;
    try {
        preCondition.check(mssqlDb, changeLog, changeSet1);
    } catch (PreconditionFailedException ex) {
        failedExceptionThrown = true;
    } catch (PreconditionErrorException ex) {
        errorExceptionThrown = true;
    }
    assertTrue(failedExceptionThrown);
    assertFalse(errorExceptionThrown);
}
Also used : PreconditionContainer(liquibase.precondition.core.PreconditionContainer) DBMSPrecondition(liquibase.precondition.core.DBMSPrecondition) ArrayList(java.util.ArrayList) PreconditionFailedException(liquibase.exception.PreconditionFailedException) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) RanChangeSet(liquibase.changelog.RanChangeSet) PreconditionErrorException(liquibase.exception.PreconditionErrorException) Test(org.junit.Test)

Example 8 with PreconditionContainer

use of liquibase.precondition.core.PreconditionContainer in project liquibase by liquibase.

the class ValidatingVisitorPreConditionsTest method testPreConditionsForOracleOnMSSQLWithChangeLog.

/**
     * Test the same precondition from a changelog with mssql database, this
     * should not fail on the validation but just mark is as handled.
     */
@Test
public void testPreConditionsForOracleOnMSSQLWithChangeLog() {
    // create the pre condition
    PreconditionContainer preCondition = new PreconditionContainer();
    preCondition.setOnFail(PreconditionContainer.FailOption.MARK_RAN.toString());
    DBMSPrecondition dbmsPrecondition = new DBMSPrecondition();
    dbmsPrecondition.setType("oracle");
    preCondition.addNestedPrecondition(dbmsPrecondition);
    changeSet1.setPreconditions(preCondition);
    MSSQLDatabase mssqlDb = new MSSQLDatabase() {

        @Override
        public List<RanChangeSet> getRanChangeSetList() throws DatabaseException {
            return new ArrayList<RanChangeSet>();
        }

        @Override
        public void rollback() throws DatabaseException {
        //super.rollback();
        }
    };
    String[] empty = {};
    boolean exceptionThrown = false;
    try {
        // call the validate which gives the error
        changeLog.validate(mssqlDb, empty);
    } catch (LiquibaseException ex) {
        System.out.println(ex.getMessage());
        exceptionThrown = true;
    }
    assertFalse(exceptionThrown);
}
Also used : PreconditionContainer(liquibase.precondition.core.PreconditionContainer) DBMSPrecondition(liquibase.precondition.core.DBMSPrecondition) ArrayList(java.util.ArrayList) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) LiquibaseException(liquibase.exception.LiquibaseException) RanChangeSet(liquibase.changelog.RanChangeSet) Test(org.junit.Test)

Aggregations

PreconditionContainer (liquibase.precondition.core.PreconditionContainer)8 ArrayList (java.util.ArrayList)3 RanChangeSet (liquibase.changelog.RanChangeSet)3 LiquibaseException (liquibase.exception.LiquibaseException)3 DBMSPrecondition (liquibase.precondition.core.DBMSPrecondition)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ContextExpression (liquibase.ContextExpression)2 Labels (liquibase.Labels)2 EmptyChange (liquibase.change.core.EmptyChange)2 RawSQLChange (liquibase.change.core.RawSQLChange)2 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)2 UnknownChangelogFormatException (liquibase.exception.UnknownChangelogFormatException)2 ParsedNodeException (liquibase.parser.core.ParsedNodeException)2 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1