liquibasejooqliquibase-maven-plugin

jOOQ: How do I set up Catalog and Schema Version Providers for Liquibase?


We have a build, where we generate a PostgreSQL database with a single schema using Liquibase. From that database, we generate Java source code using jOOQ. The build uses Maven and the jooq-codegen-maven plugin for that.

In the build output, this message appears:

[INFO] No modified files        : This code generation run has not produced any file modifications.
This means, the schema has not changed, and no other parameters (jOOQ version, driver version, database version,
and any configuration elements) have changed either.

In automated builds, it is recommended to prevent unnecessary code generation runs. This run took: 555.526ms
Possible means to prevent this:
- Use manual code generation and check in generated sources: https://www.jooq.org/doc/latest/manual/code-generation/codegen-version-control/
- Use schema version providers: https://www.jooq.org/doc/latest/manual/code-generation/codegen-advanced/codegen-config-database/codegen-database-version-providers/
- Use gradle tasks and inputs: https://docs.gradle.org/current/userguide/incremental_build.html

Having read the instructions linked in the second point, I am now trying to work out how to configure a version provider.

How would I do this?

Do I need a catalogVersionProvider? I assume the word "catalog" refers to my database here, which I don't version directly.

And what would be a good SELECT to use as a schemaVersionProvider? Liquibase doesn't really seem to have a concept of an incremental version - would the most recently applied changeset ID make sense:

select id from databasechangelog order by dateexecuted desc limit 1;

? Or would something like deployment_id or even dateexecuted be better suited?


Solution

  • First off, if you're going to save 0.5s per build, the extra effort of setting this up may not really be worth it. But things may take longer in the future as your application grows...

    Do I need a catalogVersionProvider? I assume the word "catalog" refers to my database here, which I don't version directly.

    A catalog is usually a database, and a schema is a schema inside of a database. Only few RDBMS support both catalogs and schemas (e.g. SQL Server), so typically, you'll be versioning your schema only, especially in PostgreSQL.

    Or would something like deployment_id or even dateexecuted be better suited?

    From the jOOQ perspective, the "version" scheme doesn't really matter. jOOQ only checks if it has changed between consecutive re-executions. Once you version your generated schema, jOOQ will generate a @Generated annotation on your catalog or schema, and compare contents of your version provider with the existing annotation in generated code. If the version is different, then the code is re-generated. If it is the same, then the re-generation is aborted. This saves quite a few queries against the INFORMATION_SCHEMA (or the vendor-specific alternatives).

    But according to the Liquibase docs, the latest changeset ID is probably the best descriptor for a version to be used here, so your suggested SQL query seems reasonable.

    It's also possible to use Liquibase's API to set a Maven property containing that ID from the migration scripts, so you don't even have to connect to an actual database to find the latest ID.