liquibase / liquibase-gradle-plugin

A Gradle plugin for Liquibase
Other
197 stars 57 forks source link

Unclear how to call LiquibaseTasks with parameters #86

Closed lyckligtax closed 3 years ago

lyckligtax commented 3 years ago

Hi, I'm trying to create a task to drop my database:

tasks.register("clearTestDB") {
    group "liquibase"
    dependsOn dropAll
}

unfortunately this does not work on Oracle and on Postgres there are limitations to support the paid version.

I'm trying to executeSql a file to work around this. This works on the commandline gradlew executeSql -PliquibaseCommandValue="--sqlFile=drop.sql"

But I cannot get my head around how to apply it within my gradle file like

tasks.register("clearTestDB") {
    group "liquibase"
    dependsOn dropAll

    doLast {
        executeSql "-PliquibaseCommandValue=\"--sqlFile=drop.sql\""`   
    }
}

Every error I get looks like Could not find method executeSql() for arguments [-PliquibaseCommandValue="--sqlFile=drop.sql"]

What would be the correct way to give those arguments through my gradle file?

Thanks from Germany

stevesaliman commented 3 years ago

If I understand the problem correctly, you would like to execute a SQL file with the executeSql task whenever you run the dropAll task. There 2 parts to this problem to be solved.

The first problem is how to make one task run after another task. The second part of the problem is telling executeSql what file to run. Both of those problems can be solved by adding the following to your build.gradle file:

  dropAll.finalizedBy executeSql
  executeSql { 
    doFirst { 
      if ( !project.hasProperty('liquibaseCommandValue') ) { 
        project.ext.liquibaseCommandValue = "--sqlFile=test.sql"
      } 
    } 
  } 

You can then simply run gradlew dropAll to drop your objects and call the SQL to drop the database.

The first line of that block causes the executeSql task to run after dropAll. The rest of the block sets the liquibaseCommandValue property to the file that should be used by executeSql command, but only if it doesn't already have one. This allows you to use executeSql with other files if you aren't doing a dropAll.

This all assumes that you always want to drop the database when you run the dropAll task. If you don't want to drop the database every time, you can wrap the entire block inside an if ( project.hasProperty('clearTestDB') ) statement. That will cause the executeSql part to run only when you use gradlew dropAll -PclearTestDB

I hope this helps,

Steve

lyckligtax commented 3 years ago

That's what I needed :-) Thanks

if ( !project.hasProperty('liquibaseCommandValue') ) { 
    project.ext.liquibaseCommandValue = "--sqlFile=test.sql"
}