1 package com.rsmart.kuali.tools.ant.tasks;
2
3 import liquibase.integration.ant.*;
4 import liquibase.Liquibase;
5 import liquibase.util.ui.UIFactory;
6 import org.apache.tools.ant.BuildException;
7
8 import java.io.Writer;
9
10
11
12
13 public class DatabaseUpdateTask extends BaseLiquibaseTask {
14 private boolean dropFirst = false;
15
16 public boolean isDropFirst() {
17 return dropFirst;
18 }
19
20 public void setDropFirst(boolean dropFirst) {
21 this.dropFirst = dropFirst;
22 }
23
24 @Override
25 public void execute() throws BuildException {
26 if (!shouldRun()) {
27 return;
28 }
29
30 super.execute();
31
32 Liquibase liquibase = null;
33 try {
34 liquibase = createLiquibase();
35
36 if (isPromptOnNonLocalDatabase()
37 && !liquibase.isSafeToRunMigration()
38 && UIFactory.getInstance().getFacade().promptForNonLocalDatabase(liquibase.getDatabase())) {
39 throw new BuildException("Chose not to run against non-production database");
40 }
41
42 Writer writer = createOutputWriter();
43 if (writer == null) {
44 if (isDropFirst()) {
45 liquibase.dropAll();
46 }
47
48 liquibase.update(getContexts());
49 } else {
50 if (isDropFirst()) {
51 throw new BuildException("Cannot dropFirst when outputting update SQL");
52 }
53 liquibase.update(getContexts(), writer);
54 writer.flush();
55 writer.close();
56 }
57
58 } catch (Exception e) {
59 throw new BuildException(e);
60 } finally {
61 closeDatabase(liquibase);
62 }
63 }
64 }