From 8e5921c9e76b4540f1d4b75e05e338001145ff6d Mon Sep 17 00:00:00 2001
From: Kai Moritz <kai@juplo.de>
Date: Wed, 16 Dec 2015 22:09:00 +0100
Subject: [PATCH] Introduced the goal "drop"

 * Fixed integration-test hibernate4-maven-plugin-envers-sample by adapting
   it to the new drop-goal
 * Adapted the other integration-tests to the new naming schema for the
   create-script
---
 src/it/h4mp-mod/verify.bsh                    |   2 +-
 .../create-tables-hsqldb.sql                  |  12 +--
 .../drop-tables-hsqldb.sql                    |   2 +-
 .../pom.xml                                   |   2 -
 src/it/ignored-dependency/verify.bsh          |   2 +-
 src/it/properties/verify.bsh                  |   2 +-
 src/it/schemaexport-example/verify.bsh        |   2 +-
 src/it/tutorials/verify.bsh                   |  14 +--
 .../juplo/plugins/hibernate/CreateMojo.java   |   2 +-
 .../de/juplo/plugins/hibernate/DropMojo.java  | 100 ++++++++++++++++++
 10 files changed, 119 insertions(+), 21 deletions(-)
 create mode 100644 src/main/java/de/juplo/plugins/hibernate/DropMojo.java

diff --git a/src/it/h4mp-mod/verify.bsh b/src/it/h4mp-mod/verify.bsh
index e13f8002..96ecf32f 100644
--- a/src/it/h4mp-mod/verify.bsh
+++ b/src/it/h4mp-mod/verify.bsh
@@ -3,5 +3,5 @@ import de.juplo.test.FileComparator;
 
 FileComparator comparator = new FileComparator(basedir);
 
-if (!comparator.isEqual("schema.sql","target/schema.sql"))
+if (!comparator.isEqual("schema.sql","target/create.sql"))
   return false;
diff --git a/src/it/hibernate4-maven-plugin-envers-sample/create-tables-hsqldb.sql b/src/it/hibernate4-maven-plugin-envers-sample/create-tables-hsqldb.sql
index 53262938..180166f7 100644
--- a/src/it/hibernate4-maven-plugin-envers-sample/create-tables-hsqldb.sql
+++ b/src/it/hibernate4-maven-plugin-envers-sample/create-tables-hsqldb.sql
@@ -1,7 +1,11 @@
 
+    create sequence TTL_AUDIT_REVISION_SEQ start with 1 increment by 10;
+
+    create sequence TTL_EVENT_SEQ start with 1 increment by 10;
+
     create table TTL_AUDIT_REVISION (
         ID bigint not null,
-        EVENT_DATE timestamp not null,
+        EVENT_DATE timestamp,
         ENVERS_TSTAMP bigint not null,
         USER_NAME varchar(80) not null,
         primary key (ID)
@@ -24,10 +28,6 @@
     );
 
     alter table TTL_EVENT_AUD 
-        add constraint FK_cjsh8995uabmdm9b30uvmyj6p 
+        add constraint FK295td34se5kaxwgo8i1ph0k4 
         foreign key (REV) 
         references TTL_AUDIT_REVISION;
-
-    create sequence TTL_AUDIT_REVISION_SEQ start with 1;
-
-    create sequence TTL_EVENT_SEQ start with 1;
diff --git a/src/it/hibernate4-maven-plugin-envers-sample/drop-tables-hsqldb.sql b/src/it/hibernate4-maven-plugin-envers-sample/drop-tables-hsqldb.sql
index 04b457d0..d1237677 100644
--- a/src/it/hibernate4-maven-plugin-envers-sample/drop-tables-hsqldb.sql
+++ b/src/it/hibernate4-maven-plugin-envers-sample/drop-tables-hsqldb.sql
@@ -1,6 +1,6 @@
 
     alter table TTL_EVENT_AUD 
-        drop constraint FK_cjsh8995uabmdm9b30uvmyj6p;
+        drop constraint FK295td34se5kaxwgo8i1ph0k4;
 
     drop table TTL_AUDIT_REVISION if exists;
 
diff --git a/src/it/hibernate4-maven-plugin-envers-sample/pom.xml b/src/it/hibernate4-maven-plugin-envers-sample/pom.xml
index ef0f7935..c77f3b39 100644
--- a/src/it/hibernate4-maven-plugin-envers-sample/pom.xml
+++ b/src/it/hibernate4-maven-plugin-envers-sample/pom.xml
@@ -187,7 +187,6 @@
                         <configuration>
                             <outputFile>${project.build.directory}/test-classes/sql/create-tables-hsqldb.sql
                             </outputFile>
-                            <drop>false</drop>
                             <format>true</format>
                         </configuration>
                     </execution>
@@ -199,7 +198,6 @@
                         <configuration>
                             <outputFile>${project.build.directory}/test-classes/sql/drop-tables-hsqldb.sql
                             </outputFile>
-                            <drop>true</drop>
                             <format>true</format>
                         </configuration>
                     </execution>
diff --git a/src/it/ignored-dependency/verify.bsh b/src/it/ignored-dependency/verify.bsh
index 47470ce6..d350caaa 100644
--- a/src/it/ignored-dependency/verify.bsh
+++ b/src/it/ignored-dependency/verify.bsh
@@ -3,5 +3,5 @@ import de.juplo.test.FileComparator;
 
 FileComparator comparator = new FileComparator(basedir);
 
-if (!comparator.isEqual("schema.sql","main/target/schema.sql"))
+if (!comparator.isEqual("schema.sql","main/target/create.sql"))
   return false;
diff --git a/src/it/properties/verify.bsh b/src/it/properties/verify.bsh
index 0dfcefe5..53bc2c9e 100644
--- a/src/it/properties/verify.bsh
+++ b/src/it/properties/verify.bsh
@@ -3,5 +3,5 @@ import de.juplo.test.FileComparator;
 
 FileComparator comparator = new FileComparator(basedir);
 
-if (!comparator.isEqual("h2.sql","target/schema.sql"))
+if (!comparator.isEqual("h2.sql","target/create.sql"))
   return false;
diff --git a/src/it/schemaexport-example/verify.bsh b/src/it/schemaexport-example/verify.bsh
index 6e51858d..3e56c4c6 100644
--- a/src/it/schemaexport-example/verify.bsh
+++ b/src/it/schemaexport-example/verify.bsh
@@ -3,5 +3,5 @@ import de.juplo.test.FileComparator;
 
 FileComparator comparator = new FileComparator(basedir);
 
-if (!comparator.isEqual("schema.sql","schemaexport-example-persistence-impl/target/schema.sql"))
+if (!comparator.isEqual("schema.sql","schemaexport-example-persistence-impl/target/create.sql"))
   return false;
diff --git a/src/it/tutorials/verify.bsh b/src/it/tutorials/verify.bsh
index 4164d8b0..9156c4af 100644
--- a/src/it/tutorials/verify.bsh
+++ b/src/it/tutorials/verify.bsh
@@ -3,17 +3,17 @@ import de.juplo.test.FileComparator;
 
 FileComparator comparator = new FileComparator(basedir);
 
-if (!comparator.isEqual("schema-annotations.sql","annotations/target/schema.sql"))
+if (!comparator.isEqual("schema-annotations.sql","annotations/target/create.sql"))
   return false;
-if (!comparator.isEqual("schema-basic.sql","basic/target/schema.sql"))
+if (!comparator.isEqual("schema-basic.sql","basic/target/create.sql"))
   return false;
-if (!comparator.isEqual("schema-entitymanager.sql","entitymanager/target/schema.sql"))
+if (!comparator.isEqual("schema-entitymanager.sql","entitymanager/target/create.sql"))
   return false;
-if (!comparator.isEqual("schema-envers.sql","envers/target/schema.sql"))
+if (!comparator.isEqual("schema-envers.sql","envers/target/create.sql"))
   return false;
-if (!comparator.isEqual("schema-osgi-managed-jpa.sql","osgi/managed-jpa/target/schema.sql"))
+if (!comparator.isEqual("schema-osgi-managed-jpa.sql","osgi/managed-jpa/target/create.sql"))
   return false;
-if (!comparator.isEqual("schema-osgi-unmanaged-jpa.sql","osgi/unmanaged-jpa/target/schema.sql"))
+if (!comparator.isEqual("schema-osgi-unmanaged-jpa.sql","osgi/unmanaged-jpa/target/create.sql"))
   return false;
-if (!comparator.isEqual("schema-osgi-unmanaged-native.sql","osgi/unmanaged-native/target/schema.sql"))
+if (!comparator.isEqual("schema-osgi-unmanaged-native.sql","osgi/unmanaged-native/target/create.sql"))
   return false;
diff --git a/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java b/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java
index a89d58d2..5bafe14d 100644
--- a/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java
+++ b/src/main/java/de/juplo/plugins/hibernate/CreateMojo.java
@@ -41,7 +41,7 @@ public class CreateMojo extends AbstractSchemaMojo
    * relative to the project build directory
    * (<code>project.build.directory</code>).
    *
-   * @parameter property="hibernate.schema.export.create" default-value="schema.sql"
+   * @parameter property="hibernate.schema.export.create" default-value="create.sql"
    * @since 1.0
    */
   private String outputFile;
diff --git a/src/main/java/de/juplo/plugins/hibernate/DropMojo.java b/src/main/java/de/juplo/plugins/hibernate/DropMojo.java
new file mode 100644
index 00000000..969a2198
--- /dev/null
+++ b/src/main/java/de/juplo/plugins/hibernate/DropMojo.java
@@ -0,0 +1,100 @@
+package de.juplo.plugins.hibernate;
+
+/*
+ * Copyright 2001-2005 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.io.File;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoFailureException;
+import org.hibernate.boot.spi.MetadataImplementor;
+import org.hibernate.tool.hbm2ddl.SchemaExport;
+
+
+/**
+ * Goal which extracts the hibernate-mapping-configuration and
+ * exports an according SQL-database-schema.
+ *
+ * @goal drop
+ * @phase process-classes
+ * @threadSafe
+ * @requiresDependencyResolution runtime
+ */
+public class DropMojo extends AbstractSchemaMojo
+{
+  /**
+   * Output file.
+   * <p>
+   * If the specified filename is not absolut, the file will be created
+   * relative to the project build directory
+   * (<code>project.build.directory</code>).
+   *
+   * @parameter property="hibernate.schema.export.drop" default-value="drop.sql"
+   * @since 1.0
+   */
+  private String outputFile;
+
+
+  @Override
+  public final void execute()
+    throws
+      MojoFailureException,
+      MojoExecutionException
+  {
+    super.execute(outputFile);
+  }
+
+
+  @Override
+  void build(MetadataImplementor metadata)
+      throws
+        MojoExecutionException,
+        MojoFailureException
+  {
+    SchemaExport schemaExport = new SchemaExport(metadata, createNamespaces);
+    schemaExport.setDelimiter(delimiter);
+    schemaExport.setFormat(format);
+
+    File output = new File(outputFile);
+
+    if (!output.isAbsolute())
+    {
+      // Interpret relative file path relative to build directory
+      output = new File(buildDirectory, outputFile);
+      getLog().debug("Adjusted relative path, resulting path is " + output.getPath());
+    }
+
+    // Ensure that directory path for specified file exists
+    File outFileParentDir = output.getParentFile();
+    if (null != outFileParentDir && !outFileParentDir.exists())
+    {
+      try
+      {
+        getLog().info("Creating directory path for output file:" + outFileParentDir.getPath());
+        outFileParentDir.mkdirs();
+      }
+      catch (Exception e)
+      {
+        getLog().error("Error creating directory path for output file: " + e.getLocalizedMessage());
+      }
+    }
+
+    schemaExport.setOutputFile(output.getPath());
+    schemaExport.execute(false, this.export, true, false);
+
+    for (Object exception : schemaExport.getExceptions())
+      getLog().error(exception.toString());
+  }
+}
-- 
2.20.1