Skip to content

Commit

Permalink
perf-data-loader: suggested order of data loading into tables
Browse files Browse the repository at this point in the history
  • Loading branch information
shaharuk-yb committed Jul 23, 2024
1 parent cb716bc commit fe43d0b
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,41 @@ public void getDistinctValuesFromParentTable(Connection conn, List<ForeignKey> f
while (rs.next()) {
distinctValues.add(rs.getObject(1));
}
if (distinctValues.isEmpty()) {
List<String> hierarchy = getParentTableHierarchy(foreignKey.getForeignTableName(), conn);

StringBuilder loadOrder = new StringBuilder(String.format("There are no entries in the parent " +
"table `%s` for column `%s` to be used as foreign key. Consider loading tables in " +
"following order: ", foreignKey.getForeignTableName(),
foreignKey.getForeignColumnName()));
for (String table: hierarchy) {
loadOrder.append(table).append(" --> ");
}
loadOrder.append(foreignKey.getTableName());
throw new RuntimeException(loadOrder.toString());
}
foreignKey.setDistinctValues(distinctValues);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static List<String> getParentTableHierarchy(String tableName, Connection conn) {
List<String> tableHierarchy = new ArrayList<>();
findParentTables(tableName, conn, tableHierarchy);
return tableHierarchy;
}

private static void findParentTables(String tableName, Connection conn, List<String> tableHierarchy) {
List<ForeignKey> foreignKeys = getForeignKeys(tableName, conn);

if (foreignKeys.isEmpty()) {
tableHierarchy.add(tableName);
} else {
for (ForeignKey foreignKey : foreignKeys) {
findParentTables(foreignKey.getForeignTableName(), conn, tableHierarchy);
}
tableHierarchy.add(tableName);
}
}
}

0 comments on commit fe43d0b

Please sign in to comment.