liquibase / liquibase-gradle-plugin

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

Add support for custom liquibase changelog tables #50

Closed immortaleeb closed 5 years ago

immortaleeb commented 5 years ago

In a project I'm working on we're using custom names for the liquibase changelog and changelog lock tables. These need to be specified on the command line with the arguments -Dliquibase.databaseChangeLogTableName=... and -Dliquibase.databaseChangeLogLockTableName=... but the caveat is that they need to be specified before the command.

This PR adds support for custom tables by adding 2 new properties to an activity: databaseChangeLogTableName and databaseChangeLogLockTableName.

An example usage would be:

liquibase {
    activities {
        main {
            // arguments...
            databaseChangeLogTableName 'LIQUIBASE_CHANGELOG'
            databaseChangeLogLockTableName 'LIQUIBASE_CHANGELOG_LOCK'
        }
    }
}
stevesaliman commented 5 years ago

This change doesn't do what you want it to do.

When you add databaseChangeLogTableName 'LIQUIBASE_CHANGELOG' to your build.gradle file, you aren't setting the value of Activity.databaseChangeLogTableName, you're calling Activity.methodMissing, which simply adds to the list of arguments the plugin will send to Liquibase, as it does now.

You could fix the methodMissing method to do something special for the 2 methods from your use case so that we pass -Dliquibase.databaseChangeLogTableName=LIQUIBASE_CHANGELOG to Liquibase before other options, but that won't work either because Liquibase uses a System property to change the table name, and simply passing a "-D" option as one of the arguments to the Liquibase Main.main method doesn't do that. It does cause Liquibase to complain because all options passed before the Liquibase command need to start with --.

You used to be able to put System.setProperty('liquibase.databaseChangeLogTableName', 'LIQUIBASE_CHANGELOG in your build.gradle file, or just add the -Dliquibase.databaseChangeLogTableName=LIQUIBASE_CHANGELOG option to the command line when you invoke Gradle, but that appears to have broken with the 2.0 release - the forked process that calls Liquibase doesn't appear to inherit System properties from the parent.

Fortunately, your use case should already work with the existing code - at least it does for the dropAll and update commands I tested. It isn't documented anywhere, but databaseChangeLogTableName and databaseChangeLogLockTableName are valid changeLog parameters, which means that your example usage causes --databaseChangeLogTableName=LIQUIBASE_CHANGELOG and --databaseChangeLogLockTableName=LIQUIBASE_CHANGELOG_LOCK to be passed to Liquibase, and Liquibase does the expected thing with them.

stevesaliman commented 5 years ago

I've fixed the System property problem that I mentioned earlier. When 2.0.2 is released, the JVM that we fork to run Liquibase will inherit all System properties from the parent JVM, so you'll be able to pass the change log table name via the command line, or by setting a System property in your build.gradle file, as described in issue #5. Until then you should be able to just use the methods from your use case example.

immortaleeb commented 5 years ago

You're right indeed! I didn't know about the existence of --databaseChangeLogTableName= and --databaseChangeLogLockTableName= so I assumed that my changes were the ones causing it to work but I never actually tried to run my activity with the latest released version.

I'm also pretty new at groovy so I appreciate you've taken to the time to explain the issue. Seems like I can close this PR now :)

stevesaliman commented 5 years ago

Thank you for drawing my attention to the fact that the plugin was not passing System Properties to Liquibase :-)

I should have a new release that fixes that problem by the end of the weekend.