Gradle Liquibase 插件未使用指定上下文

Gradle Liquibase plugin not using specified contexts

我们有一个使用 Liquibase 的 Gradle 项目,我们的构建文件有:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.liquibase:liquibase-gradle-plugin:1.1.1'
        classpath 'org.liquibase:liquibase-core'
    }
}

apply plugin: 'liquibase'

然而,当我们尝试通过以下方式更新我们的项目时:

gradle update -Dliquibase.contexts=foobar

(也尝试过 --contexts) 它似乎忽略了指定的上下文并运行所有的变更集。

示例:

changeSet(author: 'me', id: 'someId1', context: 'somethingElse') { // This runs, but shouldn't
// ... 
changeSet(author: 'me', id: 'someId2', context: 'foobar') { // Should only run this

该项目曾一度被分叉,所以我们可能误解了文档 (original, new),但这似乎应该可行。

我们需要一个不同的 plugin/version 吗?我们叫错了吗?

Derp,看来您需要在构建文件中手动获取上下文:

main {
    if (project.hasProperty('contexts')) {
        contexts contexts
    }
    url 'someurl'
    username 'username'
    password 'pass'
}

然后通过以下方式传入:

gradle update -Pcontexts=schema

你应该在任务中定义你的上下文

task('liquibase_dev') << {

liquibase {
    activities {
        main {
            changeLogFile changeLog
            url 'jdbc:postgresql://localhost:5432/test'
            username 'postgres'
            password 'admin'
            contexts 'DEV'
        }
    }
}

}

Liquibase 变更日志:

<changeSet author="xxx" context="DEV" id="1591257804177-1" ...>
<changeSet author="xxx" context="PROD" id="1591257804177-2" ...>

然后你可以运行喜欢:

./gradlew :liquibase_dev update 

只会执行第一个变更集

较新的插件 (2.0.3),同样的问题和略有不同的解决方案,因为我保留了默认上下文。

liquibase.gradle(片段):

if (!project.hasProperty("contexts")) {
    project.ext.contexts = "dev"
}

liquibase {
    activities {
        main {
            driver "${datasource_driver}"
            url "${datasource_url}"
            username "${datasource_username}"
            password "${datasource_password}"
            changeLogFile "src/main/resources/db/changelog/master.xml"
            defaultSchemaName ""
            logLevel "info"
            contexts contexts
        }
    }
}

tasks.liquibaseUpdate.dependsOn(tasks.classes)

gradle.properties(片段):

liquibaseTaskPrefix=liquibase

所以,现在可以安全地拨打电话了:

./gradlew liquibaseUpdate # to apply contexts=dev

并且:

./gradlew liquibaseUpdate -Pcontexts=test # to apply contexts=test