【问题标题】:Flyway Migrate Load Constraint Violation with SpringFlyway Migrate 使用 Spring 违反负载约束
【发布时间】:2017-08-13 23:37:51
【问题描述】:

我已经使用 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 来散列我的种子用户的密码,但我绝对有可能错误地考虑了这一点。任何见解将不胜感激!

【问题讨论】:

    标签: java mysql spring gradle flyway


    【解决方案1】:

    我需要将 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 中创建了一个工作示例。

    【讨论】:

    • 是的,没问题。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-15
    • 2019-07-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多