Firstly JOOQ is fantastic and has saved me more time than I care to mention. Recently I've been looking at testing with JOOQ and there is a great MockResult and associated classes for mocking results from the DB. But of course one challenge of testing DB layers is at some point you have to know if the queries actually work.

As JOOQ, when generating classes, is effectively taking a snapshot of your DB why can we not use that to recreate the DB for testing? For example spin up an in memory DB and run a createDatabase() or getTableSql() type command. So my question is does anyone know of a way this can be achieved with JOOQ?

With such a setup JOOQ could impart it's knowledge of the DB structure, albeit minus indices and other niceties, and then the queries can be tested. Sure I have db deployment scripts around but this would allow a purely java based solution. After all if the DB changes you need to regenerate JOOQ anyway so why not work from that snapshot.

As an aside to this I already have a checkDatabase() command which runs on application start that cycles through JOOQ tables and fields to verify they all exist and throws an exception if the environment in which the application is being deployed does not match the last JOOQ snapshot which was taken.

有帮助吗?

解决方案

Issue #3160 has been implemented for jOOQ 3.8

With this, you can run (for example):

for (Query query : 
    DSL.using(SQLDialect.H2, new Settings().withRenderFormatted())
       .ddl(T_BOOK)
       .queries()) {
    System.out.println(query + ";\n");
}

... which will print (for example):

create table "PUBLIC"."T_BOOK"(
  "ID" int not null,
  "AUTHOR_ID" int not null,
  "CO_AUTHOR_ID" int null,
  "DETAILS_ID" int null,
  "TITLE" varchar(400) not null,
  "PUBLISHED_IN" int not null,
  "LANGUAGE_ID" int not null,
  "CONTENT_TEXT" clob null,
  "CONTENT_PDF" blob null,
  "REC_VERSION" int null,
  "REC_TIMESTAMP" timestamp null,
  constraint "PK_T_BOOK"
    primary key ("ID"),
  constraint "FK_T_BOOK_AUTHOR_ID"
    foreign key ("AUTHOR_ID")
    references "PUBLIC"."T_AUTHOR" ("ID"),
  constraint "FK_T_BOOK_CO_AUTHOR_ID"
    foreign key ("CO_AUTHOR_ID")
    references "PUBLIC"."T_AUTHOR" ("ID"),
  constraint "FK_T_BOOK_LANGUAGE_ID"
    foreign key ("LANGUAGE_ID")
    references "PUBLIC"."T_LANGUAGE" ("ID")
);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top