Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flyway Migrate Load Constraint Violation with Spring

I've built some back ends with Node/Express and Go, but this is my first time trying to build one with Java/Spring.

I was told Flyway is the best tool for migrations. I got the SQL migrations working to get the schema set up for all my tables, now I'm trying to seed a user table with a Java-based migration.

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 ('[email protected]', 'test123')");
    }
}

Any idea what might be happening? I've poked around on here and done a fair bit of Googling but haven't found other similar examples. The whole reason I am doing a Java migration is so that I can try to bring in bCrypt to hash the passwords of my seeded users, but it's definitely possible that I'm thinking about this incorrectly. Any insight would be much appreciated!

like image 608
Isaac Miller Avatar asked Dec 30 '25 14:12

Isaac Miller


1 Answers

I needed to add spring-jdbc to the buildscript dependencies to get this working 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 to the compile dependencies to ensure the same version would be used for the gradle tasks and at runtime.

compile("org.flywaydb:flyway-core:${flywayVersion}")

I created a working example in this repo.

like image 187
codemonkey Avatar answered Jan 01 '26 06:01

codemonkey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!