All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kualigan.tools.ant.tasks.DatabaseUpdateTask Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package org.kualigan.tools.ant.tasks;

import liquibase.integration.ant.*;
import liquibase.Liquibase;
import liquibase.util.ui.UIFactory;
import org.apache.tools.ant.BuildException;

import java.io.Writer;

/**
 * Ant task for migrating a database forward.
 */
public class DatabaseUpdateTask extends BaseLiquibaseTask {
    private boolean dropFirst = false;

    public boolean isDropFirst() {
        return dropFirst;
    }

    public void setDropFirst(boolean dropFirst) {
        this.dropFirst = dropFirst;
    }

    @Override
    public void execute() throws BuildException {
        if (!shouldRun()) {
            return;
        }

        super.execute();

        Liquibase liquibase = null;
        try {
            liquibase = createLiquibase();

            if (isPromptOnNonLocalDatabase()
                    && !liquibase.isSafeToRunMigration()
                    && UIFactory.getInstance().getFacade().promptForNonLocalDatabase(liquibase.getDatabase())) {
                throw new BuildException("Chose not to run against non-production database");
            }

            Writer writer = createOutputWriter();
            if (writer == null) {
                if (isDropFirst()) {
                    liquibase.dropAll();
                }

                liquibase.update(getContexts());
            } else {
                if (isDropFirst()) {
                    throw new BuildException("Cannot dropFirst when outputting update SQL");
                }
                liquibase.update(getContexts(), writer);
                writer.flush();
                writer.close();
            }

        } catch (Exception e) {
            throw new BuildException(e);
        } finally {
            closeDatabase(liquibase);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy