Search in sources :

Example 21 with LiquibaseException

use of liquibase.exception.LiquibaseException in project liquibase by liquibase.

the class XMLChangeLogSAXParserTest method testIgnoreDuplicateChangeSets.

@Test
public void testIgnoreDuplicateChangeSets() throws ChangeLogParseException, Exception {
    XMLChangeLogSAXParser xmlParser = new XMLChangeLogSAXParser();
    DatabaseChangeLog changeLog = xmlParser.parse("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/master.changelog.xml", new ChangeLogParameters(), new JUnitResourceAccessor());
    final List<ChangeSet> changeSets = new ArrayList<ChangeSet>();
    new ChangeLogIterator(changeLog).run(new ChangeSetVisitor() {

        @Override
        public Direction getDirection() {
            return Direction.FORWARD;
        }

        @Override
        public void visit(ChangeSet changeSet, DatabaseChangeLog databaseChangeLog, Database database, Set<ChangeSetFilterResult> filterResults) throws LiquibaseException {
            changeSets.add(changeSet);
        }
    }, new RuntimeEnvironment(new MockDatabase(), new Contexts(), new LabelExpression()));
    Assert.assertEquals(8, changeSets.size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(0).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(1).toString());
    Assert.assertEquals(1, changeSets.get(1).getContexts().getContexts().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(2).toString());
    Assert.assertEquals(1, changeSets.get(2).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(3).toString());
    Assert.assertEquals(2, changeSets.get(3).getLabels().getLabels().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog4.xml::1::testuser", changeSets.get(4).toString());
    Assert.assertEquals(1, changeSets.get(4).getDbmsSet().size());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog1.xml::1::testuser", changeSets.get(5).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog3.xml::1::testuser", changeSets.get(6).toString());
    Assert.assertEquals("liquibase/parser/core/xml/ignoreDuplicatedChangeLogs/included.changelog2.xml::1::testuser", changeSets.get(7).toString());
}
Also used : ChangeSetVisitor(liquibase.changelog.visitor.ChangeSetVisitor) RuntimeEnvironment(liquibase.RuntimeEnvironment) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) ArrayList(java.util.ArrayList) Contexts(liquibase.Contexts) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) ChangeLogIterator(liquibase.changelog.ChangeLogIterator) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) MockDatabase(liquibase.sdk.database.MockDatabase) Database(liquibase.database.Database) LabelExpression(liquibase.LabelExpression) MockDatabase(liquibase.sdk.database.MockDatabase) LiquibaseException(liquibase.exception.LiquibaseException) ChangeSetFilterResult(liquibase.changelog.filter.ChangeSetFilterResult) ChangeSet(liquibase.changelog.ChangeSet) Test(org.junit.Test)

Example 22 with LiquibaseException

use of liquibase.exception.LiquibaseException in project liquibase by liquibase.

the class Liquibase method reportUnexpectedChangeSets.

public void reportUnexpectedChangeSets(boolean verbose, Contexts contexts, LabelExpression labelExpression, Writer out) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    try {
        Collection<RanChangeSet> unexpectedChangeSets = listUnexpectedChangeSets(contexts, labelExpression);
        if (unexpectedChangeSets.size() == 0) {
            out.append(getDatabase().getConnection().getConnectionUserName());
            out.append("@");
            out.append(getDatabase().getConnection().getURL());
            out.append(" contains no unexpected changes!");
            out.append(StreamUtil.getLineSeparator());
        } else {
            out.append(String.valueOf(unexpectedChangeSets.size()));
            out.append(" unexpected changes were found in ");
            out.append(getDatabase().getConnection().getConnectionUserName());
            out.append("@");
            out.append(getDatabase().getConnection().getURL());
            out.append(StreamUtil.getLineSeparator());
            if (verbose) {
                for (RanChangeSet ranChangeSet : unexpectedChangeSets) {
                    out.append("     ").append(ranChangeSet.toString()).append(StreamUtil.getLineSeparator());
                }
            }
        }
        out.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
}
Also used : UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 23 with LiquibaseException

use of liquibase.exception.LiquibaseException in project liquibase by liquibase.

the class Liquibase method update.

public void update(int changesToApply, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    Executor oldTemplate = ExecutorService.getInstance().getExecutor(database);
    LoggingExecutor loggingExecutor = new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), output, database);
    ExecutorService.getInstance().setExecutor(database, loggingExecutor);
    outputHeader("Update " + changesToApply + " Change Sets Database Script");
    update(changesToApply, contexts, labelExpression);
    try {
        output.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
    resetServices();
    ExecutorService.getInstance().setExecutor(database, oldTemplate);
}
Also used : Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LoggingExecutor(liquibase.executor.LoggingExecutor) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 24 with LiquibaseException

use of liquibase.exception.LiquibaseException in project liquibase by liquibase.

the class Liquibase method rollback.

public void rollback(int changesToRollback, String rollbackScript, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException {
    changeLogParameters.setContexts(contexts);
    changeLogParameters.setLabels(labelExpression);
    Executor oldTemplate = ExecutorService.getInstance().getExecutor(database);
    ExecutorService.getInstance().setExecutor(database, new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), output, database));
    outputHeader("Rollback " + changesToRollback + " Change(s) Script");
    rollback(changesToRollback, rollbackScript, contexts, labelExpression);
    try {
        output.flush();
    } catch (IOException e) {
        throw new LiquibaseException(e);
    }
    ExecutorService.getInstance().setExecutor(database, oldTemplate);
    resetServices();
}
Also used : Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) LoggingExecutor(liquibase.executor.LoggingExecutor) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException)

Example 25 with LiquibaseException

use of liquibase.exception.LiquibaseException in project liquibase by liquibase.

the class Liquibase method executeRollbackScript.

protected void executeRollbackScript(String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException {
    final Executor executor = ExecutorService.getInstance().getExecutor(database);
    String rollbackScriptContents;
    try {
        Set<InputStream> streams = resourceAccessor.getResourcesAsStream(rollbackScript);
        if (streams == null || streams.size() == 0) {
            throw new LiquibaseException("Cannot find rollbackScript " + rollbackScript);
        } else if (streams.size() > 1) {
            throw new LiquibaseException("Found multiple rollbackScripts named " + rollbackScript);
        }
        rollbackScriptContents = StreamUtil.getStreamContents(streams.iterator().next());
    } catch (IOException e) {
        throw new LiquibaseException("Error reading rollbackScript " + executor + ": " + e.getMessage());
    }
    RawSQLChange rollbackChange = new RawSQLChange(rollbackScriptContents);
    rollbackChange.setSplitStatements(true);
    rollbackChange.setStripComments(true);
    try {
        executor.execute(rollbackChange);
    } catch (DatabaseException e) {
        e = new DatabaseException("Error executing rollback script. ChangeSets will still be marked as rolled back: " + e.getMessage(), e);
        System.err.println(e.getMessage());
        log.severe("Error executing rollback script", e);
        if (changeExecListener != null) {
            changeExecListener.runFailed(null, databaseChangeLog, database, e);
        }
    }
    database.commit();
}
Also used : RawSQLChange(liquibase.change.core.RawSQLChange) Executor(liquibase.executor.Executor) LoggingExecutor(liquibase.executor.LoggingExecutor) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

LiquibaseException (liquibase.exception.LiquibaseException)38 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)23 Executor (liquibase.executor.Executor)12 Liquibase (liquibase.Liquibase)10 LoggingExecutor (liquibase.executor.LoggingExecutor)10 BuildException (org.apache.tools.ant.BuildException)8 IOException (java.io.IOException)7 Database (liquibase.database.Database)7 Contexts (liquibase.Contexts)6 DatabaseException (liquibase.exception.DatabaseException)5 ArrayList (java.util.ArrayList)4 ResourceAccessor (liquibase.resource.ResourceAccessor)4 FileResource (org.apache.tools.ant.types.resources.FileResource)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 Writer (java.io.Writer)3 CatalogAndSchema (liquibase.CatalogAndSchema)3 OracleDatabase (liquibase.database.core.OracleDatabase)3 DiffOutputControl (liquibase.diff.output.DiffOutputControl)3 Test (org.junit.Test)3 File (java.io.File)2