Search code examples
jooqjooq-codegen-maven

Is there a jOOQ tool to verify generated definitions are still correct?


I am working with classes generated by jOOQ based on a schema maintained by Liquibase. I am looking for a way to ensure that the jOOQ classes remain consistent with the actual database. The preferred approach is to create a test that can be run by our CI tool when pull requests are created.

Is there a tool to verify that the jOOQ generated definitions are still correct?


Solution

  • Two obvious approaches involving your build setup are:

    • To check in generated sources (get a diff when they're not up to date). See also the manual section about code generation and version control.
    • To include both Liquibase migrations and jOOQ code generation in your build. That way, the generated sources and the database are always up to date with what is defined in your Liquibase migration scripts. You can also base your code generation directly on your Liquibase files using the LiquibaseDatabase, if you're not doing anything fancy, vendor specific.

    A less obvious way using programmatic jOOQ API is to compare two versions of Meta using Meta.migrateTo(Meta):

    // This corresponds to the meta data from your live connection
    Meta m1 = ctx.meta();
    
    // This corresponds to the meta data from your generated catalog (or schema, table, etc)
    Meta m2 = ctx.meta(catalog);
    
    // This is a generated migration script between the two versions, should be empty
    Queries queries = m1.migrateTo(m2);
    

    The approach might work, though it has a lot of caveats, which are still being fixed as of jOOQ 3.14, 3.15. Work in progress can be seen here: https://github.com/jOOQ/jOOQ/projects/1, bug reports very welcome!