Fly-through migration Load constraint violation with Spring
I've created some ends with Node / Express and Go, but this is my first time trying to build it with Java / Spring.
I was told that Flyway is the best migration tool. I got a SQL migration to set up the schema for all my tables, now I am trying to seed a table user
using Java based migrations.
Now when I call gradle flywayMigrate
I get this error:
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
This is what I have in 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')
}
And this is the java class I am trying to migrate with:
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')");
}
}
Any idea what might be happening? I've stuck in here and have done quite a bit of Googling but haven't found any other similar examples. The whole reason I am doing Java porting is that I might try to inject bCrypt to hash my users' passwords with seeding, but it is certainly possible that I am thinking about it wrong. Any insight would be much appreciated!
source to share
I needed to add spring-jdbc to my buildscript dependencies for this to work with Flyway 4.1.2.
Flyway 4.0.3 worked without it.
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')
}
I also added an explicit Flyway version for compilation dependencies to ensure that the same version is used for both gradle tasks and runtime.
compile("org.flywaydb:flyway-core:${flywayVersion}")
I created a working example in this repo .
source to share