Skip to content

Commit

Permalink
Merge branch '3.9' of https://github.com/JumpMind/symmetric-ds.git in…
Browse files Browse the repository at this point in the history
…to 3.9
  • Loading branch information
jumpmind-josh committed Oct 11, 2018
2 parents eddd847 + 0f630be commit 4c0fce6
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 38 deletions.
Expand Up @@ -208,4 +208,7 @@ public Map<Integer, List<TriggerRouter>> fillTriggerRoutersByHistId(
public Collection<Trigger> findMatchingTriggers(List<Trigger> triggers, String catalog, String schema,
String table);

public List<Table> getTablesFor(List<TriggerHistory> histories);

public List<Table> getSortedTablesFor(List<TriggerHistory> histories);
}
Expand Up @@ -41,6 +41,7 @@
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Column;
import org.jumpmind.db.model.Database;
import org.jumpmind.db.model.ForeignKey;
import org.jumpmind.db.model.Reference;
import org.jumpmind.db.model.Table;
Expand Down Expand Up @@ -443,6 +444,7 @@ public void insertReloadEvents(Node targetNode, boolean reverse, List<TableReloa
triggerHistories = channelTriggerHistories;
}
}
Database.logMissingDependentTableNames(triggerRouterService.getTablesFor(triggerHistories));
} else {
for (TableReloadRequest reloadRequest : reloadRequests) {
triggerHistories.addAll(engine.getTriggerRouterService()
Expand Down
Expand Up @@ -2204,7 +2204,11 @@ protected Map<Integer, List<TriggerRouter>> fillTriggerRoutersByHistId(
return triggerRoutersByHistoryId;
}

protected List<Table> getSortedTablesFor(List<TriggerHistory> histories) {
public List<Table> getSortedTablesFor(List<TriggerHistory> histories) {
return Database.sortByForeignKeys(getTablesFor(histories), null, null, null);
}

public List<Table> getTablesFor(List<TriggerHistory> histories) {
List<Table> tables = new ArrayList<Table>(histories.size());
for (TriggerHistory triggerHistory : histories) {
Table table = platform.getTableFromCache(triggerHistory.getSourceCatalogName(),
Expand All @@ -2214,7 +2218,7 @@ protected List<Table> getSortedTablesFor(List<TriggerHistory> histories) {
tables.add(table);
}
}
return Database.sortByForeignKeys(tables, null, tablePrefix, null, null);
return tables;
}

protected void awaitTermination(ExecutorService executor, List<Future<?>> futures) {
Expand Down
64 changes: 47 additions & 17 deletions symmetric-db/src/main/java/org/jumpmind/db/model/Database.java
Expand Up @@ -90,7 +90,7 @@ public class Database implements Serializable, Cloneable {
* foreign key for table B then table B will precede table A in the
* list.
*/
public static List<Table> sortByForeignKeys(List<Table> tables, Map<String, Table> allTables, String tablePrefix,
public static List<Table> sortByForeignKeys(List<Table> tables, Map<String, Table> allTables,
Map<Integer, Set<Table>> dependencyMap, Map<Table, Set<String>> missingDependencyMap) {

if (allTables == null) {
Expand Down Expand Up @@ -120,16 +120,53 @@ public static List<Table> sortByForeignKeys(List<Table> tables, Map<String, Tabl
if (t != null) {
depth.setValue(1);
parentPosition.setValue(-1);
resolveForeginKeyOrder(t, allTables, resolved, temporary, finalList, null, missingDependencyMap,
resolveForeignKeyOrder(t, allTables, resolved, temporary, finalList, null, missingDependencyMap,
dependencyMap, depth, position, resolvedPosition, parentPosition);
}
}

Collections.reverse(finalList);
return finalList;
}

public static void logMissingDependentTableNames(List<Table> tables) {
Map<String, List<String>> missingTablesByChildTable = findMissingDependentTableNames(tables);
for (String childTableName : missingTablesByChildTable.keySet()) {
List<String> missingTables = missingTablesByChildTable.get(childTableName);
StringBuilder dependentTables = new StringBuilder();
for (String missingTableName : missingTables) {
if (dependentTables.length() > 0) {
dependentTables.append(", ");
}
dependentTables.append(missingTableName);
}
log.info("Unable to resolve foreign keys for table " + childTableName + " because the following dependent tables were not included [" + dependentTables.toString() + "].");
}
}

public static void resolveForeginKeyOrder(Table t, Map<String, Table> allTables, Set<Table> resolved, Set<Table> temporary,
public static Map<String, List<String>> findMissingDependentTableNames(List<Table> tables) {
Map<String, List<String>> missingTablesByChildTable = new HashMap<String, List<String>>();
Map<String, Table> allTables = new HashMap<String, Table>();
for (Table t : tables) {
allTables.put(t.getName(), t);
}

for (Table table : tables) {
List<String> missingTables = missingTablesByChildTable.get(table.getName());
for (ForeignKey fk : table.getForeignKeys()) {
if (allTables.get(fk.getForeignTableName()) == null) {
if (missingTables == null) {
missingTables = new ArrayList<String>();
missingTablesByChildTable.put(table.getName(), missingTables);
}
missingTables.add(fk.getForeignTableName());
}
}
}
return missingTablesByChildTable;
}

public static void resolveForeignKeyOrder(Table t, Map<String, Table> allTables, Set<Table> resolved, Set<Table> temporary,
List<Table> finalList, Table parentTable, Map<Table, Set<String>> missingDependencyMap,
Map<Integer, Set<Table>> dependencyMap, MutableInt depth, MutableInt position,
Map<Table, Integer> resolvedPosition, MutableInt parentPosition) {
Expand All @@ -138,26 +175,19 @@ public static void resolveForeginKeyOrder(Table t, Map<String, Table> allTables,
parentPosition.setValue(resolvedPosition.get(t));
return;
}

if (temporary.contains(t)) {log.info("Possible circular dependent: " + t.getName()); return; }

if (!temporary.contains(t) && !resolved.contains(t)) {
Set<Integer> parentTablesChannels = new HashSet<Integer>();
if (t == null) {
if (parentTable != null) {
StringBuilder dependentTables = new StringBuilder();
for (ForeignKey fk : parentTable.getForeignKeys()) {
if (dependentTables.length() > 0) {
dependentTables.append(", ");
}
dependentTables.append(fk.getForeignTableName());
if (missingDependencyMap.get(parentTable) == null) {
missingDependencyMap.put(parentTable, new HashSet<String>());
}
if (allTables.get(fk.getForeignTableName()) == null) {
if (missingDependencyMap.get(parentTable) == null) {
missingDependencyMap.put(parentTable, new HashSet<String>());
}
missingDependencyMap.get(parentTable).add(fk.getForeignTableName());
}
}
log.info("Unable to resolve foreign keys for table " + parentTable.getName() + " because the following dependent tables were not included [" + dependentTables.toString() + "].");
}
} else {
temporary.add(t);
Expand All @@ -166,7 +196,7 @@ public static void resolveForeginKeyOrder(Table t, Map<String, Table> allTables,
Table fkTable = allTables.get(fk.getForeignTableName());
if (fkTable != t) {
depth.increment();
resolveForeginKeyOrder(fkTable, allTables, resolved, temporary, finalList, t, missingDependencyMap,
resolveForeignKeyOrder(fkTable, allTables, resolved, temporary, finalList, t, missingDependencyMap,
dependencyMap, depth, position, resolvedPosition, parentPosition);
Integer resolvedParentTableChannel = resolvedPosition.get(fkTable);
if (resolvedParentTableChannel != null) {
Expand Down Expand Up @@ -260,14 +290,14 @@ public static Table[] sortByForeignKeys(Table... tables) {
for (Table table : tables) {
list.add(table);
}
list = sortByForeignKeys(list, null, null, null, null);
list = sortByForeignKeys(list, null, null, null);
tables = list.toArray(new Table[list.size()]);
}
return tables;
}

public static List<Table> sortByForeignKeys(List<Table> tables) {
return sortByForeignKeys(tables, null, null, null, null);
return sortByForeignKeys(tables, null, null, null);
}

/**
Expand Down
32 changes: 16 additions & 16 deletions symmetric-db/src/test/java/org/jumpmind/db/model/DatabaseTest.java
Expand Up @@ -53,7 +53,7 @@ public void testOrderingOfFourTables() {
list.add(t4);
list.add(t3);

list = Database.sortByForeignKeys(list, null, null, null, null);
list = Database.sortByForeignKeys(list, null, null, null);

assertTrue(list.toString(), list.indexOf(t4) < list.indexOf(t1));
assertTrue(list.toString(), list.indexOf(t2) < list.indexOf(t1));
Expand Down Expand Up @@ -93,7 +93,7 @@ public void testOrderingOfTenTables() {
list.add(t8);


list = Database.sortByForeignKeys(list, null, null, null, null);
list = Database.sortByForeignKeys(list, null, null, null);

assertTrue(list.toString(), list.indexOf(t4) < list.indexOf(t5));
assertTrue(list.toString(), list.indexOf(t3) < list.indexOf(t4));
Expand All @@ -118,7 +118,7 @@ public void testCyclicalReferences() {
list.add(t2);
list.add(t1);

list = Database.sortByForeignKeys(list, null, null, null, null);
list = Database.sortByForeignKeys(list, null, null, null);

// for now just make sure it doesn't blow up

Expand All @@ -144,7 +144,7 @@ public void testMultipleParentsTables() {
list.add(t4);
list.add(t3);

list = Database.sortByForeignKeys(list, null, null, null, null);
list = Database.sortByForeignKeys(list, null, null, null);

assertTrue(list.toString(), list.indexOf(t2) < list.indexOf(t1));
assertTrue(list.toString(), list.indexOf(t3) < list.indexOf(t2));
Expand All @@ -169,7 +169,7 @@ public void testSplitTreeTables() {
list.add(t4);
list.add(t3);

list = Database.sortByForeignKeys(list, null, null, null, null);
list = Database.sortByForeignKeys(list, null, null, null);

assertTrue(list.toString(), list.indexOf(t3) < list.indexOf(t4));
assertTrue(list.toString(), list.indexOf(t3) < list.indexOf(t2));
Expand All @@ -194,7 +194,7 @@ public void testIndependentTreesSameTables() {
list.add(t3);
list.add(t4);

list = Database.sortByForeignKeys(list, null, null, null, null);
list = Database.sortByForeignKeys(list, null, null, null);

assertTrue(list.toString(), list.indexOf(t3) < list.indexOf(t4));
assertTrue(list.toString(), list.indexOf(t3) < list.indexOf(t2));
Expand All @@ -221,7 +221,7 @@ public void testSelfReferenceTables() {
list.add(t3);
list.add(t4);

list = Database.sortByForeignKeys(list, null, null, null, null);
list = Database.sortByForeignKeys(list, null, null, null);

assertTrue(list.toString(), list.indexOf(t4) < list.indexOf(t3));
assertTrue(list.toString(), list.indexOf(t3) < list.indexOf(t2));
Expand All @@ -244,7 +244,7 @@ public void testMissingDepdendentTables() {
list.add(t4);

Map<Table, Set<String>> missingDependencyMap = new HashMap<Table, Set<String>>();
list = Database.sortByForeignKeys(list, null, null, null, missingDependencyMap);
list = Database.sortByForeignKeys(list, null, null, missingDependencyMap);

assertTrue(list.toString(), list.indexOf(t1) < list.indexOf(t4));

Expand Down Expand Up @@ -284,7 +284,7 @@ public void testDependentMapIndependent() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.size() == 3);
}
Expand All @@ -304,7 +304,7 @@ public void testDependentMapParentChild() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.get(1).contains(t1));
assertTrue(dependencyMap.get(1).contains(t2));
Expand All @@ -327,7 +327,7 @@ public void testDependentMapParentChildReverseOrder() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.get(1).contains(t1));
assertTrue(dependencyMap.get(1).contains(t2));
Expand Down Expand Up @@ -360,7 +360,7 @@ public void testDependentMapTwoGroups() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.get(1).contains(t1));
assertTrue(dependencyMap.get(1).contains(t2));
Expand Down Expand Up @@ -394,7 +394,7 @@ public void testDependentMapCircular() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.get(1).contains(t1));
assertTrue(dependencyMap.get(2).contains(t2));
Expand Down Expand Up @@ -426,7 +426,7 @@ public void testDependentMapMultipleParents() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.get(1).contains(t1));
assertTrue(dependencyMap.get(1).contains(t2));
Expand Down Expand Up @@ -458,7 +458,7 @@ public void testDependentMapMergeGroups() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.get(1).contains(t1));
assertTrue(dependencyMap.get(1).contains(t2));
Expand Down Expand Up @@ -487,7 +487,7 @@ public void testDependentMapOutOfOrder() throws Exception {

Map<Integer, Set<Table>> dependencyMap = new HashMap<Integer, Set<Table>>();

list = Database.sortByForeignKeys(list, null, null, dependencyMap, null);
list = Database.sortByForeignKeys(list, null, dependencyMap, null);

assertTrue(dependencyMap.get(1).contains(t1));
assertTrue(dependencyMap.get(1).contains(t4));
Expand Down
Expand Up @@ -29,7 +29,6 @@
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand Down Expand Up @@ -224,7 +223,7 @@ public void fillTables(String[] tableNames, Map<String,int[]> tableProperties) {
}
}
log.info("TABLES " + tables.size());
tables = Database.sortByForeignKeys(tables, getAllDbTables(), null, null, null);
tables = Database.sortByForeignKeys(tables, getAllDbTables(), null, null);

StringBuffer tableOrder = new StringBuffer();
for(Table t : tables) {
Expand Down
Expand Up @@ -74,6 +74,8 @@ public class DefaultDatabaseWriter extends AbstractDatabaseWriter {
protected DmlStatement currentDmlStatement;

protected Object[] currentDmlValues;

protected LogSqlBuilder logSqlBuilder = new LogSqlBuilder();

public DefaultDatabaseWriter(IDatabasePlatform platform) {
this(platform, null, null);
Expand Down Expand Up @@ -688,7 +690,13 @@ protected void logFailureDetails(Throwable e, CsvData data, boolean logLastDmlDe

if (logLastDmlDetails && this.currentDmlStatement != null) {
failureMessage.append("Failed sql was: ");
failureMessage.append(this.currentDmlStatement.getSql());
String dynamicSQL = logSqlBuilder.buildDynamicSqlForLog(this.currentDmlStatement.getSql(), currentDmlValues, this.currentDmlStatement.getTypes());
failureMessage.append(dynamicSQL);
if (!dynamicSQL.equals(this.currentDmlStatement.getSql())) {
failureMessage.append("\n");
failureMessage.append("Failed raw sql was: ");
failureMessage.append(this.currentDmlStatement.getSql());
}
failureMessage.append("\n");
}

Expand Down

0 comments on commit 4c0fce6

Please sign in to comment.