我試圖繞過Flyway issue 156(see here)的方法之一就是依靠Java遷移。爲此,我寫了一個基於the sample的數據庫,以建立數據庫的初始狀態。我怎樣才能讓Flyway Java Migration僅僅執行一個sql腳本?
首先,我搶的數據庫(MySQL的5.5)的初始狀態,並把它變成一個文本文件進行遷移閱讀:
mysqldump -u root -p myProject > db/migration/_V1__baseline.sql
然後,我寫一個Java遷移到該狀態裝入當它第一次創建一個新的數據庫:
package com.myCompany.myProject.migration;
import com.googlecode.flyway.core.migration.java.JavaMigration;
import org.springframework.jdbc.core.JdbcTemplate;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.springframework.dao.DataAccessException;
public class V1__baseline implements JavaMigration {
@Override
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
BufferedReader stream = null;
String sqlCode = "";
String line;
try {
stream = new BufferedReader(new FileReader("db/migration/_V1__baseline.sql"));
while ((line = stream.readLine()) != null) {
sqlCode += line + '\n';
}
jdbcTemplate.execute(sqlCode);
}catch(IOException e){
System.out.println("File error: "+ e.getMessage());
}catch(DataAccessException e){
System.out.println("Data access exception: "+ e.getMessage());
}catch(Exception e){
System.out.println("Generic Exception: "+ e.getMessage());
} finally {
if (stream != null) {
stream.close();
}
}
}
}
然後,嘗試一下:
mvn compile flyway:clean flyway:init flyway:migrate
而且結果:
[INFO] --- flyway-maven-plugin:1.5:migrate (default-cli) @ elysium-unity-server ---
[WARNING] Unable to find path for sql migrations: db/migration
[WARNING] Unable to find path for sql migrations: db/migration
[WARNING] Unable to find path for sql migrations: db/migration
[INFO] Validated 0 migrations (mode: ALL) (execution time 00:00.002s)
[INFO] Current schema version: 0
[INFO] Migrating to version 1
Data access exception: StatementCallback; bad SQL grammar [-- MySQL dump 10.13 Distrib 5.5.19, for Win64 (x86)
--
-- Host: localhost Database: myProject
-- ------------------------------------------------------
-- Server version 5.5.19
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @[email protected]@TIME_ZONE */;
... lots more lines ...
/*!40111 SET [email protected]_SQL_NOTES */;
-- Dump completed on 2012-02-03 12:51:33
]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COL' at line 8
所以它看起來像mysql語法錯誤。但是,語法是由我試圖提供給它的同一個數據庫生成的。所以我猜測中間一定會有東西擋在路上。有什麼建議麼?我想了解爲什麼這種確切的方法不起作用,但我也會感激地接受其他方式完成my ultimate goal的建議。
也許有'''字符需要轉義? – 2012-02-13 13:00:07