如何在Spring Boot中运行基于Java的Flyway迁移?

问题描述 投票:0回答:1

我一直在尝试让基于Java的Flyway Migration在Spring Boot中正常运行大约10个小时,但即使经过无数的努力和搜索,到目前为止仍然没有任何效果。

这是我想要运行的基于 Java 的迁移。请注意,我现在可以使用 SQL 运行它,但我需要基于 Java 的迁移才能工作,以便稍后可以执行更复杂的任务。

package db.migration.kotlin

import org.flywaydb.core.api.migration.BaseJavaMigration
import org.flywaydb.core.api.migration.Context


class V1__create_users_table : BaseJavaMigration() {
    override fun migrate(context: Context) {
        context.connection.use { connection ->
            connection.createStatement().execute("""
            CREATE TABLE users
            (
                id         SERIAL NOT NULL,
                email      TEXT   NOT NULL,
                created_at TIMESTAMP
            );
            """
            )
        }
    }

}

当前位于

src/main/resources/db/migration/kotlin
下。

当我运行 Spring Boot 应用程序时,以下是我在日志中看到的内容:

2024-05-12T00:16:23.743+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.c.i.s.SchemaHistoryFactory           : Default schema: null
2024-05-12T00:16:23.747+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.c.i.c.SqlScriptCallbackFactory       : Scanning for SQL callbacks ...
2024-05-12T00:16:23.747+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V2__update_created_at.kt (filename: V2__update_created_at.kt)
2024-05-12T00:16:23.747+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V1__create_users_table.kt (filename: V1__create_users_table.kt)
2024-05-12T00:16:23.752+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.command.DbValidate     : Validating migrations ...
2024-05-12T00:16:23.753+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V2__update_created_at.kt (filename: V2__update_created_at.kt)
2024-05-12T00:16:23.753+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V1__create_users_table.kt (filename: V1__create_users_table.kt)
2024-05-12T00:16:23.753+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V2__update_created_at.kt (filename: V2__update_created_at.kt)
2024-05-12T00:16:23.753+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V1__create_users_table.kt (filename: V1__create_users_table.kt)
2024-05-12T00:16:23.753+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V2__update_created_at.kt (filename: V2__update_created_at.kt)
2024-05-12T00:16:23.754+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.scanner.Scanner        : Filtering out resource: db/migration/kotlin/V1__create_users_table.kt (filename: V1__create_users_table.kt)
2024-05-12T00:16:23.761+05:45  INFO 77861 --- [flyway-java-migration] [           main] o.f.core.internal.command.DbValidate     : Successfully validated 0 migrations (execution time 00:00.008s)
2024-05-12T00:16:23.761+05:45  WARN 77861 --- [flyway-java-migration] [           main] o.f.core.internal.command.DbValidate     : No migrations found. Are your locations set up correctly?
2024-05-12T00:16:23.762+05:45 DEBUG 77861 --- [flyway-java-migration] [           main] o.f.core.internal.command.DbSchemas      : Skipping creation of existing schema: "public"

它似乎跳过了基于Java的迁移。

这是我的

application.yml
文件的样子:

spring:
  application:
    name: flyway-java-migration
  flyway:
    locations: classpath:db/migration/kotlin

server:
  port: 8080
  shutdown: graceful

logging:
  level:
    org:
      flywaydb: DEBUG
java spring spring-boot flyway
1个回答
0
投票
@BasilBourque 发布的

这篇文章帮助我解决了我的问题。我必须做以下事情来解决我的问题:

  1. 将文件从
    src/main/resources/db/migration/kotlin
    移动到
    src/main/db/migration
    ,以便 Spring 编译 Kotlin 文件。之前的 Dzone 文章解释了
    src/main/resources
    中的文件未编译,这就是为什么您需要将基于 Java 的迁移直接放在
    src/main
    中。
  2. application.yml
    中删除以下行,因为我们不再将基于 Java 的迁移放在那里:
  flyway:
    locations: classpath:db/migration/kotlin

仅通过这些更改,一切就正常了。我希望这个答案对其他人也有用,因为我很难在互联网上找到解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.