對於最近的一個項目,我已經很快了解了這一點,而且絕對不應該把它當作最好或最乾淨的解決方案。還應該指出,我純粹用於基於單元的集成測試。理想情況下,我會在接下來的幾周內在github上提供這個功能,以便人們可以添加它,但同時它可能會有所幫助。 Java解決方案:
/**
* Designed to go through MySQL schema produced from forward engineering tools and normalize slightly to
* work with H2 for testing. This only works on the SQL constructs themselves and is not able to
* detect errors such as constraints with the same name.
*
* Assumptions
* - INDEX is created separately from main SQL body
* - Incompatible key words such as order are not used
* - Assumes the name of a constraint is not duplicated
* @author jpgough
*
*/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MySqlToH2 {
public static String convert(String filePath) throws IOException {
String[] rawSQL = new String(Files.readAllBytes(Paths.get(filePath))).split("\\n");
StringBuilder builder = new StringBuilder();
for(String line : rawSQL) {
if(line.contains("SET")) {
continue;
} else if(line.contains("INDEX")) {
continue;
} else if(line.contains("IF NOT EXISTS")) {
line = line.replaceAll("IF NOT EXISTS", "");
} else if(line.contains("--")) {
continue;
} else if(line.contains("ENGINE")) {
line = ";";
}
line = line.replace("`", "");
builder.append(line).append("\n");
}
return builder.toString();
}
}
來源
2013-04-11 13:09:16
Jim