From e5d8804591f9a0eee96020c2fa2e0bfc6b29e0e2 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Thu, 7 Jan 2016 10:06:35 +1300 Subject: [PATCH] #514 - ENH: Add ebean.ddl.createOnly=true ... such that all the drop table statements are skipped (for running tests against H2 usually) --- .../com/avaje/ebean/config/ServerConfig.java | 33 +++++++++++++++++-- .../avaje/ebean/dbmigration/DdlGenerator.java | 10 ++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/avaje/ebean/config/ServerConfig.java b/src/main/java/com/avaje/ebean/config/ServerConfig.java index ff255ffbe..24e4ff093 100644 --- a/src/main/java/com/avaje/ebean/config/ServerConfig.java +++ b/src/main/java/com/avaje/ebean/config/ServerConfig.java @@ -212,6 +212,8 @@ public class ServerConfig { private boolean ddlRun; + private boolean ddlCreateOnly; + private boolean useJtaTransactionManager; /** @@ -1529,19 +1531,45 @@ public class ServerConfig { } /** - * Set to true to run the DDL generation on startup. + * Set to true to generate the "create all" DDL on startup. + * + * Typically we want this on when we are running tests locally (and often using H2) + * and we want to create the full DB schema from scratch to run tests. */ public void setDdlGenerate(boolean ddlGenerate) { this.ddlGenerate = ddlGenerate; } /** - * Set to true to run the generated DDL on startup. + * Set to true to run the generated "create all DDL" on startup. + * + * Typically we want this on when we are running tests locally (and often using H2) + * and we want to create the full DB schema from scratch to run tests. */ public void setDdlRun(boolean ddlRun) { this.ddlRun = ddlRun; } + /** + * Return true if the "drop all ddl" should be skipped. + * + * Typically we want to do this when using H2 (in memory) as our test database and the drop statements + * are not required so skipping the drop table statements etc makes it faster with less noise in the logs. + */ + public boolean isDdlCreateOnly() { + return ddlCreateOnly; + } + + /** + * Set to true if the "drop all ddl" should be skipped. + * + * Typically we want to do this when using H2 (in memory) as our test database and the drop statements + * are not required so skipping the drop table statements etc makes it faster with less noise in the logs. + */ + public void setDdlCreateOnly(boolean ddlCreateOnly) { + this.ddlCreateOnly = ddlCreateOnly; + } + /** * Return true if the DDL should be generated. */ @@ -2232,6 +2260,7 @@ public class ServerConfig { ddlGenerate = p.getBoolean("ddl.generate", ddlGenerate); ddlRun = p.getBoolean("ddl.run", ddlRun); + ddlCreateOnly = p.getBoolean("ddl.createOnly", ddlCreateOnly); classes = getClasses(p); } diff --git a/src/main/java/com/avaje/ebean/dbmigration/DdlGenerator.java b/src/main/java/com/avaje/ebean/dbmigration/DdlGenerator.java index 52de4f34d..852ce7527 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/DdlGenerator.java +++ b/src/main/java/com/avaje/ebean/dbmigration/DdlGenerator.java @@ -33,6 +33,7 @@ public class DdlGenerator implements SpiEbeanPlugin { private boolean generateDdl; private boolean runDdl; + private boolean createOnly; private CurrentModel currentModel; private String dropContent; @@ -42,6 +43,7 @@ public class DdlGenerator implements SpiEbeanPlugin { this.server = server; this.generateDdl = serverConfig.isDdlGenerate(); this.runDdl = serverConfig.isDdlRun(); + this.createOnly = serverConfig.isDdlCreateOnly(); } /** @@ -60,7 +62,9 @@ public class DdlGenerator implements SpiEbeanPlugin { */ public void generateDdl() { if (generateDdl) { - writeDrop(getDropFileName()); + if (!createOnly) { + writeDrop(getDropFileName()); + } writeCreate(getCreateFileName()); } } @@ -78,7 +82,9 @@ public class DdlGenerator implements SpiEbeanPlugin { if (createContent == null) { createContent = readFile(getCreateFileName()); } - runScript(true, dropContent); + if (!createOnly) { + runScript(true, dropContent); + } runScript(false, createContent); } catch (IOException e) {