liquibase / liquibase-gradle-plugin

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

Cannot exclude objects from "diff" #56

Closed tkruemmel closed 4 years ago

tkruemmel commented 5 years ago

I wrote a custom gradle task, to use the diff command. I would like to exclude the tables where Liquibase stores its information from the "diff" command. Here is my code: `task diffFixedDBvsEntities(type: JavaExec) { group = "liquibase"

classpath sourceSets.main.runtimeClasspath
main = "liquibase.integration.commandline.Main"

args "--changeLogFile=" + '/home/tom/IdeaProjects/demoLiquibaseGit/src/main/resources/db.changelog/db.changelog-master.yaml'
args "--referenceUrl=" + "hibernate:spring:demoLiquibaseGit.domain.entity?dialect=org.hibernate.dialect.MySQL5Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy"
args "--username=" + "db"
args "--password=" + "db"
args "--url=" + "jdbc:mariadb://localhost:3306/db?useUnicode=yes&characterEncoding=UTF-8"
args "--referenceDriver=liquibase.ext.hibernate.database.connection.HibernateDriver"
args "--classpath=src/main"
args "diff"
args "--diffExcludeObjects=table_DATABASECHANGELOG"

}`

And here is the error I am getting: Unexpected error running Liquibase: Unknown option: 'diffExcludeObjects' liquibase.exception.CommandLineParsingException: Unknown option: 'diffExcludeObjects' at liquibase.integration.commandline.Main.parseOptionArgument(Main.java:818) at liquibase.integration.commandline.Main.parseOptions(Main.java:764) at liquibase.integration.commandline.Main.run(Main.java:177) at liquibase.integration.commandline.Main.main(Main.java:129)

I used the documentation for the maven task, because I thought this would be the way to go.

stevesaliman commented 5 years ago

Your custom task is actually completely separate from the Liquibase Gradle plugin. Your task uses JavaExec to run Liquibase as if you had run it from the command line. The best documentation to follow would be here: http://www.liquibase.org/documentation/command_line.html. If you want to keep your task the way it is, --diffExcludeObjects should become excludeObjects=DATABASECHANGELOG (if you're using the default table). This option is helpfully excluded from the official documentation...

There is another way that does use the plugin. You could have a liquibase block in your build.gradle that looks like this:

liquibase {
  activities {
    main {
      changeLogFile 'src/main/db/main.groovy'
      url project.ext.mainUrl
      username project.ext.mainUsername
      password project.ext.mainPassword
    }
    diffMain {
      changeLogFile 'src/main/resources/db.changelog/db.changelog-master.yaml'
      referenceUrl 'hibernate:spring:demoLiquibaseGit.domain.entity?dialect=org.hibernate.dialect.MySQL5Dialect&hibernate.physical_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy&hibernate.implicit_naming_strategy=org.springframework.boot.orm.jpa.hibernate.SpringImplicitNamingStrategy'
      username project.ext.mainUsername
      password  project.ext.mainPassword
      url 'jdbc:mariadb://localhost:3306/db?useUnicode=yes&characterEncoding=UTF-8'
      referenceDriver 'liquibase.ext.hibernate.database.connection.HibernateDriver'
      excludeObjects 'DATABASECHANGELOG'
    }
  }
  runList = project.ext.runList
}

You'd probably also want runList=main in gradle.properties so that by default it only runs normal Liquibase tasks. You could then run your diff with gradle diff -PrunList=diffMain

Two things to keep in mind:

  1. The method names inside an activity, like url or username are a direct match to the comnand line arguments of Liquibase.
  2. There is nothing special about activity names. I use main and diffMain but you can call them whatever you want, as long as you reference them correctly in the runList.

I hope this helps.