gpt4 book ai didi

mysql - 如何仅执行 sql 脚本才能获得 Flyway Java 迁移?

转载 作者:行者123 更新时间:2023-11-30 23:33:56 24 4
gpt4 key购买 nike

我尝试解决 Flyway 的方法之一 issue 156 ( see here ) 依赖于 Java 迁移。为此,我基于 the sample 编写了一个以建立数据库的初始状态。

首先,我获取数据库(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 @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
... lots more lines ...
/*!40111 SET SQL_NOTES=@OLD_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 @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COL' at line 8

所以看起来像 mysql 语法错误。但是语法是由我试图将其提供给的同一个数据库生成的。所以我猜中间一定有什么东西挡住了路。有什么建议么?我想了解为什么这种确切的方法不起作用,但我也将感激地接受有关其他完成 my ultimate goal 的方法的建议。 .

最佳答案

查看您其他问题的答案。

Flyway SQL 解析器应该能够毫无问题地处理这种类型的输入。

关于mysql - 如何仅执行 sql 脚本才能获得 Flyway Java 迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9135532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com