View Javadoc

1   package liquibase.integration.ant;
2   
3   import liquibase.Liquibase;
4   import liquibase.util.ui.UIFactory;
5   import org.apache.tools.ant.BuildException;
6   
7   import java.io.Writer;
8   
9   /**
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      @Override
24      public void execute() throws BuildException {
25          if (!shouldRun()) {
26              return;
27          }
28  
29          super.execute();
30  
31          Liquibase liquibase = null;
32          try {
33              liquibase = createLiquibase();
34  
35              if (isPromptOnNonLocalDatabase() && !liquibase.isSafeToRunMigration()
36                      && UIFactory.getInstance().getFacade().promptForNonLocalDatabase(liquibase.getDatabase())) {
37                  throw new BuildException("Chose not to run against non-production database");
38              }
39  
40              Writer writer = createOutputWriter();
41              if (writer == null) {
42                  if (isDropFirst()) {
43                      liquibase.dropAll();
44                  }
45  
46                  liquibase.update(getContexts());
47              } else {
48                  if (isDropFirst()) {
49                      throw new BuildException("Cannot dropFirst when outputting update SQL");
50                  }
51                  liquibase.update(getContexts(), writer);
52                  writer.flush();
53                  writer.close();
54              }
55  
56          } catch (Exception e) {
57              throw new BuildException(e);
58          } finally {
59              closeDatabase(liquibase);
60          }
61      }
62  }