Runtime validation of jOOQ generated classes after schema update?

五迷三道 提交于 2019-12-12 13:22:57

问题


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();


回答1:


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



来源:https://stackoverflow.com/questions/14100907/runtime-validation-of-jooq-generated-classes-after-schema-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!