Wednesday, 15 August 2012

syntax - Translate DDL for tables creation from MySQL to H2 -



syntax - Translate DDL for tables creation from MySQL to H2 -

i trying set-up unit/integration tests using in-memory h2 database, whole thing wired spring. existing app uses mysql dev , in production.

now, need ddl sql script create database construction in h2. can export ddl statements mysql. don't know differences in syntax - @ to the lowest degree pretty sure things such engine=innodb must go.

are there other syntax differences should aware of ?

is there tool can automatically translate de ddl statements mysql syntax h2 syntax ?

i've had quick go @ recent project, , should not taken best or cleanest solution. should noted used purely unit based integration testing. ideally i'll on github in next few weeks people can add together it, maybe in meantime help. solution in java:

/** * designed go through mysql schema produced forwards engineering science tools , normalize * work h2 testing. works on sql constructs , not able * observe errors such constraints same name. * * assumptions * - index created separately main sql body * - incompatible key words such order not used * - assumes name of constraint 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"); } homecoming builder.tostring(); } }

mysql syntax h2

No comments:

Post a Comment