我正在尝试使用内存中的H2数据库来设置一些单元/集成测试,整个过程都与Spring绑定在一起。这适用于使用MySQL进行开发和生产的现有应用程序。
现在,我需要一个DDL SQL脚本来在H2中创建我的数据库结构。我可以很容易地从MySQL中导出DDL语句。但是我不知道语法上的区别是什么--至少我很确定像engine=InnoDB这样的东西必须去掉。
还有没有我应该注意的其他语法差异?
有没有工具可以自动将DDL语句从MySQL语法转换成H2语法?
发布于 2013-04-11 21:09:16
我在最近的一个项目中对此进行了快速的尝试,这绝对不应该被认为是最好或最干净的解决方案。还应该注意的是,我使用它纯粹是为了进行基于单元的集成测试。理想情况下,我会在接下来的几周内把它放到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();
}
}https://stackoverflow.com/questions/14963138
复制相似问题