cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r614382 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/merge/ test/java/org/apache/cayenne/merge/
Date Wed, 23 Jan 2008 00:00:47 GMT
Author: torehalset
Date: Tue Jan 22 16:00:30 2008
New Revision: 614382

URL: http://svn.apache.org/viewvc?rev=614382&view=rev
Log:
CAY-966: ObjRelationsship's are not removed processing DropColumnToModel
made DropColumnToModel drop FKs. this is also done by DropRelationshipToModel, but users (like
Oyvind) might not want to remove relationships in the model that does not exist in the db.

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropRelationshipToModelTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToModelToken.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToModelToken.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToModelToken.java?rev=614382&r1=614381&r2=614382&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToModelToken.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToModelToken.java
Tue Jan 22 16:00:30 2008
@@ -23,8 +23,10 @@
 import java.util.Set;
 
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.MappingNamespace;
 import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.util.EntityMergeSupport;
 
 /**
@@ -55,6 +57,32 @@
             }
         }
         return objEntities;
+    }
+    
+    protected void remove(DbRelationship rel, boolean reverse) {
+        if (rel == null) {
+            return;
+        }
+        if (reverse) {
+            remove(rel.getReverseRelationship(), false);
+        }
+
+        DbEntity dbEntity = (DbEntity) rel.getSourceEntity();
+        for (ObjEntity objEntity : objEntitiesMappedToDbEntity(dbEntity)) {
+            remove(objEntity.getRelationshipForDbRelationship(rel), true);
+        }
+        
+        rel.getSourceEntity().removeRelationship(rel.getName());
+    }
+
+    protected void remove(ObjRelationship rel, boolean reverse) {
+        if (rel == null) {
+            return;
+        }
+        if (reverse) {
+            remove(rel.getReverseRelationship(), false);
+        }
+        rel.getSourceEntity().removeRelationship(rel.getName());
     }
 
     @Override

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java?rev=614382&r1=614381&r2=614382&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToModel.java
Tue Jan 22 16:00:30 2008
@@ -20,6 +20,8 @@
 
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbJoin;
+import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.ObjAttribute;
 import org.apache.cayenne.map.ObjEntity;
 
@@ -42,13 +44,32 @@
         return factory.createAddColumnToDb(entity, column);
     }
 
+    public DbEntity getEntity() {
+        return entity;
+    }
+
+    public DbAttribute getAttribute() {
+        return column;
+    }
+
     public void execute(MergerContext mergerContext) {
+
+        // remove relationships mapped to column
+        for (DbRelationship dbRelationship : entity.getRelationships()) {
+            for (DbJoin join : dbRelationship.getJoins()) {
+                if (join.getSource() == column || join.getTarget() == column) {
+                    remove(dbRelationship, true);
+                }
+            }
+        }
+
         // remove ObjAttribute mapped to same column
         for (ObjEntity objEntity : objEntitiesMappedToDbEntity(entity)) {
             ObjAttribute objAttribute = objEntity.getAttributeForDbAttribute(column);
             if (objAttribute != null) {
                 objEntity.removeAttribute(objAttribute.getName());
             }
+
         }
 
         // remove DbAttribute

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java?rev=614382&r1=614381&r2=614382&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropRelationshipToModel.java
Tue Jan 22 16:00:30 2008
@@ -20,8 +20,6 @@
 
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbRelationship;
-import org.apache.cayenne.map.ObjEntity;
-import org.apache.cayenne.map.ObjRelationship;
 
 public class DropRelationshipToModel extends AbstractToModelToken {
 
@@ -39,32 +37,6 @@
 
     public void execute(MergerContext mergerContext) {
         remove(rel, true);
-    }
-
-    private void remove(DbRelationship rel, boolean reverse) {
-        if (rel == null) {
-            return;
-        }
-        if (reverse) {
-            remove(rel.getReverseRelationship(), false);
-        }
-
-        DbEntity dbEntity = (DbEntity) rel.getSourceEntity();
-        for (ObjEntity objEntity : objEntitiesMappedToDbEntity(dbEntity)) {
-            remove(objEntity.getRelationshipForDbRelationship(rel), true);
-        }
-        
-        rel.getSourceEntity().removeRelationship(rel.getName());
-    }
-
-    private void remove(ObjRelationship rel, boolean reverse) {
-        if (rel == null) {
-            return;
-        }
-        if (reverse) {
-            remove(rel.getReverseRelationship(), false);
-        }
-        rel.getSourceEntity().removeRelationship(rel.getName());
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java?rev=614382&r1=614381&r2=614382&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropColumnToModelTest.java
Tue Jan 22 16:00:30 2008
@@ -91,7 +91,7 @@
         assertTokensAndExecute(node, map, 0, 0);
     }
 
-    public void testForreignKey() throws Exception {
+    public void testRemoveFKColumnWithoutRelationshipInDb() throws Exception {
         dropTableIfPresent(node, "NEW_TABLE");
         dropTableIfPresent(node, "NEW_TABLE2");
 
@@ -124,7 +124,13 @@
 
         map.addDbEntity(dbEntity2);
 
-        // create db relationships
+        assertTokensAndExecute(node, map, 2, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+        
+        // force drop fk column in db
+        execute(mergerFactory().createDropColumToDb(dbEntity2, e2col2));
+
+        // create db relationships, but do not sync them to db
         DbRelationship rel1To2 = new DbRelationship("rel1To2");
         rel1To2.setSourceEntity(dbEntity1);
         rel1To2.setTargetEntity(dbEntity2);
@@ -140,9 +146,6 @@
         assertSame(rel1To2, rel2To1.getReverseRelationship());
         assertSame(rel2To1, rel1To2.getReverseRelationship());
 
-        assertTokensAndExecute(node, map, 4, 0);
-        assertTokensAndExecute(node, map, 0, 0);
-
         // create ObjEntities
         ObjEntity objEntity1 = new ObjEntity("NewTable");
         objEntity1.setDbEntity(dbEntity1);
@@ -152,9 +155,9 @@
         objEntity1.addAttribute(oatr1);
         map.addObjEntity(objEntity1);
         ObjEntity objEntity2 = new ObjEntity("NewTable2");
-        objEntity2.setDbEntity(dbEntity1);
+        objEntity2.setDbEntity(dbEntity2);
         ObjAttribute o2a1 = new ObjAttribute("name");
-        o2a1.setDbAttributePath(e1col2.getName());
+        o2a1.setDbAttributePath(e2col3.getName());
         o2a1.setType("java.lang.String");
         objEntity2.addAttribute(o2a1);
         map.addObjEntity(objEntity2);
@@ -177,31 +180,17 @@
         assertSame(objRel1To2, objRel2To1.getReverseRelationship());
         assertSame(objRel2To1, objRel1To2.getReverseRelationship());
 
-        // remove relationship and fk from model, merge to db and read to model
-        dbEntity2.removeRelationship(rel2To1.getName());
-        dbEntity1.removeRelationship(rel1To2.getName());
-        dbEntity2.removeAttribute(e2col2.getName());
+        // try do use the merger to remove the column and relationship in the model
         List<MergerToken> tokens = createMergeTokens();
-        assertTokens(tokens, 2, 1);
-        for (MergerToken token : tokens) {
-            if (token.getDirection().isToDb()) {
-                execute(token);
-            }
-        }
-        assertTokensAndExecute(0, 0);
-        dbEntity2.addRelationship(rel2To1);
-        dbEntity1.addRelationship(rel1To2);
-        dbEntity2.addAttribute(e2col2);
-        
-        // try do use the merger to remove the relationship in the model
-        tokens = createMergeTokens();
         assertTokens(tokens, 2, 0);
         // TODO: reversing the following two tokens should also reverse the order
         MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
         MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
-        assertTrue(token0 instanceof DropColumnToModel);
-        assertTrue(token1 instanceof DropRelationshipToModel);
-        execute(token1);
+        assertTrue(token0.getClass().getName(), token0 instanceof DropColumnToModel);
+        //assertTrue(((DropColumnToModel)token0).getEntity() == dbEntity2);
+        //assertTrue(((DropColumnToModel)token0).getAttribute() == e2col2);
+        assertTrue(token1.getClass().getName(), token1 instanceof DropRelationshipToModel);
+        // do not execute DropRelationshipToModel, only DropColumnToModel. 
         execute(token0);
         
         // check after merging

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropRelationshipToModelTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropRelationshipToModelTest.java?rev=614382&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropRelationshipToModelTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/DropRelationshipToModelTest.java
Tue Jan 22 16:00:30 2008
@@ -0,0 +1,176 @@
+/*****************************************************************
+ *   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.cayenne.merge;
+
+import java.sql.Types;
+import java.util.List;
+
+import org.apache.cayenne.access.DataContext;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.DbJoin;
+import org.apache.cayenne.map.DbRelationship;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
+import org.apache.cayenne.map.ObjRelationship;
+
+
+public class DropRelationshipToModelTest extends MergeCase {
+
+    public void testForreignKey() throws Exception {
+        dropTableIfPresent(node, "NEW_TABLE");
+        dropTableIfPresent(node, "NEW_TABLE2");
+
+        assertTokensAndExecute(node, map, 0, 0);
+
+        DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
+
+        DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
+        e1col1.setMandatory(true);
+        e1col1.setPrimaryKey(true);
+        dbEntity1.addAttribute(e1col1);
+
+        DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
+        e1col2.setMaxLength(10);
+        e1col2.setMandatory(false);
+        dbEntity1.addAttribute(e1col2);
+
+        map.addDbEntity(dbEntity1);
+
+        DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
+        DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
+        e2col1.setMandatory(true);
+        e2col1.setPrimaryKey(true);
+        dbEntity2.addAttribute(e2col1);
+        DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
+        dbEntity2.addAttribute(e2col2);
+        DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2);
+        e2col3.setMaxLength(10);
+        dbEntity2.addAttribute(e2col3);
+
+        map.addDbEntity(dbEntity2);
+
+        // create db relationships
+        DbRelationship rel1To2 = new DbRelationship("rel1To2");
+        rel1To2.setSourceEntity(dbEntity1);
+        rel1To2.setTargetEntity(dbEntity2);
+        rel1To2.setToMany(true);
+        rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
+        dbEntity1.addRelationship(rel1To2);
+        DbRelationship rel2To1 = new DbRelationship("rel2To1");
+        rel2To1.setSourceEntity(dbEntity2);
+        rel2To1.setTargetEntity(dbEntity1);
+        rel2To1.setToMany(false);
+        rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
+        dbEntity2.addRelationship(rel2To1);
+        assertSame(rel1To2, rel2To1.getReverseRelationship());
+        assertSame(rel2To1, rel1To2.getReverseRelationship());
+
+        assertTokensAndExecute(node, map, 4, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+
+        // create ObjEntities
+        ObjEntity objEntity1 = new ObjEntity("NewTable");
+        objEntity1.setDbEntity(dbEntity1);
+        ObjAttribute oatr1 = new ObjAttribute("name");
+        oatr1.setDbAttributePath(e1col2.getName());
+        oatr1.setType("java.lang.String");
+        objEntity1.addAttribute(oatr1);
+        map.addObjEntity(objEntity1);
+        ObjEntity objEntity2 = new ObjEntity("NewTable2");
+        objEntity2.setDbEntity(dbEntity2);
+        ObjAttribute o2a1 = new ObjAttribute("name");
+        o2a1.setDbAttributePath(e2col3.getName());
+        o2a1.setType("java.lang.String");
+        objEntity2.addAttribute(o2a1);
+        map.addObjEntity(objEntity2);
+        
+        // create ObjRelationships
+        assertEquals(0, objEntity1.getRelationships().size());
+        assertEquals(0, objEntity2.getRelationships().size());
+        ObjRelationship objRel1To2 = new ObjRelationship("objRel1To2");
+        objRel1To2.addDbRelationship(rel1To2);
+        objRel1To2.setSourceEntity(objEntity1);
+        objRel1To2.setTargetEntity(objEntity2);
+        objEntity1.addRelationship(objRel1To2);
+        ObjRelationship objRel2To1 = new ObjRelationship("objRel2To1");
+        objRel2To1.addDbRelationship(rel2To1);
+        objRel2To1.setSourceEntity(objEntity2);
+        objRel2To1.setTargetEntity(objEntity1);
+        objEntity2.addRelationship(objRel2To1);
+        assertEquals(1, objEntity1.getRelationships().size());
+        assertEquals(1, objEntity2.getRelationships().size());
+        assertSame(objRel1To2, objRel2To1.getReverseRelationship());
+        assertSame(objRel2To1, objRel1To2.getReverseRelationship());
+
+        // remove relationship and fk from model, merge to db and read to model
+        dbEntity2.removeRelationship(rel2To1.getName());
+        dbEntity1.removeRelationship(rel1To2.getName());
+        dbEntity2.removeAttribute(e2col2.getName());
+        List<MergerToken> tokens = createMergeTokens();
+        assertTokens(tokens, 2, 1);
+        for (MergerToken token : tokens) {
+            if (token.getDirection().isToDb()) {
+                execute(token);
+            }
+        }
+        assertTokensAndExecute(0, 0);
+        dbEntity2.addRelationship(rel2To1);
+        dbEntity1.addRelationship(rel1To2);
+        dbEntity2.addAttribute(e2col2);
+        
+        // try do use the merger to remove the relationship in the model
+        tokens = createMergeTokens();
+        assertTokens(tokens, 2, 0);
+        // TODO: reversing the following two tokens should also reverse the order
+        MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
+        MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
+        assertTrue(token0 instanceof DropColumnToModel);
+        assertTrue(token1 instanceof DropRelationshipToModel);
+        execute(token1);
+        execute(token0);
+        
+        // check after merging
+        assertNull(dbEntity2.getAttribute(e2col2.getName()));
+        assertEquals(0, dbEntity1.getRelationships().size());
+        assertEquals(0, dbEntity2.getRelationships().size());
+        assertEquals(0, objEntity1.getRelationships().size());
+        assertEquals(0, objEntity2.getRelationships().size());
+
+        // clear up
+        DataContext ctxt = createDataContext();
+        dbEntity1.removeRelationship(rel1To2.getName());
+        dbEntity2.removeRelationship(rel2To1.getName());
+        map.removeObjEntity(objEntity1.getName(), true);
+        map.removeDbEntity(dbEntity1.getName(), true);
+        map.removeObjEntity(objEntity2.getName(), true);
+        map.removeDbEntity(dbEntity2.getName(), true);
+        ctxt.getEntityResolver().clearCache();
+        assertNull(map.getObjEntity(objEntity1.getName()));
+        assertNull(map.getDbEntity(dbEntity1.getName()));
+        assertNull(map.getObjEntity(objEntity2.getName()));
+        assertNull(map.getDbEntity(dbEntity2.getName()));
+        assertFalse(map.getDbEntities().contains(dbEntity1));
+        assertFalse(map.getDbEntities().contains(dbEntity2));
+
+        assertTokensAndExecute(2, 0);
+        assertTokensAndExecute(0, 0);
+    }
+
+}



Mime
View raw message