微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Flyway 7.5.1 及以上版本无法初始化 Zonky-test DB

如何解决Flyway 7.5.1 及以上版本无法初始化 Zonky-test DB

使用 Flyway 版本 7.5.0,它仍然可以正常工作。但是,版本 7.5.17.5.2 会产生以下错误

[...]
java.lang.IllegalStateException: Unexpected error occurred while initializing the data source
    at com.google.common.base.Preconditions.checkState(Preconditions.java:459)
    at io.zonky.test.db.flyway.DefaultFlywayDataSourceContext.getTarget(DefaultFlywayDataSourceContext.java:89)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:195)
    at com.sun.proxy.$Proxy65.getConnection(UnkNown Source)
    at org.flywaydb.core.internal.jdbc.JdbcUtils.openConnection(JdbcUtils.java:59)
    at org.flywaydb.core.internal.jdbc.JdbcConnectionFactory.<init>(JdbcConnectionFactory.java:69)
    at org.flywaydb.core.Flyway.execute(Flyway.java:507)
    at org.flywaydb.core.Flyway.migrate(Flyway.java:165)
    at org.springframework.boot.autoconfigure.flyway.FlywayMigrationInitializer.afterPropertiesSet(FlywayMigrationInitializer.java:66)
[...]

有什么想法,有什么问题或如何解决

以下是我重现问题的最小示例的代码

Dockerfile

FROM openjdk:11.0.10-jdk AS base

# Needed for embedded Postgresql-server to start during integration tests.
# see: https://github.com/zonkyio/embedded-database-spring-test
RUN groupadd --system --gid 1000 test
RUN useradd --system --gid test --uid 1000 --shell /bin/bash --create-home test
USER test
workdir /home/test

ADD . /home/test/
RUN ./gradlew test

build.gradle

buildscript {
    ext {
        kotlinVersion = '1.4.21'
        springBootVersion = '2.4.2'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-allopen:${kotlinVersion}")
        classpath("org.jetbrains.kotlin:kotlin-noarg:${kotlinVersion}")
    }
}

apply plugin: 'kotlin'
apply plugin: 'kotlin-spring'
apply plugin: 'kotlin-jpa'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.acme'
version = '1.0.0-SNAPSHOT'
sourceCompatibility = 1.8

compileKotlin {
    kotlinoptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}
compileTestKotlin {
    kotlinoptions {
        freeCompilerArgs = ["-Xjsr305=strict"]
        jvmTarget = "1.8"
    }
}

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.springframework.boot',name: 'spring-boot-starter-data-jpa',version: "${springBootVersion}"
    implementation group: 'org.postgresql',name: 'postgresql',version: '42.2.18'
    implementation group: 'org.flywaydb',name: 'flyway-core',version: '7.5.2'

    implementation group: 'org.jetbrains.kotlin',name: 'kotlin-reflect',version: "${kotlinVersion}"

    testImplementation group: 'org.springframework.boot',name: 'spring-boot-starter-test',version: "${springBootVersion}"
    testImplementation group: 'junit',name: 'junit',version: '4.13.1'

    testImplementation group: 'io.zonky.test',name: 'embedded-database-spring-test',version: '1.6.2'
}

test {
    testLogging {
        exceptionFormat = 'full'
    }
}

compileKotlin.dependsOn(processResources)
compileJava.dependsOn(processResources)

settings.gradle

rootProject.name = 'flywayzonkytest'

src/main/kotlin/com/acme/flywayzonkytest/Application.kt

package com.acme.flywayzonkytest

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication


@SpringBootApplication
class Application


fun main(args: Array<String>) {
    runApplication<Application>(*args)
}

src/main/resources/application.yml

spring:
  flyway:
    enabled: true

src/main/resources/db/migration/V1__thing.sql

CREATE TABLE thing (
    some_id BIGINT PRIMARY KEY NOT NULL,some_name VARCHAR NOT NULL
);

src/test/kotlin/com/acme/flywayzonkytest/integration/CreateContextTest.kt

package com.acme.flywayzonkytest.integration

import io.zonky.test.db.AutoConfigureEmbeddedDatabase
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.context.SpringBoottest
import org.springframework.test.context.junit4.springrunner


@RunWith(springrunner::class)
@AutoConfigureEmbeddedDatabase
@SpringBoottest
class CreateContextTest {

    @Test
    fun `test create context`() {
    }

}

解决方法

Flyway 在版本 7.5.07.5.1 之间进行了一些重大更改。 zonkyio/embedded-database-spring-test 的维护者在他的库中只是 implemented a fix/workaround,它包含在版本 1.6.3 中。 :-)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。