Skip to content

Commit

Permalink
0002976: Foreign key automatic resolution was not handling nested
Browse files Browse the repository at this point in the history
dependencies properly
  • Loading branch information
jumpmind-josh committed Feb 1, 2017
1 parent 0b6f514 commit 50788bb
Showing 1 changed file with 54 additions and 45 deletions.
Expand Up @@ -1550,51 +1550,56 @@ public String reloadTable(String nodeId, String catalogName, String schemaName,
}

public void reloadMissingForeignKeyRows(String nodeId, long dataId) {
Data data = findData(dataId);
log.debug("reloadMissingForeignKeyRows for nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
TriggerHistory hist = data.getTriggerHistory();
Table table = platform.getTableFromCache(hist.getSourceCatalogName(), hist.getSourceSchemaName(), hist.getSourceTableName(), false);
Map<String, String> dataMap = data.toColumnNameValuePairs(table.getColumnNames(), CsvData.ROW_DATA);

List<TableRow> tableRows = new ArrayList<TableRow>();
Row row = new Row(dataMap.size());
row.putAll(dataMap);
tableRows.add(new TableRow(table, row, null, null, null));
List<TableRow> foreignTableRows;
try {
foreignTableRows = getForeignTableRows(tableRows);
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}

if (foreignTableRows.isEmpty()) {
log.info("Could not determine foreign table rows to fix foreign key violation for "
+ "nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
}

Collections.reverse(foreignTableRows);
Set<TableRow> visited = new HashSet<TableRow>();

for (TableRow foreignTableRow : foreignTableRows) {
if (visited.add(foreignTableRow)) {
Table foreignTable = foreignTableRow.getTable();
String catalog = foreignTable.getCatalog();
String schema = foreignTable.getSchema();
if (StringUtils.equals(platform.getDefaultCatalog(), catalog)) {
catalog = null;
}
if (StringUtils.equals(platform.getDefaultSchema(), schema)) {
schema = null;
}
Data data = findData(dataId);
log.debug("reloadMissingForeignKeyRows for nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
TriggerHistory hist = data.getTriggerHistory();
Table table = platform.getTableFromCache(hist.getSourceCatalogName(), hist.getSourceSchemaName(), hist.getSourceTableName(), false);
Map<String, String> dataMap = data.toColumnNameValuePairs(table.getColumnNames(), CsvData.ROW_DATA);

List<TableRow> tableRows = new ArrayList<TableRow>();
Row row = new Row(dataMap.size());
row.putAll(dataMap);
tableRows.add(new TableRow(table, row, null, null, null));
List<TableRow> foreignTableRows;
try {
foreignTableRows = getForeignTableRows(tableRows);
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}

log.info("Issuing foreign key correction reload "
+ "nodeId {} catalog '{}' schema '{}' foreign table name '{}' fk name '{}' where sql '{}' "
+ "to correct dataId '{}' table '{}' for column '{}'",
nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getFkName(), foreignTableRow.getWhereSql(),
dataId, data.getTableName(), foreignTableRow.getReferenceColumnName());
reloadTable(nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getWhereSql());
if (foreignTableRows.isEmpty()) {
log.info("Could not determine foreign table rows to fix foreign key violation for "
+ "nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
}
}

Collections.reverse(foreignTableRows);
Set<TableRow> visited = new HashSet<TableRow>();

for (TableRow foreignTableRow : foreignTableRows) {
if (visited.add(foreignTableRow)) {
Table foreignTable = foreignTableRow.getTable();
String catalog = foreignTable.getCatalog();
String schema = foreignTable.getSchema();
if (StringUtils.equals(platform.getDefaultCatalog(), catalog)) {
catalog = null;
}
if (StringUtils.equals(platform.getDefaultSchema(), schema)) {
schema = null;
}

log.info("Issuing foreign key correction reload "
+ "nodeId {} catalog '{}' schema '{}' foreign table name '{}' fk name '{}' where sql '{}' "
+ "to correct dataId '{}' table '{}' for column '{}'",
nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getFkName(), foreignTableRow.getWhereSql(),
dataId, data.getTableName(), foreignTableRow.getReferenceColumnName());
reloadTable(nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getWhereSql());
}
}
}
catch (Exception e) {
log.error("Unknown exception while processing foreign key.", e);
}
}

protected List<TableRow> getForeignTableRows(List<TableRow> tableRows) throws CloneNotSupportedException {
Expand Down Expand Up @@ -1635,9 +1640,13 @@ protected List<TableRow> getForeignTableRows(List<TableRow> tableRows) throws Cl
Row foreignRow = new Row(foreignTable.getColumnCount());
if (foreignTable.getForeignKeyCount() > 0) {
DmlStatement selectSt = platform.createDmlStatement(DmlType.SELECT, foreignTable, null);
Map<String, Object> values = sqlTemplate.queryForMap(selectSt.getSql(),
whereRow.toArray(foreignTable.getPrimaryKeyColumnNames()));
foreignRow.putAll(values);
Object[] keys = whereRow.toArray(foreignTable.getPrimaryKeyColumnNames());
Map<String, Object> values = sqlTemplate.queryForMap(selectSt.getSql(), keys);
if (values == null) {
log.warn("Unable to reload rows for missing foreign key data, parent data not found. Using sql='{}' with keys '{}'",selectSt.getSql(), keys);
} else {
foreignRow.putAll(values);

This comment has been minimized.

Copy link
@nguyenhoan

nguyenhoan Feb 16, 2017

Hi @jumpmind-josh
We are a team of researchers from Iowa State, The University of Texs at Dallas and Oregon State University, USA. We are investigating common/repeated code changes.
We have four short questions regarding the change in the image below which is part of this commit.
image

Questions:

Q1- Is the change at these lines similar to another change from before? (yes, no, not sure)

Q2- Can you briefly describe the change and why you made it? (for example, checking parameter before calling the method to avoid a Null Pointer Exception)

Q3- Can you give it a name? (for example, Null Check)

Q4- Would you like to have this change automated by a tool? (Yes, No, Already automated)

The data collected from the answers will never be associated with you or your project. Our questions are about recurring code changes from the developer community, not about personal information. All the data is merged across recurring changes from GitHub repositories. We will publish aggregated data from the trends of the whole community.
We have a long tradition of developing refactoring tools and contributing them freely to the Eclipse, Netbeans, Android Studio under their respective FLOSS licenses. For example, look at some of our recently released refactoring tools: http://refactoring.info/tools/

Thank you,
Hoan Nguyen https://sites.google.com/site/nguyenanhhoan/
Michael Hilton http://web.engr.oregonstate.edu/~hiltonm/
Tien Nguyen http://www.utdallas.edu/~tien.n.nguyen/
Danny Dig http://eecs.oregonstate.edu/people/dig-danny

}
}

TableRow foreignTableRow = new TableRow(foreignTable, foreignRow, whereSql,referenceColumnName, fk.getName());
Expand Down

0 comments on commit 50788bb

Please sign in to comment.