1 | |
package liquibase.dbdoc; |
2 | |
|
3 | |
import liquibase.Liquibase; |
4 | |
import liquibase.change.Change; |
5 | |
import liquibase.changelog.ChangeSet; |
6 | |
import liquibase.changelog.DatabaseChangeLog; |
7 | |
import liquibase.database.Database; |
8 | |
import liquibase.exception.DatabaseException; |
9 | |
import liquibase.exception.DatabaseHistoryException; |
10 | |
import liquibase.exception.MigrationFailedException; |
11 | |
import liquibase.executor.Executor; |
12 | |
import liquibase.executor.ExecutorService; |
13 | |
import liquibase.executor.LoggingExecutor; |
14 | |
|
15 | |
import java.io.File; |
16 | |
import java.io.FileWriter; |
17 | |
import java.io.IOException; |
18 | |
import java.util.List; |
19 | |
|
20 | |
public class PendingSQLWriter extends HTMLWriter { |
21 | |
|
22 | |
private DatabaseChangeLog databaseChangeLog; |
23 | |
|
24 | |
public PendingSQLWriter(File rootOutputDir, Database database, DatabaseChangeLog databaseChangeLog) { |
25 | 0 | super(new File(rootOutputDir, "pending"), database); |
26 | 0 | this.databaseChangeLog = databaseChangeLog; |
27 | 0 | } |
28 | |
|
29 | |
@Override |
30 | |
protected String createTitle(Object object) { |
31 | 0 | return "Pending SQL"; |
32 | |
} |
33 | |
|
34 | |
@Override |
35 | |
protected void writeBody(FileWriter fileWriter, Object object, List<Change> ranChanges, List<Change> changesToRun) |
36 | |
throws IOException, DatabaseHistoryException, DatabaseException { |
37 | |
|
38 | 0 | Executor oldTemplate = ExecutorService.getInstance().getExecutor(database); |
39 | 0 | LoggingExecutor loggingExecutor = new LoggingExecutor(ExecutorService.getInstance().getExecutor(database), |
40 | |
fileWriter, database); |
41 | 0 | ExecutorService.getInstance().setExecutor(database, loggingExecutor); |
42 | |
|
43 | |
try { |
44 | 0 | if (changesToRun.size() == 0) { |
45 | 0 | fileWriter.append("<b>NONE</b>"); |
46 | |
} |
47 | |
|
48 | 0 | fileWriter.append("<code><pre>"); |
49 | |
|
50 | 0 | ChangeSet lastRunChangeSet = null; |
51 | |
|
52 | 0 | for (Change change : changesToRun) { |
53 | 0 | ChangeSet thisChangeSet = change.getChangeSet(); |
54 | 0 | if (thisChangeSet.equals(lastRunChangeSet)) { |
55 | 0 | continue; |
56 | |
} |
57 | 0 | lastRunChangeSet = thisChangeSet; |
58 | 0 | String anchor = thisChangeSet.toString(false).replaceAll("\\W", "_"); |
59 | 0 | fileWriter.append("<a name='").append(anchor).append("'/>"); |
60 | |
try { |
61 | 0 | thisChangeSet.execute(databaseChangeLog, this.database); |
62 | 0 | } catch (MigrationFailedException e) { |
63 | 0 | fileWriter.append("EXECUTION ERROR: ").append(change.getChangeMetaData().getDescription()) |
64 | |
.append(": ").append(e.getMessage()).append("\n\n"); |
65 | 0 | } |
66 | 0 | } |
67 | 0 | fileWriter.append("</pre></code>"); |
68 | |
} finally { |
69 | 0 | ExecutorService.getInstance().setExecutor(database, oldTemplate); |
70 | 0 | } |
71 | 0 | } |
72 | |
|
73 | |
@Override |
74 | |
protected void writeCustomHTML(FileWriter fileWriter, Object object, List<Change> changes, Database database) |
75 | |
throws IOException { |
76 | 0 | } |
77 | |
} |