Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flyway says No database found to handle jdbc:postgresql://localhost:5432/postgres when using java migrator in jar only #3889

Open
plusmobileapps opened this issue May 6, 2024 · 4 comments

Comments

@plusmobileapps
Copy link

plusmobileapps commented May 6, 2024

Which version and edition of Flyway are you using?

10.12.0

Which client are you using? (Command-line, Java API, Maven plugin, Gradle plugin)

Java API. Gradle wrapper version 8.4

Which database are you using? (Type & version)

Postgres 12.18
driver 42.7.2

Which operating system are you using?

Mac OS sonoma 14.4.1

What did you do? (Please include the content causing the issue, any relevant configuration settings, the SQL statement(s) that failed (if any), and the command you ran)
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.postgresql:postgresql:42.7.2")
        classpath("org.flywaydb:flyway-database-postgresql:10.12.0")
    }
}


plugins {
    id("org.flywaydb.flyway") version "10.12.0"
}

dependencies {
    implementation("org.flywaydb:flyway-core:10.12.0")
    implementation("org.flywaydb:flyway-database-postgresql:10.12.0")
}

tasks {
    task("stage") {
        dependsOn("jar")
        mustRunAfter("clean")
    }

    jar {
        duplicatesStrategy = DuplicatesStrategy.INCLUDE

        manifest {
            attributes["Main-Class"] = "com.plusmobileapps.wolfpack.backend.ApplicationKt"
        }

        exclude("META-INF/*.RSA", "META-INF/*.SF", "META-INF/*.DSA")

        from(
            configurations.runtimeClasspath.get().map {
                if (it.isDirectory) it else zipTree(it)
            }
        )
    }
}
class DBMigrator(
    private val user: String = System.getEnv("JDBC_DATABASE_USER"),
    private val password: String = System.getEnv("JDBC_DATABASE_PASSWORD"),
    private val url: String = System.getEnv("JDBC_DATABASE_URL")
) : DBMigrator {
    override fun migrate() {
        val flyway = Flyway.configure()
            .dataSource(
                /* url = */ url,
                /* user = */ user,
                /* password = */ password
            )
            .load()
        flyway.migrate()
    }
}
// Application.kt

fun main() {
    embeddedServer(Netty, port = EnvironmentVariablesImpl.port, host = "0.0.0.0") {
        DBMigrator().migrate()
        // app configuration code for routing
    }.start(wait = true)
}
What did you expect to see?

After running the stage task which builds the jar file, the migrator should run successfully upon app startup:

./gradlew :backend:stage
java -jar ./backend/build/libs/backend-0.0.1.jar

...
May 05, 2024 6:15:30 PM org.flywaydb.core.FlywayExecutor info
INFO: Database: jdbc:postgresql://localhost:5432/postgres (PostgreSQL 12.18)
May 05, 2024 6:15:30 PM org.flywaydb.core.internal.command.DbValidate info
INFO: Successfully validated 3 migrations (execution time 00:00.035s)
May 05, 2024 6:15:30 PM org.flywaydb.core.internal.command.DbMigrate info
INFO: Current version of schema "public": 3
May 05, 2024 6:15:30 PM org.flywaydb.core.internal.command.DbMigrate info
INFO: Schema "public" is up to date. No migration necessary.

This works fine when running from the command line with the gradle wrapper, it only seems to have an error when running from the jar file.

./gradlew :backend:run
What did you see instead?
 java -jar ./backend/build/libs/backend-0.0.1.jar
18:19:21.768 [main] INFO ktor.application - Autoreload is disabled because the development mode is off.
Exception in thread "main" java.lang.ExceptionInInitializerError
	at com.plusmobileapps.wolfpack.backend.ApplicationKt$main$1.invoke(Application.kt:17)
	at com.plusmobileapps.wolfpack.backend.ApplicationKt$main$1.invoke(Application.kt:13)
	at io.ktor.server.engine.internal.CallableUtilsKt.executeModuleFunction(CallableUtils.kt:51)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$launchModuleByName$1.invoke(ApplicationEngineEnvironmentReloading.kt:332)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$launchModuleByName$1.invoke(ApplicationEngineEnvironmentReloading.kt:331)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.avoidingDoubleStartupFor(ApplicationEngineEnvironmentReloading.kt:356)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.launchModuleByName(ApplicationEngineEnvironmentReloading.kt:331)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.access$launchModuleByName(ApplicationEngineEnvironmentReloading.kt:32)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$instantiateAndConfigureApplication$1.invoke(ApplicationEngineEnvironmentReloading.kt:319)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$instantiateAndConfigureApplication$1.invoke(ApplicationEngineEnvironmentReloading.kt:310)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.avoidingDoubleStartup(ApplicationEngineEnvironmentReloading.kt:338)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.instantiateAndConfigureApplication(ApplicationEngineEnvironmentReloading.kt:310)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.createApplication(ApplicationEngineEnvironmentReloading.kt:150)
	at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.start(ApplicationEngineEnvironmentReloading.kt:277)
	at io.ktor.server.netty.NettyApplicationEngine.start(NettyApplicationEngine.kt:216)
	at com.plusmobileapps.wolfpack.backend.ApplicationKt.main(Application.kt:18)
	at com.plusmobileapps.wolfpack.backend.ApplicationKt.main(Application.kt)
Caused by: org.flywaydb.core.api.FlywayException: No database found to handle jdbc:postgresql://localhost:5432/postgres
	at org.flywaydb.core.internal.jdbc.DriverDataSource.<init>(DriverDataSource.java:158)
	at org.flywaydb.core.internal.jdbc.DriverDataSource.<init>(DriverDataSource.java:96)
	at org.flywaydb.core.api.configuration.ClassicConfiguration.setDataSource(ClassicConfiguration.java:1093)
	at org.flywaydb.core.api.configuration.FluentConfiguration.dataSource(FluentConfiguration.java:626)
	at com.plusmobileapps.wolfpack.backend.data.db.DBMigratorImpl.migrate(DBMigrator.kt:17)
	at com.plusmobileapps.wolfpack.backend.data.db.WPDatabaseImpl.<init>(WPDatabase.kt:23)
	at com.plusmobileapps.wolfpack.backend.data.db.WPDatabaseImpl.<init>(WPDatabase.kt:16)
	at com.plusmobileapps.wolfpack.backend.di.ServiceLocator.<clinit>(DI.kt:38)
Related

I have already come across a couple of issues with some suggestions which I had included in this solution, but still can't seem to get this to work with the jar.

@Barry-RG
Copy link
Contributor

Thank you for your issue. I cannot initially see what would be causing the issue. However, because you are using Kotlin and I am unsure how Flyways service loader reacts in a Kotlin environment, I will need to do some investigation.

@Barry-RG
Copy link
Contributor

In the meanwhile, would you be able to enable any sort of debug logging in your application to allow Flyway to log more debug information to potentially help locate the issue.

@plusmobileapps
Copy link
Author

plusmobileapps commented May 11, 2024

Turned on the debugger for flyway and here is the output for running with gradle vs jar. Although I'm not sure how helpful turning on the debug logs are since flyway is crashing the app before its able to log anything. Is there something else I could do to get more detailed logs? I had updated my logback configuration with the following to get the debug info from flyway:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="trace">
        <appender-ref ref="STDOUT"/>
    </root>
    <logger name="io.netty" level="INFO"/>
    <logger name="org.flywaydb" level="DEBUG"/>
</configuration>
Successful migration with gradle
> Task :backend:run
2024-05-11 14:31:08.111 [main] INFO  ktor.application - Autoreload is disabled because the development mode is off.
2024-05-11 14:31:08.598 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Multiple databases found that handle url 'jdbc:postgresql://localhost:5432/postgres': CockroachDB,PostgreSQL
2024-05-11 14:31:08.598 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Scanning for classpath resources at 'classpath:db/callback' ...
2024-05-11 14:31:08.598 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Determining location urls for classpath:db/callback using ClassLoader jdk.internal.loader.ClassLoaders$AppClassLoader@7aec35a ...
2024-05-11 14:31:08.598 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Unable to resolve location classpath:db/callback.
2024-05-11 14:31:08.598 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Scanning for classpath resources at 'classpath:db/migration' ...
2024-05-11 14:31:08.598 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Determining location urls for classpath:db/migration using ClassLoader jdk.internal.loader.ClassLoaders$AppClassLoader@7aec35a ...
2024-05-11 14:31:08.598 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Scanning URL: file:/Users/andrewsteinmetz/development/wolfpack/backend/build/resources/main/db/migration
2024-05-11 14:31:08.599 [main] DEBUG o.f.c.i.s.c.FileSystemClassPathLocationScanner - Scanning starting at classpath root in filesystem: /Users/andrewsteinmetz/development/wolfpack/backend/build/resources/main/
2024-05-11 14:31:08.599 [main] DEBUG o.f.c.i.s.c.FileSystemClassPathLocationScanner - Scanning for resources in path: /Users/andrewsteinmetz/development/wolfpack/backend/build/resources/main/db/migration (db/migration)
2024-05-11 14:31:08.600 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Found resource: db/migration/V2__Create_dog_breed.sql
2024-05-11 14:31:08.600 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Found resource: db/migration/V3__Add_Weight_Table.sql
2024-05-11 14:31:08.600 [main] DEBUG o.f.c.i.s.classpath.ClassPathScanner - Scanning for classes at classpath:db/migration
2024-05-11 14:31:08.603 [main] DEBUG o.f.c.i.r.ResourceNameValidator - Validating V2__Create_dog_breed.sql
2024-05-11 14:31:08.611 [main] DEBUG o.f.c.i.d.DatabaseTypeRegister - Multiple databases found that handle url 'jdbc:postgresql://localhost:5432/postgres': CockroachDB,PostgreSQL
2024-05-11 14:31:08.612 [main] DEBUG o.f.c.i.r.ResourceNameValidator - Validating V3__Add_Weight_Table.sql
2024-05-11 14:31:08.612 [main] DEBUG o.f.c.i.d.DatabaseTypeRegister - Multiple databases found that handle url 'jdbc:postgresql://localhost:5432/postgres': CockroachDB,PostgreSQL
2024-05-11 14:31:08.848 [main] DEBUG o.f.c.i.d.DatabaseTypeRegister - Multiple databases found that handle url 'jdbc:postgresql://localhost:5432/postgres': CockroachDB,PostgreSQL
2024-05-11 14:31:08.849 [main] INFO  org.flywaydb.core.FlywayExecutor - Database: jdbc:postgresql://localhost:5432/postgres (PostgreSQL 12.18)
2024-05-11 14:31:08.849 [main] DEBUG org.flywaydb.core.FlywayExecutor - Database Type: PostgreSQL
2024-05-11 14:31:08.849 [main] DEBUG org.flywaydb.core.FlywayExecutor - Driver: PostgreSQL JDBC Driver 42.7.2
2024-05-11 14:31:08.850 [main] DEBUG org.flywaydb.core.FlywayExecutor - DDL Transactions Supported: true
2024-05-11 14:31:08.851 [main] DEBUG o.f.c.i.s.SchemaHistoryFactory - Schemas: 
2024-05-11 14:31:08.851 [main] DEBUG o.f.c.i.s.SchemaHistoryFactory - Default schema: null
2024-05-11 14:31:08.860 [main] DEBUG o.f.c.i.c.SqlScriptCallbackFactory - Scanning for SQL callbacks ...
2024-05-11 14:31:08.875 [main] DEBUG o.f.core.internal.command.DbValidate - Validating migrations ...
2024-05-11 14:31:08.878 [main] DEBUG o.f.c.a.c.ClassicConfiguration - CherryPickConfigurationExtension not found
2024-05-11 14:31:08.885 [main] DEBUG o.f.core.internal.scanner.Scanner - Filtering out resource: db/migration/V2__Create_dog_breed.sql (filename: V2__Create_dog_breed.sql)
2024-05-11 14:31:08.885 [main] DEBUG o.f.core.internal.scanner.Scanner - Filtering out resource: db/migration/V3__Add_Weight_Table.sql (filename: V3__Add_Weight_Table.sql)
2024-05-11 14:31:08.886 [main] DEBUG o.f.core.internal.scanner.Scanner - Filtering out resource: db/migration/V2__Create_dog_breed.sql (filename: V2__Create_dog_breed.sql)
2024-05-11 14:31:08.886 [main] DEBUG o.f.core.internal.scanner.Scanner - Filtering out resource: db/migration/V3__Add_Weight_Table.sql (filename: V3__Add_Weight_Table.sql)
2024-05-11 14:31:08.920 [main] INFO  o.f.core.internal.command.DbValidate - Successfully validated 3 migrations (execution time 00:00.044s)
2024-05-11 14:31:08.923 [main] DEBUG o.f.core.internal.command.DbSchemas - Skipping creation of existing schema: "public"
2024-05-11 14:31:08.951 [main] DEBUG o.f.c.a.c.ClassicConfiguration - CherryPickConfigurationExtension not found
2024-05-11 14:31:08.953 [main] INFO  o.f.core.internal.command.DbMigrate - Current version of schema "public": 3
2024-05-11 14:31:08.956 [main] INFO  o.f.core.internal.command.DbMigrate - Schema "public" is up to date. No migration necessary.
2024-05-11 14:31:08.961 [main] DEBUG org.flywaydb.core.FlywayExecutor - Memory usage: 15 of 34M

Failing migration with java jar
 java -jar backend/build/libs/backend-0.0.1.jar
2024-05-11 14:37:32.194 [main] INFO  ktor.application - Autoreload is disabled because the development mode is off.
2024-05-11 14:37:32.362 [main] ERROR DBMigrator - Failed to migrate database
org.flywaydb.core.api.FlywayException: No database found to handle jdbc:postgresql://localhost:5432/postgres
        at org.flywaydb.core.internal.jdbc.DriverDataSource.<init>(DriverDataSource.java:158)
        at org.flywaydb.core.internal.jdbc.DriverDataSource.<init>(DriverDataSource.java:96)
        at org.flywaydb.core.api.configuration.ClassicConfiguration.setDataSource(ClassicConfiguration.java:1093)
        at org.flywaydb.core.api.configuration.FluentConfiguration.dataSource(FluentConfiguration.java:626)
        at com.plusmobileapps.wolfpack.backend.data.db.DBMigratorImpl.migrate(DBMigrator.kt:20)
        at com.plusmobileapps.wolfpack.backend.data.db.WPDatabaseImpl.<init>(WPDatabase.kt:23)
        at com.plusmobileapps.wolfpack.backend.data.db.WPDatabaseImpl.<init>(WPDatabase.kt:16)
        at com.plusmobileapps.wolfpack.backend.di.ServiceLocator.<clinit>(DI.kt:38)
        at com.plusmobileapps.wolfpack.backend.ApplicationKt$main$1.invoke(Application.kt:17)
        at com.plusmobileapps.wolfpack.backend.ApplicationKt$main$1.invoke(Application.kt:13)
        at io.ktor.server.engine.internal.CallableUtilsKt.executeModuleFunction(CallableUtils.kt:51)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$launchModuleByName$1.invoke(ApplicationEngineEnvironmentReloading.kt:332)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$launchModuleByName$1.invoke(ApplicationEngineEnvironmentReloading.kt:331)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.avoidingDoubleStartupFor(ApplicationEngineEnvironmentReloading.kt:356)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.launchModuleByName(ApplicationEngineEnvironmentReloading.kt:331)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.access$launchModuleByName(ApplicationEngineEnvironmentReloading.kt:32)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$instantiateAndConfigureApplication$1.invoke(ApplicationEngineEnvironmentReloading.kt:319)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading$instantiateAndConfigureApplication$1.invoke(ApplicationEngineEnvironmentReloading.kt:310)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.avoidingDoubleStartup(ApplicationEngineEnvironmentReloading.kt:338)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.instantiateAndConfigureApplication(ApplicationEngineEnvironmentReloading.kt:310)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.createApplication(ApplicationEngineEnvironmentReloading.kt:150)
        at io.ktor.server.engine.ApplicationEngineEnvironmentReloading.start(ApplicationEngineEnvironmentReloading.kt:277)
        at io.ktor.server.netty.NettyApplicationEngine.start(NettyApplicationEngine.kt:216)
        at com.plusmobileapps.wolfpack.backend.ApplicationKt.main(Application.kt:18)
        at com.plusmobileapps.wolfpack.backend.ApplicationKt.main(Application.kt)

@lilyrrose
Copy link

This works for me as a solution.

val pg = PGSimpleDataSource()
pg.setUrl("jdbc:postgresql://localhost:5432/db?user=user&password=password")
val flyway = Flyway.configure().dataSource(pg).load()
flyway.migrate()

Make sure to add org.flywaydb:flyway-database-postgresql:10.13.0 and this uses the official postgres jdbc driver org.postgresql:postgresql:42.7.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants