diff --git a/jOOQ-meta-extensions/pom.xml b/jOOQ-meta-extensions/pom.xml
index 55ec3e3ab1..66122d05b6 100644
--- a/jOOQ-meta-extensions/pom.xml
+++ b/jOOQ-meta-extensions/pom.xml
@@ -52,7 +52,6 @@
@@ -76,9 +81,11 @@ import org.jooq.util.h2.H2Database; */ public class DDLDatabase extends H2Database { - private static final JooqLogger log = JooqLogger.getLogger(DDLDatabase.class); + private static final JooqLogger log = JooqLogger.getLogger(DDLDatabase.class); + private static final Pattern P_NAME = Pattern.compile("(?s:.*?\"([^\"]*)\".*)"); private Connection connection; + private DSLContext ctx; @Override protected DSLContext create0() { @@ -96,6 +103,7 @@ public class DDLDatabase extends H2Database { info.put("user", "sa"); info.put("password", ""); connection = new org.h2.Driver().connect("jdbc:h2:mem:jooq-meta-extensions-" + UUID.randomUUID(), info); + ctx = DSL.using(connection); InputStream in = null; try { @@ -119,14 +127,38 @@ public class DDLDatabase extends H2Database { if (in != null) { Scanner s = new Scanner(in, encoding).useDelimiter("\\A"); - Queries queries = DSL - .using(connection) - .parser() - .parse(s.hasNext() ? s.next() : ""); + Queries queries = ctx.parser().parse(s.hasNext() ? s.next() : ""); for (Query query : queries) { - log.info(query); - query.execute(); + + repeat: + for (;;) { + try { + query.execute(); + log.info(query); + break repeat; + } + catch (DataAccessException e) { + + // [#7039] Auto create missing schemas. We're using the + if (Integer.toString(ErrorCode.SCHEMA_NOT_FOUND_1).equals(e.sqlState())) { + SQLException cause = e.getCause(SQLException.class); + + if (cause != null) { + Matcher m = P_NAME.matcher(cause.getMessage()); + + if (m.find()) { + Query createSchema = ctx.createSchemaIfNotExists(name(m.group(1))); + createSchema.execute(); + log.info(createSchema); + continue repeat; + } + } + } + + throw e; + } + } } } else { @@ -150,12 +182,14 @@ public class DDLDatabase extends H2Database { } } - return DSL.using(connection); + return ctx; } @Override public void close() { JDBCUtils.safeClose(connection); + connection = null; + ctx = null; super.close(); }