2013-02-19 2 views
3

인 메모리 H2 데이터베이스를 사용하여 일부 유닛/통합 테스트를 설정하려고합니다. 모든 것은 스프링으로 배선되었습니다. 이것은 dev에 MySQL을 사용하고 프로덕션 환경에서 사용하는 기존 앱용입니다.MySQL에서 H2로 테이블 생성을위한 DDL 변환

이제 데이터베이스 구조를 만들려면 DDL SQL 스크립트가 필요합니다. MySQL에서 DDL 문을 쉽게 내보낼 수 있습니다. 그러나 문법상의 차이점이 무엇인지 모르겠다. 적어도 나는 engine=InnoDB과 같은 것들이 반드시 있어야한다고 확신한다.

알고 있어야하는 다른 구문 차이점이 있습니까?

DDL 문을 MySQL 구문에서 H2 구문으로 자동 변환 할 수있는 도구가 있습니까?

답변

0

저는 최근 프로젝트에서이 작업을 신속하게 수행 했으므로이 작업을 가장 좋거나 가장 깨끗한 솔루션으로 간주해서는 안됩니다. 또한 필자는 단위 기반 통합 테스트를 위해 이것을 사용했음을 주목해야합니다. 이상적으로 나는 사람들을 그것에 추가 할 수 있도록 다음 몇 주 안에 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(); 
    } 
} 
관련 문제