Search in sources :

Example 1 with LiquibaseException

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

the class DatabaseChangeLog method handleChildNode.

protected void handleChildNode(ParsedNode node, ResourceAccessor resourceAccessor) throws ParsedNodeException, SetupException {
    expandExpressions(node);
    String nodeName = node.getName();
    if (nodeName.equals("changeSet")) {
        this.addChangeSet(createChangeSet(node, resourceAccessor));
    } else if (nodeName.equals("include")) {
        String path = node.getChildValue(null, "file", String.class);
        if (path == null) {
            throw new UnexpectedLiquibaseException("No 'file' attribute on 'include'");
        }
        path = path.replace('\\', '/');
        ContextExpression includeContexts = new ContextExpression(node.getChildValue(null, "context", String.class));
        try {
            include(path, node.getChildValue(null, "relativeToChangelogFile", false), resourceAccessor, includeContexts);
        } catch (LiquibaseException e) {
            throw new SetupException(e);
        }
    } else if (nodeName.equals("includeAll")) {
        String path = node.getChildValue(null, "path", String.class);
        String resourceFilterDef = node.getChildValue(null, "filter", String.class);
        if (resourceFilterDef == null) {
            resourceFilterDef = node.getChildValue(null, "resourceFilter", String.class);
        }
        IncludeAllFilter resourceFilter = null;
        if (resourceFilterDef != null) {
            try {
                resourceFilter = (IncludeAllFilter) Class.forName(resourceFilterDef).newInstance();
            } catch (Exception e) {
                throw new SetupException(e);
            }
        }
        String resourceComparatorDef = node.getChildValue(null, "resourceComparator", String.class);
        Comparator<?> resourceComparator = null;
        if (resourceComparatorDef != null) {
            try {
                resourceComparator = (Comparator<?>) Class.forName(resourceComparatorDef).newInstance();
            } catch (Exception e) {
                //take default comparator
                LogFactory.getInstance().getLog().info("no resourceComparator defined - taking default implementation");
                resourceComparator = getStandardChangeLogComparator();
            }
        }
        ContextExpression includeContexts = new ContextExpression(node.getChildValue(null, "context", String.class));
        includeAll(path, node.getChildValue(null, "relativeToChangelogFile", false), resourceFilter, node.getChildValue(null, "errorIfMissingOrEmpty", true), getStandardChangeLogComparator(), resourceAccessor, includeContexts);
    } else if (nodeName.equals("preConditions")) {
        this.preconditionContainer = new PreconditionContainer();
        try {
            this.preconditionContainer.load(node, resourceAccessor);
        } catch (ParsedNodeException e) {
            e.printStackTrace();
        }
    } else if (nodeName.equals("property")) {
        try {
            String context = node.getChildValue(null, "context", String.class);
            String dbms = node.getChildValue(null, "dbms", String.class);
            String labels = node.getChildValue(null, "labels", String.class);
            Boolean global = node.getChildValue(null, "global", Boolean.class);
            if (global == null) {
                // okay behave like liquibase < 3.4 and set global == true
                global = true;
            }
            String file = node.getChildValue(null, "file", String.class);
            if (file == null) {
                // direct referenced property, no file
                String name = node.getChildValue(null, "name", String.class);
                String value = node.getChildValue(null, "value", String.class);
                this.changeLogParameters.set(name, value, context, labels, dbms, global, this);
            } else {
                // read properties from the file
                Properties props = new Properties();
                InputStream propertiesStream = StreamUtil.singleInputStream(file, resourceAccessor);
                if (propertiesStream == null) {
                    LogFactory.getInstance().getLog().info("Could not open properties file " + file);
                } else {
                    props.load(propertiesStream);
                    for (Map.Entry entry : props.entrySet()) {
                        this.changeLogParameters.set(entry.getKey().toString(), entry.getValue().toString(), context, labels, dbms, global, this);
                    }
                }
            }
        } catch (IOException e) {
            throw new ParsedNodeException(e);
        }
    }
}
Also used : PreconditionContainer(liquibase.precondition.core.PreconditionContainer) InputStream(java.io.InputStream) ContextExpression(liquibase.ContextExpression) IOException(java.io.IOException) ParsedNodeException(liquibase.parser.core.ParsedNodeException) SetupException(liquibase.exception.SetupException) UnknownChangelogFormatException(liquibase.exception.UnknownChangelogFormatException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ValidationFailedException(liquibase.exception.ValidationFailedException) LiquibaseException(liquibase.exception.LiquibaseException) SetupException(liquibase.exception.SetupException) ParsedNodeException(liquibase.parser.core.ParsedNodeException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 2 with LiquibaseException

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

the class OfflineChangeLogHistoryService method getNextSequenceValue.

@Override
public int getNextSequenceValue() throws LiquibaseException {
    if (lastChangeSetSequenceValue == null) {
        lastChangeSetSequenceValue = 0;
        Reader reader = null;
        try {
            reader = new InputStreamReader(new FileInputStream(this.changeLogFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
            CSVReader csvReader = new CSVReader(reader);
            //skip header line
            String[] line = csvReader.readNext();
            List<RanChangeSet> returnList = new ArrayList<RanChangeSet>();
            while ((line = csvReader.readNext()) != null) {
                try {
                    lastChangeSetSequenceValue = Integer.valueOf(line[COLUMN_ORDEREXECUTED]);
                } catch (NumberFormatException ignore) {
                }
            }
        } catch (Exception ignore) {
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException ignore) {
                }
            }
        }
    }
    return ++lastChangeSetSequenceValue;
}
Also used : GlobalConfiguration(liquibase.configuration.GlobalConfiguration) CSVReader(liquibase.util.csv.CSVReader) CSVReader(liquibase.util.csv.CSVReader) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 3 with LiquibaseException

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

the class AbstractDatabaseDiffTask method getDiffResult.

protected DiffResult getDiffResult() {
    Liquibase liquibase = getLiquibase();
    Database targetDatabase = liquibase.getDatabase();
    Database referenceDatabase = createDatabaseFromType(referenceDatabaseType);
    CatalogAndSchema targetCatalogAndSchema = buildCatalogAndSchema(targetDatabase);
    CatalogAndSchema referenceCatalogAndSchema = buildCatalogAndSchema(referenceDatabase);
    CompareControl.SchemaComparison[] schemaComparisons = { new CompareControl.SchemaComparison(referenceCatalogAndSchema, targetCatalogAndSchema) };
    SnapshotGeneratorFactory snapshotGeneratorFactory = SnapshotGeneratorFactory.getInstance();
    DatabaseSnapshot referenceSnapshot;
    try {
        referenceSnapshot = snapshotGeneratorFactory.createSnapshot(referenceDatabase.getDefaultSchema(), referenceDatabase, new SnapshotControl(referenceDatabase, diffTypes));
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to create a DatabaseSnapshot. " + e.toString(), e);
    }
    CompareControl compareControl = new CompareControl(schemaComparisons, referenceSnapshot.getSnapshotControl().getTypesToInclude());
    try {
        return liquibase.diff(referenceDatabase, targetDatabase, compareControl);
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to diff databases. " + e.toString(), e);
    }
}
Also used : Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) CompareControl(liquibase.diff.compare.CompareControl) SnapshotGeneratorFactory(liquibase.snapshot.SnapshotGeneratorFactory) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException) CatalogAndSchema(liquibase.CatalogAndSchema) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl)

Example 4 with LiquibaseException

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

the class BaseLiquibaseTask method execute.

@Override
public final void execute() throws BuildException {
    super.execute();
    log("Starting Liquibase.", Project.MSG_INFO);
    classLoader = getProject().createClassLoader(classpath);
    classLoader.setParent(this.getClass().getClassLoader());
    classLoader.setThreadContextLoader();
    validateParameters();
    Database database = null;
    try {
        ResourceAccessor resourceAccessor = createResourceAccessor(classLoader);
        database = createDatabaseFromType(databaseType);
        liquibase = new Liquibase(getChangeLogFile(), resourceAccessor, database);
        if (changeLogParameters != null) {
            changeLogParameters.applyParameters(liquibase);
        }
        if (isPromptOnNonLocalDatabase() && !liquibase.isSafeToRunUpdate() && UIFactory.getInstance().getFacade().promptForNonLocalDatabase(liquibase.getDatabase())) {
            log("User chose not to run task against a non-local database.", Project.MSG_INFO);
            return;
        }
        if (shouldRun()) {
            executeWithLiquibaseClassloader();
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to initialize Liquibase. " + e.toString(), e);
    } finally {
        closeDatabase(database);
        classLoader.resetThreadContextLoader();
        classLoader.cleanup();
        classLoader = null;
    }
}
Also used : Liquibase(liquibase.Liquibase) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) Database(liquibase.database.Database) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException)

Example 5 with LiquibaseException

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

the class DBDocTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    File outputDirFile = outputDirectory.getFile();
    if (!outputDirFile.exists()) {
        boolean success = outputDirFile.mkdirs();
        if (!success) {
            throw new BuildException("Unable to create output directory.");
        }
    }
    if (!outputDirFile.isDirectory()) {
        throw new BuildException("Output path is not a directory.");
    }
    Liquibase liquibase = getLiquibase();
    try {
        if (contexts != null) {
            liquibase.generateDocumentation(outputDirectory.toString(), contexts);
        } else {
            liquibase.generateDocumentation(outputDirectory.toString());
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Liquibase encountered an error while creating database documentation. " + e.toString(), e);
    }
}
Also used : Liquibase(liquibase.Liquibase) BuildException(org.apache.tools.ant.BuildException) LiquibaseException(liquibase.exception.LiquibaseException) File(java.io.File)

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