/
DatabaseMigrator.java
127 lines (115 loc) · 3.11 KB
/
DatabaseMigrator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package org.kompiro.jamcircle.storage.service.internal;
import java.io.*;
import org.h2.tools.RunScript;
public class DatabaseMigrator {
private static final String TEMP_SCRIPT = "backup.sql";
private PrintStream sysOut = System.out;
private boolean quiet;
public DatabaseMigrator() {
}
/**
* Migrate a database.
*
* @param oldH2Jar
* the old JAR file
* @param file
* the database file (must end with .data.db) or directory
* @param recursive
* if the file parameter is in fact a directory (in which
* case the directory is scanned recursively)
* @param user
* the user name of the database
* @param password
* the password
* @param runQuiet
* to run in quiet mode
* @throws Exception
* if conversion fails
*/
public void execute(File oldH2Jar, File file, boolean recursive, String user, String password, boolean runQuiet)
throws Exception {
String pathToJavaExe = getJavaExecutablePath();
this.quiet = runQuiet;
if (file.isDirectory() && recursive) {
for (File f : file.listFiles()) {
execute(oldH2Jar, f, recursive, user, password, runQuiet);
}
return;
}
if (!file.getName().endsWith(".data.db")) {
return;
}
println("Migrating " + file.getName());
String url = "jdbc:h2:" + file.getAbsolutePath();
url = url.substring(0, url.length() - ".data.db".length());
exec(new String[] {
pathToJavaExe,
"-Xmx128m",
"-cp", oldH2Jar.getAbsolutePath(),
"org.h2.tools.Script",
"-script", TEMP_SCRIPT,
"-url", url,
"-user", user,
"-password", password });
file.renameTo(new File(file.getAbsoluteFile() + ".backup"));
RunScript.execute(url, user, password, TEMP_SCRIPT, "UTF-8", true);
new File(TEMP_SCRIPT).delete();
}
private String getJavaExecutablePath() {
String pathToJava;
if (File.separator.equals("\\")) {
pathToJava = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java.exe";
} else {
pathToJava = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
}
if (!new File(pathToJava).exists()) {
// Fallback to old behaviour
pathToJava = "java";
}
return pathToJava;
}
private void println(String s) {
if (!quiet) {
sysOut.println(s);
}
}
private void print(String s) {
if (!quiet) {
sysOut.print(s);
}
}
private int exec(String[] command) {
try {
for (String c : command) {
print(c + " ");
}
println("");
Process p = Runtime.getRuntime().exec(command);
copyInThread(p.getInputStream(), quiet ? null : sysOut);
copyInThread(p.getErrorStream(), quiet ? null : sysOut);
p.waitFor();
return p.exitValue();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private void copyInThread(final InputStream in, final OutputStream out) {
new Thread() {
public void run() {
try {
while (true) {
int x = in.read();
if (x < 0) {
return;
}
if (out != null) {
out.write(x);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}.start();
}
}