Author: struberg
Date: Sun Jun  7 06:50:58 2015
New Revision: 1683992

URL: http://svn.apache.org/r1683992
Log:
OPENJPA-2596 schema-delta generation (sqlAction=refresh) drops columns if they 
have an alternative typeName

solved by additionally comparing the final column definition end to end

Added:
    
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestMappingToolRefresh.java
   (with props)
Modified:
    
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
    
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java
    
openjpa/trunk/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java

Modified: 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java?rev=1683992&r1=1683991&r2=1683992&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
 (original)
+++ 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/Column.java
 Sun Jun  7 06:50:58 2015
@@ -33,6 +33,7 @@ import org.apache.openjpa.jdbc.identifie
 import org.apache.openjpa.jdbc.identifier.QualifiedDBIdentifier;
 import org.apache.openjpa.jdbc.meta.JavaSQLTypes;
 import org.apache.openjpa.jdbc.meta.VersionStrategy;
+import org.apache.openjpa.jdbc.sql.DBDictionary;
 import org.apache.openjpa.meta.JavaTypes;
 
 /**
@@ -634,7 +635,7 @@ public class Column
      * Return true if this column is compatible with the given JDBC type
      * from {@link Types} and size.
      */
-    public boolean isCompatible(int type, String typeName, int size, 
+    public boolean isCompatible(int type, String typeName, int size,
         int decimals) {
         if (type == Types.OTHER || getType() == Types.OTHER)
             return true;
@@ -723,7 +724,7 @@ public class Column
                      default:
                          return false;
                 }
-                
+
             default:
                 return type == getType();
         }
@@ -753,7 +754,7 @@ public class Column
     /**
      * Tests compatibility.
      */
-    public boolean equalsColumn(Column col) {
+    public boolean equalsColumn(DBDictionary dict, Column col) {
         if (col == this)
             return true;
         if (col == null)
@@ -762,8 +763,14 @@ public class Column
         if (!getQualifiedPath().equals(col.getQualifiedPath()))
             return false;
         if (!isCompatible(col.getType(), col.getTypeIdentifier().getName(), 
col.getSize(),
-            col.getDecimalDigits()))
+            col.getDecimalDigits())) {
+            // do an additional lookup in case the java.sql.Types are 
different but
+            // they map to the same representation in the DB
+            if (dict.getTypeName(this).equals(dict.getTypeName(col))) {
+                return true;
+            }
             return false;
+        }
         if (getType() == Types.VARCHAR && getSize() > 0 && col.getSize() > 0
             && getSize() != col.getSize())
             return false;

Modified: 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java?rev=1683992&r1=1683991&r2=1683992&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java
 (original)
+++ 
openjpa/trunk/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/SchemaTool.java
 Sun Jun  7 06:50:58 2015
@@ -528,7 +528,7 @@ public class SchemaTool {
                             else
                                 _log.warn(_loc.get("add-col", cols[k],
                                         tabs[j]));
-                        } else if (!cols[k].equalsColumn(col)) {
+                        } else if (!cols[k].equalsColumn(_dict, col)) {
                             _log.warn(_loc.get("bad-col", new Object[]{
                                     col, dbTable, col.getDescription(),
                                     cols[k].getDescription() }));
@@ -767,7 +767,7 @@ public class SchemaTool {
                 if (reposTable != null) {
                     for (int k = 0; k < cols.length; k++) {
                         col = reposTable.getColumn(cols[k].getIdentifier());
-                        if (col == null || !cols[k].equalsColumn(col)) {
+                        if (col == null || !cols[k].equalsColumn(_dict, col)) {
                             if (tabs[j].getColumns().length == 1)
                                 drops.add(tabs[j]);
                             else if (dropColumn(cols[k]))

Added: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestMappingToolRefresh.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestMappingToolRefresh.java?rev=1683992&view=auto
==============================================================================
--- 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestMappingToolRefresh.java
 (added)
+++ 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestMappingToolRefresh.java
 Sun Jun  7 06:50:58 2015
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.openjpa.jdbc.meta;
+
+import java.io.StringWriter;
+import java.net.URL;
+import java.util.Map;
+
+
+import org.apache.openjpa.jdbc.conf.JDBCConfiguration;
+import org.apache.openjpa.jdbc.conf.JDBCConfigurationImpl;
+import org.apache.openjpa.jdbc.schema.SchemaTool;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerFactorySPI;
+import org.apache.openjpa.persistence.OpenJPAEntityManagerSPI;
+import org.apache.openjpa.persistence.test.AbstractPersistenceTestCase;
+import org.junit.Test;
+
+/**
+ * Test that a {@link MappingTool#ACTION_REFRESH} uses the right
+ * types for new columns and takes any mapping in DBDictionary into account.
+ */
+public class TestMappingToolRefresh extends AbstractPersistenceTestCase {
+
+    /**
+     * First we create a schema mapping with boolean representation as CHAR(1).
+     * Then we create an entry.
+     * After that we create a diff from the entity to the current DB.
+     * This should result in an empty diff.
+     */
+    @Test
+    public void testSchemaCreation() throws Exception {
+        Map<String, Object> emfProps = getPropertiesMap(EntityBool.class,
+                "openjpa.jdbc.SynchronizeMappings",
+                "buildSchema(ForeignKeys=true, 
SchemaAction='add,deleteTableContents')",
+                "openjpa.jdbc.DBDictionary",
+                
"(BitTypeName=CHAR(1),BooleanTypeName=CHAR(1),BooleanRepresentation=STRING_10)");
+
+
+        {
+            // stage 1. Create the DB and insert a line into it
+            OpenJPAEntityManagerFactorySPI openjpaEmf = 
createNamedOpenJPAEMF("test", null, emfProps);
+
+            OpenJPAEntityManagerSPI em = openjpaEmf.createEntityManager();
+            assertNotNull(em);
+            em.getTransaction().begin();
+            EntityBool val = new EntityBool();
+            val.setDummy(true);
+            em.persist(val);
+
+            em.getTransaction().commit();
+            int id = val.getId();
+            em.close();
+
+            OpenJPAEntityManagerSPI em2 = openjpaEmf.createEntityManager();
+            assertNotNull(em2);
+
+            EntityBool val2 = em2.find(EntityBool.class, id);
+            assertNotNull(val2);
+            assertNotEquals(val, val2);
+
+            openjpaEmf.close();
+        }
+
+        {
+            // now we create a 2nd EntityManagerFactory but with a different 
configuration
+            // we switch the boolean representation to CHAR(1)
+            OpenJPAEntityManagerFactorySPI openjpaEmf = 
createNamedOpenJPAEMF("test", null, emfProps);
+            String metaDataFactory = 
openjpaEmf.getConfiguration().getMetaDataFactory();
+
+            JDBCConfiguration jdbcConf = new JDBCConfigurationImpl();
+            jdbcConf.setMetaDataFactory(metaDataFactory);
+
+            String[] entityClassFiles = new String[1];
+            URL entityClassUrl = this.getClass().getClassLoader().
+                    getResource(EntityBool.class.getName().replace(".", "/") + 
".class");
+            entityClassFiles[0] = entityClassUrl.getFile();
+
+            MappingTool.Flags flags = new MappingTool.Flags();
+            flags.mappingWriter = new StringWriter();
+            flags.action = MappingTool.ACTION_REFRESH;
+            flags.schemaAction = SchemaTool.ACTION_REFRESH;
+            flags.sqlWriter = new StringWriter();
+            flags.schemaWriter = new StringWriter();
+
+            boolean ok = MappingTool.run(jdbcConf, entityClassFiles, flags, 
this.getClass().getClassLoader());
+            assertTrue(ok);
+            assertTrue(flags.sqlWriter.toString().isEmpty());
+
+
+        }
+
+    }
+
+
+}

Propchange: 
openjpa/trunk/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/jdbc/meta/TestMappingToolRefresh.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
openjpa/trunk/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java
URL: 
http://svn.apache.org/viewvc/openjpa/trunk/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java?rev=1683992&r1=1683991&r2=1683992&view=diff
==============================================================================
--- 
openjpa/trunk/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java
 (original)
+++ 
openjpa/trunk/openjpa-tools/openjpa-maven-plugin/src/main/java/org/apache/openjpa/tools/maven/AbstractOpenJpaMojo.java
 Sun Jun  7 06:50:58 2015
@@ -96,7 +96,17 @@ public abstract class AbstractOpenJpaMoj
      * @parameter
      */
     private String persistenceXmlFile;
-    
+
+    /**
+     * An optional PersistenceUnit name.
+     * If not specified then OpenJPA will run against 'all anchors'.
+     * Means it will use all persistenceunits of all persistence.xml files it 
finds.
+     *
+     * @parameter default-value="${openjpa.persistenceUnitName}"
+     */
+    private String persistenceUnitName;
+
+
     /**
      * <p>This setting can be used to override any 
openjpa.ConnectionDriverName set in the
      * persistence.xml. It can also be used if the persistence.xml contains no 
connection
@@ -259,13 +269,22 @@ public abstract class AbstractOpenJpaMoj
         {
           opts.putAll( toolProperties );
         }
-        
-        if ( persistenceXmlFile != null )
+
+        String persistenceXmlResource = "META-INF/persistence.xml";
+
+        if ( persistenceXmlFile != null && persistenceXmlFile.length() > 0)
         {
             
fixPersistenceXmlIfNeeded(Thread.currentThread().getContextClassLoader());
             opts.put( OPTION_PROPERTIES_FILE, persistenceXmlFile );
             getLog().debug("using special persistence XML file: " + 
persistenceXmlFile);
+            persistenceXmlResource = persistenceXmlFile;
         }
+
+        if (persistenceUnitName != null && persistenceUnitName.length() > 0) {
+            opts.put(OPTION_PROPERTIES, persistenceXmlResource + "#" + 
persistenceUnitName);
+        }
+
+
         else if (!new File(classes, "META-INF/persistence.xml").exists())
         { // use default but try from classpath
             persistenceXmlFile = "META-INF/persistence.xml";


Reply via email to