1 | |
package org.liquibase.maven.plugins; |
2 | |
|
3 | |
import java.io.File; |
4 | |
import java.io.FileWriter; |
5 | |
import java.io.IOException; |
6 | |
import java.io.Writer; |
7 | |
import java.text.DateFormat; |
8 | |
import java.text.ParseException; |
9 | |
import java.text.SimpleDateFormat; |
10 | |
|
11 | |
import liquibase.Liquibase; |
12 | |
import liquibase.database.Database; |
13 | |
import liquibase.exception.LiquibaseException; |
14 | |
import liquibase.resource.ResourceAccessor; |
15 | |
|
16 | |
import org.apache.maven.plugin.MojoExecutionException; |
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | 0 | public class LiquibaseRollbackSQL extends LiquibaseRollback { |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
protected File migrationSqlOutputFile; |
35 | |
|
36 | |
|
37 | |
private Writer outputWriter; |
38 | |
|
39 | |
@Override |
40 | |
protected boolean isPromptOnNonLocalDatabase() { |
41 | |
|
42 | |
|
43 | 0 | return false; |
44 | |
} |
45 | |
|
46 | |
@Override |
47 | |
protected Liquibase createLiquibase(ResourceAccessor fo, Database db) throws MojoExecutionException { |
48 | 0 | Liquibase liquibase = super.createLiquibase(fo, db); |
49 | |
|
50 | |
|
51 | |
try { |
52 | 0 | if (!migrationSqlOutputFile.exists()) { |
53 | |
|
54 | 0 | migrationSqlOutputFile.getParentFile().mkdirs(); |
55 | |
|
56 | 0 | if (!migrationSqlOutputFile.createNewFile()) { |
57 | 0 | throw new MojoExecutionException("Cannot create the migration SQL file; " |
58 | |
+ migrationSqlOutputFile.getAbsolutePath()); |
59 | |
} |
60 | |
} |
61 | 0 | outputWriter = new FileWriter(migrationSqlOutputFile); |
62 | 0 | } catch (IOException e) { |
63 | 0 | getLog().error(e); |
64 | 0 | throw new MojoExecutionException("Failed to create SQL output writer", e); |
65 | 0 | } |
66 | 0 | getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath()); |
67 | 0 | return liquibase; |
68 | |
} |
69 | |
|
70 | |
@Override |
71 | |
protected void printSettings(String indent) { |
72 | 0 | super.printSettings(indent); |
73 | 0 | getLog().info(indent + "migrationSQLOutputFile: " + migrationSqlOutputFile); |
74 | 0 | } |
75 | |
|
76 | |
@Override |
77 | |
protected void cleanup(Database db) { |
78 | 0 | super.cleanup(db); |
79 | 0 | if (outputWriter != null) { |
80 | |
try { |
81 | 0 | outputWriter.close(); |
82 | 0 | } catch (IOException e) { |
83 | 0 | getLog().error(e); |
84 | 0 | } |
85 | |
} |
86 | 0 | } |
87 | |
|
88 | |
@Override |
89 | |
protected void performLiquibaseTask(Liquibase liquibase) throws LiquibaseException { |
90 | 0 | switch (type) { |
91 | |
case COUNT: { |
92 | 0 | liquibase.rollback(rollbackCount, contexts, outputWriter); |
93 | 0 | break; |
94 | |
} |
95 | |
case DATE: { |
96 | 0 | DateFormat format = DateFormat.getDateInstance(); |
97 | |
try { |
98 | 0 | liquibase.rollback(format.parse(rollbackDate), contexts, outputWriter); |
99 | 0 | } catch (ParseException e) { |
100 | 0 | String message = "Error parsing rollbackDate: " + e.getMessage(); |
101 | 0 | if (format instanceof SimpleDateFormat) { |
102 | 0 | message += "\nDate must match pattern: " + ((SimpleDateFormat) format).toPattern(); |
103 | |
} |
104 | 0 | throw new LiquibaseException(message, e); |
105 | 0 | } |
106 | |
break; |
107 | |
} |
108 | |
case TAG: { |
109 | 0 | liquibase.rollback(rollbackTag, contexts, outputWriter); |
110 | 0 | break; |
111 | |
} |
112 | |
default: { |
113 | 0 | throw new IllegalStateException("Unexpected rollback type, " + type); |
114 | |
} |
115 | |
} |
116 | 0 | } |
117 | |
} |