模式更新后jOOQ生成的类的运行时验证? [英] Runtime validation of jOOQ generated classes after schema update?

查看:60
本文介绍了模式更新后jOOQ生成的类的运行时验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建过程中,我使用 org.jooq.util.DefaultGenerator 生成代表我的数据库模式的jOOQ类。

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.

如何在运行时检测生成的代码是否仍对特定模式有效?

我正在寻找类似 boolean stillValid = new SchemaValidator(existingGeneratedCodePath,jdbcUrl,jdbcProps).validate();

推荐答案

使用org.jooq.Meta的jOOQ 3.0解决方案



在即将到来的jOOQ 3.0中,可以通过新的DatabaseMetaData 。 com / jOOQ / jOOQ / blob / master / jOOQ / src / main / java / org / jooq / Meta.java rel = nofollow> org.jooq.Meta 对象(通过功能请求#1968 实现)。该对象提供对这些类型的各种对象的访问:

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

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



使用JDBC DatabaseMetaData的jOOQ 2.x解决方案



上述解决方案可以是手动实施,查询 Connection.getMetaData() 。当然,这将需要更多的工作

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

另一个简单的解决方案将查询所有生成的表,如下所示:

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

这篇关于模式更新后jOOQ生成的类的运行时验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆