Вопрос

We’re using liquibase, gradle and the com.augusttechgroup:gradle-liquibase-plugin. We’re currently facing the bug https://liquibase.jira.com/browse/CORE-1803 which kills our continuoes integration server. Until this problem is resolved, we would like to use a workaround so that we can run the „dropAll“ task in gradle twice.

gradle dropAll dropAll

doesn’t work and

gradle dropAll && gradle dropAll

is no option because of our continuos integration servers which can’t manage this.

Is there a way to make a task run twice or do I have to work around the gradle plugin and write my own dropAll task like @judoole proposed here Liquibase 3.0.1 Gradle integration?

Это было полезно?

Решение

The simplest solution I found was just to declare two databases.

liquibase {
    activities {
        main {
            url 'jdbc:postgresql://localhost:5432/db'
            username 'user'
            password 'passwd'
            ...
        }
        secondRun {
            url 'jdbc:postgresql://localhost:5432/db'
            username 'user'
            password 'passwd'
            ...        
        }
    }
    runList = 'main'
}

dropAll.doFirst { liquibase.runList = 'main,secondRun' }
dropAll.doLast { liquibase.runList = 'main' }

Per default the 'runList' only contains the main db. But for dropAll he sets both databases active, so that dropAll will run on both ... which are actually the same.

Другие советы

I would consider using JavaExec, like the linked StackOverflow question, especially if you're only using xml and not Groovy to write your changelog. The beauty of the liquibase-gradle-plugin is just these strange cases you describe. From the Github repo:

Let's suppose that for each deployment, you need to update the data model for your application's database, and wou also need to run some SQL statements in a separate database used for security.

Now I haven't used the plugin, because I didn't need those features. But I did run into bugs in liquibase and needed to use a spesific version, namely 3.0.4, as generateChangelog has a strange bug since 3.0.5. Using the JavaExec version I was able to track down a version that suited my needs.

Now, the liquibase-gradle-plugin doesn't need to be your only weapon of choice. Gradle has plenty of room for writing your own little tasks. Also those who do some sql. Maybe try something along these lines and see if that works:

configurations {    
    driver
}

dependencies {
    driver '<your-sql-driver>'
}

//"Bug" in Gradle. Groovy classes are loaded first. They need to know about sql driver
//Or I think it's still so
URLClassLoader loader = GroovyObject.class.classLoader
configurations.driver.each { File file ->
    loader.addURL(file.toURL())
}

task deleteFromTables(description: 'Deletes everything.') <<{
    def props = [user: "<username>", password: "<password>", allowMultiQueries: 'true'] as Properties
    def sql = Sql.newInstance("<url>", props, "<driver-classname>)

    try {
        //Here you can do your magic. Delete something, or simple drop the database.
        //After dropping it, you'd probably want another task for creating 
        //it back up again
        sql.execute("DELETE ...")
    } finally {
        sql.close()
    }
}

Gradle makes sure that each task gets executed at most once per Gradle invocation. If you need to do some work twice, you need to declare two tasks. Chances are that the plugin provides a suitable task type for you to declare a second dropAll task.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top