From 0c368ed7b381e4a6f0c62e3b43a05ac50ecd548b Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Thu, 1 Mar 2018 23:48:35 +0100 Subject: [PATCH] FIX: extra-ddl can contribute to "drop-all" script, otherwise the drop all script is not appyable to h2:file (#1301) --- .../ebeaninternal/dbmigration/DdlGenerator.java | 10 +++++++++- .../dbmigration/DefaultDbMigration.java | 3 ++- .../ebeaninternal/extraddl/model/DdlScript.java | 16 ++++++++++++++++ .../extraddl/model/ExtraDdlXmlReader.java | 5 +++-- .../extraddl/model/ExtraDdlXmlReaderTest.java | 6 +++--- src/test/resources/extra-ddl.xml | 3 +++ 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java b/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java index e6ed35533..2a54e6f9a 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java +++ b/src/main/java/io/ebeaninternal/dbmigration/DdlGenerator.java @@ -127,6 +127,14 @@ public class DdlGenerator { protected void runDropSql() throws IOException { if (!createOnly) { + String ignoreExtraDdl = System.getProperty("ebean.ignoreExtraDdl"); + if (!"true".equalsIgnoreCase(ignoreExtraDdl) && jaxbPresent) { + String extraApply = ExtraDdlXmlReader.buildExtra(server.getDatabasePlatform().getName(), true); + if (extraApply != null) { + runScript(false, extraApply, "extra-dll"); + } + } + if (dropAllContent == null) { dropAllContent = readFile(getDropFileName()); } @@ -142,7 +150,7 @@ public class DdlGenerator { String ignoreExtraDdl = System.getProperty("ebean.ignoreExtraDdl"); if (!"true".equalsIgnoreCase(ignoreExtraDdl) && jaxbPresent) { - String extraApply = ExtraDdlXmlReader.buildExtra(server.getDatabasePlatform().getName()); + String extraApply = ExtraDdlXmlReader.buildExtra(server.getDatabasePlatform().getName(), false); if (extraApply != null) { runScript(false, extraApply, "extra-dll"); } diff --git a/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java b/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java index 9bf36c631..900e049f3 100644 --- a/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java +++ b/src/main/java/io/ebeaninternal/dbmigration/DefaultDbMigration.java @@ -268,7 +268,8 @@ public class DefaultDbMigration implements DbMigration { if (extraDdl != null) { List ddlScript = extraDdl.getDdlScript(); for (DdlScript script : ddlScript) { - if (ExtraDdlXmlReader.matchPlatform(dbPlatform.getName(), script.getPlatforms())) { + if (script.isDrop() == false + && ExtraDdlXmlReader.matchPlatform(dbPlatform.getName(), script.getPlatforms())) { writeExtraDdl(migrationDir, script); } } diff --git a/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java b/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java index c5bd66989..3b0c945ec 100644 --- a/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java +++ b/src/main/java/io/ebeaninternal/extraddl/model/DdlScript.java @@ -37,6 +37,8 @@ public class DdlScript { protected String name; @XmlAttribute(name = "platforms") protected String platforms; + @XmlAttribute(name = "drop") + protected boolean drop; /** * Gets the value of the value property. @@ -98,4 +100,18 @@ public class DdlScript { this.platforms = value; } + /** + * Return if this a drop script. + */ + public boolean isDrop() { + return drop; + } + + /** + * Sets that this is a drop script. + */ + public void setDrop(boolean drop) { + this.drop = drop; + } + } diff --git a/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java b/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java index 1d3fdd723..b9c7e8392 100644 --- a/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java +++ b/src/main/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReader.java @@ -20,7 +20,7 @@ public class ExtraDdlXmlReader { /** * Return the combined extra DDL that should be run given the platform name. */ - public static String buildExtra(String platformName) { + public static String buildExtra(String platformName, boolean drops) { ExtraDdl read = ExtraDdlXmlReader.read("/extra-ddl.xml"); if (read == null) { @@ -28,7 +28,8 @@ public class ExtraDdlXmlReader { } StringBuilder sb = new StringBuilder(300); for (DdlScript script : read.getDdlScript()) { - if (matchPlatform(platformName, script.getPlatforms())) { + if (script.isDrop() == drops + && matchPlatform(platformName, script.getPlatforms())) { logger.debug("include script {}", script.getName()); String value = script.getValue(); sb.append(value); diff --git a/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java b/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java index b6e71e8e5..440e889c0 100644 --- a/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java +++ b/src/test/java/io/ebeaninternal/extraddl/model/ExtraDdlXmlReaderTest.java @@ -17,7 +17,7 @@ public class ExtraDdlXmlReaderTest { @Test public void buildExtra_when_h2() { - String ddl = ExtraDdlXmlReader.buildExtra("h2"); + String ddl = ExtraDdlXmlReader.buildExtra("h2", false); assertThat(ddl).contains("create or replace view order_agg_vw"); assertThat(ddl).contains("-- h2 and postgres script"); @@ -27,7 +27,7 @@ public class ExtraDdlXmlReaderTest { @Test public void buildExtra_when_oracle() { - String ddl = ExtraDdlXmlReader.buildExtra("oracle"); + String ddl = ExtraDdlXmlReader.buildExtra("oracle", false); assertThat(ddl).contains("create or replace view order_agg_vw"); assertThat(ddl).doesNotContain("-- h2 and postgres script"); @@ -37,7 +37,7 @@ public class ExtraDdlXmlReaderTest { @Test public void buildExtra_when_mysql() { - String ddl = ExtraDdlXmlReader.buildExtra("mysql"); + String ddl = ExtraDdlXmlReader.buildExtra("mysql", false); assertThat(ddl).contains("create or replace view order_agg_vw"); assertThat(ddl).doesNotContain("-- h2 and postgres script"); diff --git a/src/test/resources/extra-ddl.xml b/src/test/resources/extra-ddl.xml index 42e27bcd3..8ca843ad6 100644 --- a/src/test/resources/extra-ddl.xml +++ b/src/test/resources/extra-ddl.xml @@ -1,6 +1,9 @@ + + drop view order_agg_vw if exists; + create or replace view order_agg_vw as