Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
181 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...amcircle.storage/src/org/kompiro/jamcircle/storage/service/internal/DatabaseMigrator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters