cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject [07/16] cayenne git commit: New DbMerger for dbsync utils - merger process split in independent steps - new attributes compared (full type comparision of DbAttribute)
Date Wed, 14 Dec 2016 19:12:43 GMT
New DbMerger for dbsync utils
 - merger process split in independent steps
 - new attributes compared (full type comparision of DbAttribute)

Source filtering and additional tests


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

Branch: refs/heads/master
Commit: c86094f07373649f48cfd4f99daec1e98e6c1fae
Parents: 34ae195
Author: Nikita Timofeev <stariy95@gmail.com>
Authored: Mon Dec 12 11:13:34 2016 +0300
Committer: Nikita Timofeev <stariy95@gmail.com>
Committed: Mon Dec 12 11:13:34 2016 +0300

----------------------------------------------------------------------
 .../cayenne/dbsync/merge/AbstractMerger.java    |  2 +
 .../cayenne/dbsync/merge/ChainMerger.java       | 13 +++-
 .../cayenne/dbsync/merge/DataMapMerger.java     | 49 ++++++++-----
 .../cayenne/dbsync/merge/DbAttributeMerger.java | 15 ++--
 .../dbsync/merge/DbEntityDictionary.java        | 25 ++++++-
 .../cayenne/dbsync/merge/DbEntityMerger.java    | 16 ++++-
 .../dbsync/merge/DbRelationshipMerger.java      |  6 ++
 .../cayenne/dbsync/merge/DbMergerTest.java      | 74 ++++++++++++++++----
 8 files changed, 154 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/AbstractMerger.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/AbstractMerger.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/AbstractMerger.java
index c6772e1..ba3c35f 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/AbstractMerger.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/AbstractMerger.java
@@ -78,6 +78,8 @@ abstract class AbstractMerger<T, M> implements Merger<T> {
         }
     }
 
+    public abstract List<MergerToken> createMergeTokens();
+
     abstract MergerDictionaryDiff<M> createDiff(T original, T imported);
 
     abstract Collection<MergerToken> createTokensForMissingOriginal(M imported);

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/ChainMerger.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/ChainMerger.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/ChainMerger.java
index 04583d6..e70c295 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/ChainMerger.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/ChainMerger.java
@@ -20,6 +20,7 @@
 package org.apache.cayenne.dbsync.merge;
 
 import java.util.Collection;
+import java.util.List;
 
 import org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory;
 import org.apache.cayenne.dbsync.merge.token.MergerToken;
@@ -31,15 +32,21 @@ class ChainMerger<T, M> extends AbstractMerger<T, M> {
 
     private final AbstractMerger<?, T> parentMerger;
 
-    ChainMerger(MergerTokenFactory tokenFactory, DataMap original, DataMap imported, AbstractMerger<T,
M> merger, AbstractMerger<?, T> parentMerger) {
+    ChainMerger(MergerTokenFactory tokenFactory, DataMap original, DataMap imported,
+                AbstractMerger<T, M> merger, AbstractMerger<?, T> parentMerger)
{
         super(tokenFactory, original, imported);
         this.merger = merger;
         this.parentMerger = parentMerger;
     }
 
     @Override
-    MergerDictionaryDiff<M> createDiff(T original, T imported) {
-        MergerDictionaryDiff<M> diff = new MergerDictionaryDiff<M>();
+    public List<MergerToken> createMergeTokens() {
+        return createMergeTokens(null, null);
+    }
+
+    @Override
+    MergerDictionaryDiff<M> createDiff(T unused1, T unused2) {
+        MergerDictionaryDiff<M> diff = new MergerDictionaryDiff<>();
         MergerDictionaryDiff<T> parentDiff = parentMerger.getDiff();
         for(MergerDiffPair<T> pair : parentDiff.getSame()) {
             diff.addAll(merger.createDiff(pair.getOriginal(), pair.getImported()));

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DataMapMerger.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DataMapMerger.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DataMapMerger.java
index 51bea0d..ce49565 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DataMapMerger.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DataMapMerger.java
@@ -38,7 +38,7 @@ import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbRelationship;
 
 /**
- * Synchronization data base store and Cayenne model.
+ * Synchronization of data base store and Cayenne model.
  */
 public class DataMapMerger implements Merger<DataMap> {
 
@@ -47,6 +47,8 @@ public class DataMapMerger implements Merger<DataMap> {
     private boolean skipRelationshipsTokens;
     private boolean skipPKTokens;
     private FiltersConfig filters;
+    private DbEntityMerger dbEntityMerger;
+    private List<AbstractMerger<?, ?>> mergerList = new ArrayList<>();
 
     private DataMapMerger() {
     }
@@ -55,42 +57,51 @@ public class DataMapMerger implements Merger<DataMap> {
      * Create List of MergerToken that represent the difference between two {@link DataMap}
objects.
      */
     public List<MergerToken> createMergeTokens(DataMap original, DataMap importedFromDb)
{
-        List<MergerToken> tokens = new ArrayList<>();
+        prepare(original, importedFromDb);
 
-        DbEntityMerger dbEntityMerger = mergeDbEntities(tokens, original, importedFromDb);
-        mergeAttributes(tokens, dbEntityMerger, original, importedFromDb);
-        mergeRelationships(tokens, dbEntityMerger, original, importedFromDb);
+        createDbEntityMerger(original, importedFromDb);
+        createAttributeMerger(original, importedFromDb);
+        createRelationshipMerger(original, importedFromDb);
 
-        Collections.sort(tokens, new TokenComparator());
+        return createTokens();
+    }
+
+    private void prepare(DataMap original, DataMap imported) {
+        imported.setQuotingSQLIdentifiers(original.isQuotingSQLIdentifiers());
+    }
 
+    private List<MergerToken> createTokens() {
+        List<MergerToken> tokens = new ArrayList<>();
+        for(AbstractMerger<?, ?> merger : mergerList) {
+            tokens.addAll(merger.createMergeTokens());
+        }
+        Collections.sort(tokens, new TokenComparator());
         return tokens;
     }
 
-    private DbEntityMerger mergeDbEntities(List<MergerToken> tokens, DataMap original,
DataMap importedFromDb) {
-        DbEntityMerger dbEntityMerger = new DbEntityMerger(tokenFactory, skipPKTokens, original,
importedFromDb);
-        tokens.addAll(dbEntityMerger.createMergeTokens(original, importedFromDb));
-        return dbEntityMerger;
+    private void createDbEntityMerger(DataMap original, DataMap imported) {
+        dbEntityMerger = new DbEntityMerger(tokenFactory, original, imported, filters, skipPKTokens);
+        mergerList.add(dbEntityMerger);
     }
 
-    private void mergeAttributes(List<MergerToken> tokens, DbEntityMerger dbEntityMerger,
DataMap original, DataMap importedFromDb) {
+    private void createAttributeMerger(DataMap original, DataMap imported) {
         ChainMerger<DbEntity, DbAttribute> dbAttributeMerger = new ChainMerger<>(
-                tokenFactory, original, importedFromDb,
-                new DbAttributeMerger(tokenFactory, original, importedFromDb, valueForNull),
+                tokenFactory, original, imported,
+                new DbAttributeMerger(tokenFactory, original, imported, valueForNull),
                 dbEntityMerger
         );
-        tokens.addAll(dbAttributeMerger.createMergeTokens(null, null));
+        mergerList.add(dbAttributeMerger);
     }
 
-    private void mergeRelationships(List<MergerToken> tokens, DbEntityMerger dbEntityMerger,
DataMap original, DataMap importedFromDb) {
+    private void createRelationshipMerger(DataMap original, DataMap imported) {
         ChainMerger<DbEntity, DbRelationship> dbRelationshipMerger = new ChainMerger<>(
-                tokenFactory, original, importedFromDb,
-                new DbRelationshipMerger(tokenFactory, original, importedFromDb, skipRelationshipsTokens),
+                tokenFactory, original, imported,
+                new DbRelationshipMerger(tokenFactory, original, imported, skipRelationshipsTokens),
                 dbEntityMerger
         );
-        tokens.addAll(dbRelationshipMerger.createMergeTokens(null, null));
+        mergerList.add(dbRelationshipMerger);
     }
 
-
     public static Builder builder(MergerTokenFactory tokenFactory) {
         return new Builder(tokenFactory);
     }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbAttributeMerger.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbAttributeMerger.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbAttributeMerger.java
index cf26201..caee8e4 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbAttributeMerger.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbAttributeMerger.java
@@ -90,11 +90,9 @@ class DbAttributeMerger extends AbstractMerger<DbEntity, DbAttribute>
{
         checkMandatory(same.getOriginal(), same.getImported(), tokens);
         // check type (including max length, scale and precision)
         checkType(same.getOriginal(), same.getImported(), tokens);
-
         // not implemented yet
         // isGenerated flag
-        //checkIsGenerated(same.getOriginal(), same.getImported(), tokens);
-
+        checkIsGenerated(same.getOriginal(), same.getImported(), tokens);
 
         return tokens;
     }
@@ -121,7 +119,7 @@ class DbAttributeMerger extends AbstractMerger<DbEntity, DbAttribute>
{
      * @param imported attribute from db
      * @return true if attributes not same
      */
-    private boolean needUpdate(DbAttribute original, DbAttribute imported) {
+    private boolean needUpdateType(DbAttribute original, DbAttribute imported) {
         if(original.getType() != imported.getType()) {
             return true;
         }
@@ -142,11 +140,11 @@ class DbAttributeMerger extends AbstractMerger<DbEntity, DbAttribute>
{
     }
 
     private void checkType(DbAttribute original, DbAttribute imported, List<MergerToken>
tokens) {
-        if(!needUpdate(original, imported)) {
+        if(!needUpdateType(original, imported)) {
             return;
         }
         DbEntity originalDbEntity = original.getEntity();
-        tokens.add(getTokenFactory().createSetColumnTypeToDb(originalDbEntity, original,
imported));
+        tokens.add(getTokenFactory().createSetColumnTypeToDb(originalDbEntity, imported,
original));
     }
 
     private void checkIsGenerated(DbAttribute original, DbAttribute imported, List<MergerToken>
tokens) {
@@ -156,4 +154,9 @@ class DbAttributeMerger extends AbstractMerger<DbEntity, DbAttribute>
{
 
 
     }
+
+    @Override
+    public List<MergerToken> createMergeTokens() {
+        throw new UnsupportedOperationException();
+    }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityDictionary.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityDictionary.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityDictionary.java
index 9114711..8f2068b 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityDictionary.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityDictionary.java
@@ -20,7 +20,10 @@
 package org.apache.cayenne.dbsync.merge;
 
 import java.util.Collection;
+import java.util.LinkedList;
 
+import org.apache.cayenne.dbsync.reverse.filters.FiltersConfig;
+import org.apache.cayenne.dbsync.reverse.filters.TableFilter;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbEntity;
 
@@ -28,8 +31,11 @@ class DbEntityDictionary extends MergerDictionary<DbEntity> {
 
     private final DataMap container;
 
-    DbEntityDictionary(DataMap container) {
+    private final FiltersConfig filtersConfig;
+
+    DbEntityDictionary(DataMap container, FiltersConfig filtersConfig) {
         this.container = container;
+        this.filtersConfig = filtersConfig;
     }
 
     @Override
@@ -39,6 +45,21 @@ class DbEntityDictionary extends MergerDictionary<DbEntity> {
 
     @Override
     Collection<DbEntity> getAll() {
-        return container.getDbEntities();
+        return filter();
+    }
+
+    private Collection<DbEntity> filter() {
+        if(filtersConfig == null) {
+            return container.getDbEntities();
+        }
+
+        Collection<DbEntity> existingFiltered = new LinkedList<>();
+        for (DbEntity entity : container.getDbEntities()) {
+            TableFilter tableFilter = filtersConfig.tableFilter(entity.getCatalog(), entity.getSchema());
+            if (tableFilter != null && tableFilter.isIncludeTable(entity.getName())
!= null) {
+                existingFiltered.add(entity);
+            }
+        }
+        return existingFiltered;
     }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityMerger.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityMerger.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityMerger.java
index b718005..e6a0247 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityMerger.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbEntityMerger.java
@@ -22,10 +22,12 @@ package org.apache.cayenne.dbsync.merge;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Set;
 
 import org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory;
 import org.apache.cayenne.dbsync.merge.token.MergerToken;
+import org.apache.cayenne.dbsync.reverse.filters.FiltersConfig;
 import org.apache.cayenne.map.Attribute;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbAttribute;
@@ -34,18 +36,26 @@ import org.apache.cayenne.map.DetectedDbEntity;
 
 class DbEntityMerger extends AbstractMerger<DataMap, DbEntity> {
 
+    private final FiltersConfig filtersConfig;
     private final boolean skipPKTokens;
 
-    DbEntityMerger(MergerTokenFactory tokenFactory, boolean skipPKTokens, DataMap original,
DataMap imported) {
+    DbEntityMerger(MergerTokenFactory tokenFactory, DataMap original, DataMap imported,
+                   FiltersConfig filtersConfig, boolean skipPKTokens) {
         super(tokenFactory, original, imported);
+        this.filtersConfig = filtersConfig;
         this.skipPKTokens = skipPKTokens;
     }
 
     @Override
+    public List<MergerToken> createMergeTokens() {
+        return createMergeTokens(originalDataMap, importedDataMap);
+    }
+
+    @Override
     MergerDictionaryDiff<DbEntity> createDiff(DataMap original, DataMap imported) {
         return new MergerDictionaryDiff.Builder<DbEntity>()
-                .originalDictionary(new DbEntityDictionary(original))
-                .importedDictionary(new DbEntityDictionary(imported))
+                .originalDictionary(new DbEntityDictionary(original, filtersConfig))
+                .importedDictionary(new DbEntityDictionary(imported, null))
                 .build();
     }
 

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipMerger.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipMerger.java
b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipMerger.java
index a094cb2..9bac4bb 100644
--- a/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipMerger.java
+++ b/cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/merge/DbRelationshipMerger.java
@@ -21,6 +21,7 @@ package org.apache.cayenne.dbsync.merge;
 
 import java.util.Collection;
 import java.util.Collections;
+import java.util.List;
 
 import org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory;
 import org.apache.cayenne.dbsync.merge.token.MergerToken;
@@ -124,4 +125,9 @@ public class DbRelationshipMerger extends AbstractMerger<DbEntity,
DbRelationshi
         }
         return null;
     }
+
+    @Override
+    public List<MergerToken> createMergeTokens() {
+        throw new UnsupportedOperationException();
+    }
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/c86094f0/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/DbMergerTest.java
----------------------------------------------------------------------
diff --git a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/DbMergerTest.java
b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/DbMergerTest.java
index 821f254..55097e2 100644
--- a/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/DbMergerTest.java
+++ b/cayenne-dbsync/src/test/java/org/apache/cayenne/dbsync/merge/DbMergerTest.java
@@ -97,7 +97,7 @@ public class DbMergerTest {
     }
 
     @Test
-    public void testChangeColumnType() throws Exception {
+    public void testChangeColumnTypeSimple() throws Exception {
         DataMap existing = dataMap().with(
                 dbEntity("table1").attributes(
                         dbAttr("attr01").typeInt()
@@ -156,25 +156,73 @@ public class DbMergerTest {
      * Test unsupported type changes
      */
     @Test
-    public void testNotChangeColumnType() throws Exception {
+    public void testChangeColumnType() throws Exception {
+        DbEntity fromModel = dbEntity("table1").attributes(
+                dbAttr("attr01").typeInt(),
+                dbAttr("attr02").type("DATE"),
+                dbAttr("attr03").type("BOOLEAN"),
+                dbAttr("attr04").type("FLOAT")
+        ).build();
+        DataMap existing = dataMap().with(fromModel).build();
+
+        DbEntity fromDb = dbEntity("table1").attributes(
+                dbAttr("attr01").typeBigInt(),
+                dbAttr("attr02").type("NUMERIC"),
+                dbAttr("attr03").type("BLOB"),
+                dbAttr("attr04").type("TIMESTAMP")
+        ).build();
+        DataMap db = dataMap().with(fromDb).build();
+
+        List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
+        assertEquals(4, tokens.size());
+
+        assertEquals(
+                factory()
+                        .createSetColumnTypeToDb(fromModel, fromDb.getAttribute("attr02"),
fromModel.getAttribute("attr02"))
+                        .getTokenValue(),
+                tokens.get(1)
+                        .getTokenValue()
+        );
+
+        for(MergerToken token : tokens) {
+            assertTrue(token instanceof SetColumnTypeToDb);
+        }
+    }
+
+    @Test
+    public void testDropPrimaryKey() throws Exception {
         DataMap existing = dataMap().with(
                 dbEntity("table1").attributes(
-                        dbAttr("attr01").typeInt(),
-                        dbAttr("attr02").type("DATE"),
-                        dbAttr("attr03").type("BOOLEAN"),
-                        dbAttr("attr04").type("FLOAT")
-                )).build();
+                        dbAttr("attr01").typeInt()
+                )
+        ).build();
 
         DataMap db = dataMap().with(
                 dbEntity("table1").attributes(
-                        dbAttr("attr01").typeBigInt(),
-                        dbAttr("attr02").type("NUMERIC"),
-                        dbAttr("attr03").type("BLOB"),
-                        dbAttr("attr04").type("TIMESTAMP")
-                )).build();
+                        dbAttr("attr01").typeInt().primaryKey()
+                )
+        ).build();
+
+        List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
+        assertEquals(1, tokens.size());
+    }
+
+    @Test
+    public void testAddPrimaryKey() throws Exception {
+        DataMap existing = dataMap().with(
+                dbEntity("table1").attributes(
+                        dbAttr("attr01").typeInt().primaryKey()
+                )
+        ).build();
+
+        DataMap db = dataMap().with(
+                dbEntity("table1").attributes(
+                        dbAttr("attr01").typeInt()
+                )
+        ).build();
 
         List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
-        assertEquals(0, tokens.size());
+        assertEquals(1, tokens.size());
     }
 
     @Test


Mime
View raw message