eirirlar
eirirlar

Reputation: 812

Runtime validation of jOOQ generated classes after schema update?

I use the org.jooq.util.DefaultGenerator during the build process to generate jOOQ classes to represent my database schema.

While the application runs, the schema is expected to change without the application knowing about it. Such changes may or may not be compatible with the already generated code.

How can I detect in runtime whether the generated code is still valid against a certain schema?

I'm looking for something like boolean stillValid = new SchemaValidator(existingGeneratedCodePath, jdbcUrl, jdbcProps).validate();

Upvotes: 3

Views: 1042

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221106

A jOOQ 3.0 solution using org.jooq.Meta

In the upcoming jOOQ 3.0, JDBC's DatabaseMetaData can be accessed in a "jOOQ way" through a new org.jooq.Meta object (implemented with feature request #1968). This object provides access to various objects of these types:

  • org.jooq.Catalog
  • org.jooq.Schema
  • org.jooq.Table
  • org.jooq.Field
  • org.jooq.DataType

These could be compared to your generated classes, e.g.

MY_SCHEMA.getTables().equals(create.meta().getTables())

A jOOQ 2.x solution using JDBC DatabaseMetaData

The above solution can be implemented manually, querying the Connection.getMetaData(). It'll be a bit more work, of course

A trick querying all the tables

Another simple solution would be to query all the generated tables like this:

List<Table<?>> invalidTables = new ArrayList<>();

for (Table<?> table : MY_SCHEMA.getTables()) {
    try {
        create.selectFrom(table).where(Factory.falseCondition()).fetch();
    }

    // If table names / column names change, the above query would fail
    catch (DataAccessException e) {
        invalidTables.add(table);
    }
}

This trick would allow to detect if increments are compatible

Upvotes: 3

Related Questions