Flyway Gradle 插件使用 postgres 连接字符串手动运行

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

我正在尝试使用 https://documentation.red-gate.com/fd/gradle-task-184127407.html 手动运行 Flyway 迁移。

在 micronaut application.properties 文件中我已经有了 postgres 连接

datasources.default.url=jdbc:postgresql://localhost:5432/school_staff
datasources.default.username=keycloak
datasources.default.password=password
datasources.default.dialect=POSTGRES
datasources.default.driver-class-name=org.postgresql.Driver
datasources.default.schema-generate=NONE

build.gradle.kts中,我有以下配置

buildscript {
    dependencies {
        classpath("org.flywaydb:flyway-database-postgresql:11.1.0")
    }
}

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

flyway {
    url = "jdbc:postgresql://localhost:5432/school_staff"
    user = "keycloak"
    password = "password"
    locations = arrayOf("classpath:db/migration")
    baselineOnMigrate = true
}

// Ensure classes are built before migration
tasks.named("flywayMigrate").configure {
    dependsOn(tasks.named("classes"))
}

我不想在这里再次配置postgres配置。如何在gradle Flyway配置中选择application.properties配置。

postgresql gradle flyway micronaut micronaut-data
1个回答
0
投票

我必须使用下面的代码来实现这一点。

导入java.util.Properties

// Function to load properties based on the environment
fun loadProperties(env: String): Properties {
    val properties = Properties()
    val propsFile = file("src/main/resources/application-$env.properties")

    if (propsFile.exists()) {
        propsFile.inputStream().use { properties.load(it) }
    } else {
        throw GradleException("Properties file for environment '$env' not found: ${propsFile.absolutePath}")
    }

    return properties
}

// Set the environment (default is 'dev' if no argument is passed)
val env = project.findProperty("env")?.toString() ?: "dev"

// Load properties for the chosen environment
val dbProps = loadProperties(env)



flyway {
    url = dbProps["datasources.default.url"] as String
    user = dbProps["datasources.default.username"] as String
    password = dbProps["datasources.default.password"] as String
    locations = arrayOf("classpath:db/migration")
    baselineOnMigrate = true 
}
© www.soinside.com 2019 - 2024. All rights reserved.