1 | |
|
2 | |
|
3 | |
package org.liquibase.maven.plugins; |
4 | |
|
5 | |
import java.io.*; |
6 | |
import liquibase.resource.ResourceAccessor; |
7 | |
import liquibase.Liquibase; |
8 | |
import liquibase.database.Database; |
9 | |
import liquibase.exception.LiquibaseException; |
10 | |
import org.apache.maven.plugin.MojoExecutionException; |
11 | |
import org.apache.maven.plugin.MojoFailureException; |
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | 0 | public class LiquibaseMigrateSQL extends AbstractLiquibaseUpdateMojo { |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
protected File migrationSqlOutputFile; |
31 | |
|
32 | |
|
33 | |
private Writer outputWriter; |
34 | |
|
35 | |
@Override |
36 | |
public void configureFieldsAndValues(ResourceAccessor fo) throws MojoExecutionException, MojoFailureException { |
37 | 0 | getLog().warn( |
38 | |
"This plugin goal is DEPRICATED and will bre removed in a future " |
39 | |
+ "release, please use \"updateSQL\" instead of \"migrateSQL\"."); |
40 | 0 | super.configureFieldsAndValues(fo); |
41 | 0 | } |
42 | |
|
43 | |
@Override |
44 | |
protected boolean isPromptOnNonLocalDatabase() { |
45 | |
|
46 | |
|
47 | 0 | return false; |
48 | |
} |
49 | |
|
50 | |
@Override |
51 | |
protected void doUpdate(Liquibase liquibase) throws LiquibaseException { |
52 | 0 | if (changesToApply > 0) { |
53 | 0 | liquibase.update(changesToApply, contexts, outputWriter); |
54 | |
} else { |
55 | 0 | liquibase.update(contexts, outputWriter); |
56 | |
} |
57 | 0 | } |
58 | |
|
59 | |
@Override |
60 | |
protected Liquibase createLiquibase(ResourceAccessor fo, Database db) throws MojoExecutionException { |
61 | 0 | Liquibase liquibase = super.createLiquibase(fo, db); |
62 | |
|
63 | |
|
64 | |
try { |
65 | 0 | if (!migrationSqlOutputFile.exists()) { |
66 | |
|
67 | 0 | migrationSqlOutputFile.getParentFile().mkdirs(); |
68 | |
|
69 | 0 | if (!migrationSqlOutputFile.createNewFile()) { |
70 | 0 | throw new MojoExecutionException("Cannot create the migration SQL file; " |
71 | |
+ migrationSqlOutputFile.getAbsolutePath()); |
72 | |
} |
73 | |
} |
74 | 0 | outputWriter = new FileWriter(migrationSqlOutputFile); |
75 | 0 | } catch (IOException e) { |
76 | 0 | getLog().error(e); |
77 | 0 | throw new MojoExecutionException("Failed to create SQL output writer", e); |
78 | 0 | } |
79 | 0 | getLog().info("Output SQL Migration File: " + migrationSqlOutputFile.getAbsolutePath()); |
80 | 0 | return liquibase; |
81 | |
} |
82 | |
|
83 | |
@Override |
84 | |
protected void printSettings(String indent) { |
85 | 0 | super.printSettings(indent); |
86 | 0 | getLog().info(indent + "migrationSQLOutputFile: " + migrationSqlOutputFile); |
87 | 0 | } |
88 | |
|
89 | |
@Override |
90 | |
protected void cleanup(Database db) { |
91 | 0 | super.cleanup(db); |
92 | 0 | if (outputWriter != null) { |
93 | |
try { |
94 | 0 | outputWriter.close(); |
95 | 0 | } catch (IOException e) { |
96 | 0 | getLog().error(e); |
97 | 0 | } |
98 | |
} |
99 | 0 | } |
100 | |
} |