01: package liquibase.ant;
02:
03: import liquibase.Liquibase;
04: import liquibase.UIFactory;
05: import org.apache.tools.ant.BuildException;
06:
07: import java.io.Writer;
08:
09: /**
10: * Ant task for migrating a database forward.
11: */
12: public class DatabaseUpdateTask extends BaseLiquibaseTask {
13: private boolean dropFirst = false;
14:
15: public boolean isDropFirst() {
16: return dropFirst;
17: }
18:
19: public void setDropFirst(boolean dropFirst) {
20: this .dropFirst = dropFirst;
21: }
22:
23: public void execute() throws BuildException {
24: if (!shouldRun()) {
25: return;
26: }
27:
28: Liquibase liquibase = null;
29: try {
30: liquibase = createLiquibase();
31:
32: if (isPromptOnNonLocalDatabase()
33: && !liquibase.isSafeToRunMigration()
34: && UIFactory.getInstance().getFacade()
35: .promptForNonLocalDatabase(
36: liquibase.getDatabase())) {
37: throw new BuildException(
38: "Chose not to run against non-production database");
39: }
40:
41: if (isDropFirst()) {
42: liquibase.dropAll();
43: }
44:
45: Writer writer = createOutputWriter();
46: if (writer == null) {
47: liquibase.update(getContexts());
48: } else {
49: liquibase.update(getContexts(), writer);
50: writer.flush();
51: writer.close();
52: }
53:
54: } catch (Exception e) {
55: throw new BuildException(e);
56: } finally {
57: closeDatabase(liquibase);
58: }
59: }
60: }
|