我已经使用 Node/Express 和 Go 构建了一些后端,但这是我第一次尝试使用 Java/Spring 构建后端。
有人告诉我 Flyway 是最好的迁移工具。我让 SQL 迁移工作来为我的所有表设置架构,现在我正在尝试使用基于 Java 的迁移来为 user
表播种。
现在,当我调用 gradle flywayMigrate
时,出现此错误:
loader constraint violation in interface itable initialization: when
resolving method
"db.migration.V2_1__Add_Users.migrate(Lorg/springframework/jdbc/core/JdbcTemplate;)V"
the class loader (instance of java/net/URLClassLoader) of the current
class, db/migration/V2_1__Add_Users, and the class loader
(instance of org/gradle/internal/classloader/VisitableURLClassLoader)
for interface
org/flywaydb/core/api/migration/spring/SpringJdbcMigration have
different Class objects for the type
org/springframework/jdbc/core/JdbcTemplate used in the signature
这是我在 build.gradle 中的内容:
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id "org.flywaydb.flyway" version "4.1.2"
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'project-report'
flyway {
url = 'jdbc:mysql://localhost/upshift?serverTimezone=UTC'
user = 'root'
locations = ['db.migration']
}
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.flywaydb:flyway-core')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-security')
compile("org.springframework:spring-jdbc")
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.security:spring-security-test')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
这是我尝试迁移的 java 类:
package db.migration;
import org.flywaydb.core.api.migration.spring.SpringJdbcMigration;
import org.springframework.jdbc.core.JdbcTemplate;
public class V2_1__Add_Users implements SpringJdbcMigration {
public void migrate(JdbcTemplate jdbcTemplate) throws Exception {
jdbcTemplate.execute("INSERT INTO users (email, password) VALUES ('test@test.com', 'test123')");
}
}
知道会发生什么吗?我在这里四处闲逛并进行了一些谷歌搜索,但没有找到其他类似的例子。我进行 Java 迁移的全部原因是我可以尝试引入 bCrypt 来散列我的种子用户的密码,但我肯定有可能错误地考虑了这一点。任何见解将不胜感激!
最佳答案
我需要将 spring-jdbc 添加到 buildscript 依赖项中才能使其与 Flyway 4.1.2 一起使用。
Flyway 4.0.3 没有它也能正常工作。
buildscript {
ext {
springBootVersion = '1.5.2.RELEASE'
springVersion = '4.3.7.RELEASE'
flywayVersion = '4.1.2'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
classpath("org.flywaydb:flyway-gradle-plugin:${flywayVersion}")
classpath("org.springframework:spring-jdbc:${springVersion}")
}
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'project-report'
apply plugin: 'org.flywaydb.flyway'
flyway {
url = 'jdbc:mysql://localhost/upshift?serverTimezone=UTC'
user = 'root'
password = 'root'
locations = ['db.migration']
}
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile("org.flywaydb:flyway-core:${flywayVersion}")
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-security')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.security:spring-security-test')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
我还在编译依赖项中添加了一个明确的 Flyway 版本,以确保相同的版本将用于 gradle 任务和运行时。
compile("org.flywaydb:flyway-core:${flywayVersion}")
我在 this repo 中创建了一个工作示例.
关于java - Flyway Migrate 负载约束违反 Spring ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941777/