cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ntimof...@apache.org
Subject [1/2] cayenne git commit: CAY-2440 cdbimport: allow cross-schema relationships
Date Fri, 01 Jun 2018 07:44:18 GMT
Repository: cayenne
Updated Branches:
  refs/heads/master 6ea7f3958 -> 85805afca


CAY-2440 cdbimport: allow cross-schema relationships


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/bc3acb1f
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/bc3acb1f
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/bc3acb1f

Branch: refs/heads/master
Commit: bc3acb1fa3ef30538fec0620c963e6ffcedede5f
Parents: 6ea7f39
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Fri Jun 1 10:41:16 2018 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Fri Jun 1 10:41:16 2018 +0300

----------------------------------------------------------------------
 RELEASE-NOTES.txt                               |  1 +
 .../dbsync/reverse/dbload/ExportedKey.java      | 22 ++++++++++++++++
 .../reverse/dbload/ExportedKeyLoader.java       |  8 +++---
 .../reverse/dbload/RelationshipLoader.java      | 27 ++++++--------------
 .../reverse/dbload/ExportedKeyLoaderIT.java     | 10 ++++++++
 5 files changed, 45 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/bc3acb1f/RELEASE-NOTES.txt
----------------------------------------------------------------------
diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt
index 27fe24d..b7d8a7c 100644
--- a/RELEASE-NOTES.txt
+++ b/RELEASE-NOTES.txt
@@ -35,6 +35,7 @@ CAY-2414 Modeler: new icon design
 CAY-2415 Transaction isolation and propagation support
 CAY-2416 Change TreeMap for HashMap to store data in Cayenne model classes
 CAY-2422 Modeler: Open driver setup window on driver load error
+CAY-2440 cdbimport: allow cross-schema relationships
 
 Bug Fixes:
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bc3acb1f/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
index 6d5f25c..51a5eb6 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKey.java
@@ -19,12 +19,14 @@
 
 package org.apache.cayenne.dbsync.reverse.dbload;
 
+import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.util.EqualsBuilder;
 import org.apache.cayenne.util.HashCodeBuilder;
 import org.apache.cayenne.util.CompareToBuilder;
 
 import java.sql.ResultSet;
 import java.sql.SQLException;
+import java.util.Objects;
 
 /**
  * A representation of relationship between two tables in database. It can be used for creating
names
@@ -216,5 +218,25 @@ public class ExportedKey implements Comparable {
                     .toHashCode();
         }
 
+        /**
+         * Validate that entity is for this key (exists and has same catalog/schema)
+         * @param entity to validate
+         * @return is entity matches for this key
+         */
+        public boolean validateEntity(DbEntity entity) {
+            if (entity == null) {
+                return false;
+            }
+
+            if (!Objects.equals(catalog, entity.getCatalog())) {
+                return false;
+            }
+
+            if (!Objects.equals(schema, entity.getSchema())) {
+                return false;
+            }
+
+            return true;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bc3acb1f/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoader.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoader.java
index 2afc8d1..3dd3d07 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoader.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoader.java
@@ -58,14 +58,14 @@ class ExportedKeyLoader extends PerEntityLoader {
         ExportedKey key = new ExportedKey(rs);
 
         DbEntity pkEntity = map.getDbEntity(key.getPk().getTable());
-        if (pkEntity == null) {
-            LOGGER.info("Skip relation: '" + key + "' because table '" + key.getPk().getTable()
+ "' is not found");
+        if (!key.getPk().validateEntity(pkEntity)) {
+            LOGGER.info("Skip relation: '" + key + "' because table '" + key.getPk().getTable()
+ "' is not found or in different catalog/schema");
             return;
         }
 
         DbEntity fkEntity = map.getDbEntity(key.getFk().getTable());
-        if (fkEntity == null) {
-            LOGGER.info("Skip relation: '" + key + "' because table '" + key.getFk().getTable()
+ "' is not found");
+        if (!key.getFk().validateEntity(fkEntity)) {
+            LOGGER.info("Skip relation: '" + key + "' because table '" + key.getFk().getTable()
+ "' is not found or in different catalog/schema");
             return;
         }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bc3acb1f/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
index 73a0277..aaefc9a 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbload/RelationshipLoader.java
@@ -72,19 +72,6 @@ public class RelationshipLoader extends AbstractLoader {
                 throw new IllegalStateException();
             }
 
-            if (!new EqualsBuilder()
-                    .append(pkEntity.getCatalog(), PK.getCatalog())
-                    .append(pkEntity.getSchema(), PK.getSchema()).append(fkEntity.getCatalog(),
FK.getCatalog())
-                    .append(fkEntity.getSchema(), PK.getSchema()).isEquals()) {
-
-                LOGGER.info("Skip relation: '" + key + "' because it related to objects from
other catalog/schema");
-                LOGGER.info("     relation primary key: '" + PK.getCatalog() + "." + PK.getSchema()
+ "'");
-                LOGGER.info("       primary key entity: '" + pkEntity.getCatalog() + "."
+ pkEntity.getSchema() + "'");
-                LOGGER.info("     relation foreign key: '" + FK.getCatalog() + "." + FK.getSchema()
+ "'");
-                LOGGER.info("       foreign key entity: '" + fkEntity.getCatalog() + "."
+ fkEntity.getSchema() + "'");
-                continue;
-            }
-
             // forwardRelationship is a reference from table with primary key
             // it is what exactly we load from db
             DbRelationship forwardRelationship = new DbRelationship();
@@ -126,11 +113,14 @@ public class RelationshipLoader extends AbstractLoader {
     }
 
     private void checkAndAddRelationship(DbEntity entity, DbRelationship relationship){
-        TableFilter tableFilter = config.getFiltersConfig()
-                .tableFilter(entity.getCatalog(), entity.getSchema());
+        TableFilter sourceTableFilter = config.getFiltersConfig()
+                .tableFilter(relationship.getSourceEntity().getCatalog(), relationship.getSourceEntity().getSchema());
+
+        TableFilter targetTableFilter = config.getFiltersConfig()
+                .tableFilter(relationship.getTargetEntity().getCatalog(), relationship.getTargetEntity().getSchema());
 
         // check that relationship can be included
-        if(!tableFilter.getIncludeTableRelationshipFilter(entity.getName())
+        if(!sourceTableFilter.getIncludeTableRelationshipFilter(entity.getName())
                 .isIncluded(relationship.getName())) {
             return;
         }
@@ -143,8 +133,8 @@ public class RelationshipLoader extends AbstractLoader {
 
         // check that all join attributes are included
         for(DbJoin join : relationship.getJoins()) {
-            if(!tableFilter.getIncludeTableColumnFilter(entity.getName()).isIncluded(join.getSourceName())
||
-                    !tableFilter.getIncludeTableColumnFilter(relationship.getTargetEntityName()).isIncluded(join.getTargetName()))
{
+            if(!sourceTableFilter.getIncludeTableColumnFilter(entity.getName()).isIncluded(join.getSourceName())
||
+                    !targetTableFilter.getIncludeTableColumnFilter(relationship.getTargetEntityName()).isIncluded(join.getTargetName()))
{
                 return;
             }
         }
@@ -190,7 +180,6 @@ public class RelationshipLoader extends AbstractLoader {
                 continue;
             }
 
-
             addJoin(forwardRelationship, pkName, fkName);
             addJoin(reverseRelationship, fkName, pkName);
         }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/bc3acb1f/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoaderIT.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoaderIT.java
b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoaderIT.java
index 73ba353..812581a 100644
--- a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoaderIT.java
+++ b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/reverse/dbload/ExportedKeyLoaderIT.java
@@ -41,15 +41,25 @@ public class ExportedKeyLoaderIT extends BaseLoaderIT {
         createEntity(nameForDb("ARTIST"));
         createEntity(nameForDb("GALLERY"));
         createEntity(nameForDb("PAINTING"));
+
+        String catalog = connection.getCatalog();
+        String schema = connection.getSchema();
+
         DbEntity artist = getDbEntity("ARTIST");
+        artist.setCatalog(catalog);
+        artist.setSchema(schema);
         DbAttribute artistId = new DbAttribute("ARTIST_ID");
         artist.addAttribute(artistId);
 
         DbEntity gallery = getDbEntity("GALLERY");
+        gallery.setCatalog(catalog);
+        gallery.setSchema(schema);
         DbAttribute galleryId = new DbAttribute("GALLERY_ID");
         gallery.addAttribute(galleryId);
 
         DbEntity painting = getDbEntity("PAINTING");
+        painting.setCatalog(catalog);
+        painting.setSchema(schema);
         DbAttribute paintingId = new DbAttribute("PAINTING_ID");
         DbAttribute paintingArtistId = new DbAttribute("ARTIST_ID");
         DbAttribute paintingGalleryId = new DbAttribute("GALLERY_ID");


Mime
View raw message