【问题标题】:Gradle Liquibase plugin not using specified contextsGradle Liquibase 插件未使用指定的上下文
【发布时间】:2021-06-03 00:49:17
【问题描述】:

我们有一个使用 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

该项目曾一度分叉,所以也许我们误解了文档(originalnew),但看起来这应该可行。

我们需要不同的插件/版本吗?我们说错了吗?

【问题讨论】:

    标签: gradle liquibase


    【解决方案1】:

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

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

    然后通过:

    gradle update -Pcontexts=schema
    

    【讨论】:

      【解决方案2】:

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

      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 
      

      只会执行第一个变更集

      【讨论】:

        【解决方案3】:

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

        liquibase.gradle (sn-p):

        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 (sn-p):

        liquibaseTaskPrefix=liquibase
        

        所以,现在可以安全地调用了:

        ./gradlew liquibaseUpdate # to apply contexts=dev
        

        还有:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-10
          • 1970-01-01
          • 2012-08-07
          • 2020-06-15
          • 2016-06-13
          • 1970-01-01
          • 2021-12-28
          相关资源
          最近更新 更多