Skip to content

Commit

Permalink
0005271: Conflict Resolution of deletes of parent records fail to delete
Browse files Browse the repository at this point in the history
child records when child tables have no primary keys
  • Loading branch information
Philip Marzullo committed Apr 15, 2022
1 parent 4598254 commit df2dec1
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
Expand Up @@ -56,8 +56,11 @@ public class DatabaseWriterConflictTest extends AbstractWriterTest {
private final static String[] TEST_KEYS_GRANDCHILD = { "id" };

private final static String[] TEST_COLUMNS_GRANDCHILD = { "id", "pid" };
private final static String TEST_TABLE_CHILD_NOPK = "test_dataloader_child_nopk";
private final static String[] TEST_KEYS_CHILD_NOPK = { "id", "pid" };
private final static String[] TEST_COLUMNS_CHILD_NOPK = { "id", "pid" };

private enum WhichTable { PARENT, CHILD, GRANDCHILD };
private enum WhichTable { PARENT, CHILD, GRANDCHILD, CHILD_NOPK };

private WhichTable whichTable;

Expand Down Expand Up @@ -260,6 +263,15 @@ public void testDeleteFkViolationChildTable() throws Exception {
whichTable = WhichTable.PARENT;
delete(firstId, firstId, "deleteparent", null);
}

@Test
public void testDeleteFkViolationChildTableNoPk() throws Exception {
String firstId = insert(getNextId(), "deleteparentnopk", null);
whichTable = WhichTable.CHILD_NOPK;
insert(getNextId(), firstId);
whichTable = WhichTable.PARENT;
delete(firstId, firstId, "deleteparentnopk", null);
}

private String insert(String ...values) {
if (shouldTest) {
Expand Down Expand Up @@ -313,28 +325,46 @@ private void delete(String id, String ...values) {
protected void assertTestTableEquals(String testTableId, String[] expectedValues) {
String sql = "select " + getSelect(getTestColumns()) + " from " + getTestTable() + " where "
+ getWhere(getTestKeys());
Map<String, Object> results = platform.getSqlTemplate().queryForMap(sql, Long.valueOf(testTableId));
Map<String, Object> results = null;
if (whichTable != WhichTable.CHILD_NOPK) {
results = platform.getSqlTemplate().queryForMap(sql, Long.valueOf(testTableId));
} else {
Long[] l = new Long[expectedValues.length];
for (int i = 0; i < expectedValues.length; i++) {
l[i] = Long.valueOf(expectedValues[i]);
}
results = platform.getSqlTemplate().queryForMap(sql, (Object[]) l);
}
assertEquals(getTestColumns(), expectedValues, results);
}

@Override
protected String getTestTable() {
if (whichTable == WhichTable.CHILD) return TEST_TABLE_CHILD;
else if (whichTable == WhichTable.GRANDCHILD) return TEST_TABLE_GRANDCHILD;
else if (whichTable == WhichTable.CHILD_NOPK) {
return TEST_TABLE_CHILD_NOPK;
}
else return TEST_TABLE;
}

@Override
protected String[] getTestKeys() {
if (whichTable == WhichTable.CHILD) return TEST_KEYS_CHILD;
else if (whichTable == WhichTable.GRANDCHILD) return TEST_KEYS_GRANDCHILD;
else if (whichTable == WhichTable.CHILD_NOPK) {
return TEST_KEYS_CHILD_NOPK;
}
else return TEST_KEYS;
}

@Override
protected String[] getTestColumns() {
if (whichTable == WhichTable.CHILD) return TEST_COLUMNS_CHILD;
else if (whichTable == WhichTable.GRANDCHILD) return TEST_COLUMNS_GRANDCHILD;
else if (whichTable == WhichTable.CHILD_NOPK) {
return TEST_COLUMNS_CHILD_NOPK;
}
else return TEST_COLUMNS;
}

Expand Down
Expand Up @@ -261,7 +261,7 @@ protected String getSelect(String[] columns) {
protected String getWhere(String[] columns) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < columns.length; i++) {
str.append(columns[i]).append(" = ?").append(i + 1 < columns.length ? "," : "");
str.append(columns[i]).append(" = ?").append(i + 1 < columns.length ? " AND " : "");
}
return str.toString();
}
Expand Down
8 changes: 8 additions & 0 deletions symmetric-io/src/test/resources/testDatabaseWriter.xml
Expand Up @@ -75,5 +75,13 @@
<column name="string_value" type="VARCHAR" size="50" />
<column name="blob_value" type="BLOB" />
</table>

<table name="test_dataloader_child_nopk">
<column name="id" type="INTEGER" required="true" />
<column name="pid" type="INTEGER" />
<foreign-key foreignTable="test_dataloader_parent" name="fk_test_dataloader_child_nopk_pid">
<reference local="pid" foreign="id" />
</foreign-key>
</table>

</database>
Expand Up @@ -1736,12 +1736,23 @@ public List<TableRow> getExportedForeignTableRows(ISqlTransaction transaction, L

if (rows != null) {
for (Row row : rows) {
// Return a TableRow with a where statement that can access this foreign table row by its primary key
DmlStatement whereSt = platform.createDmlStatement(DmlType.WHERE, foreignTable.getCatalog(),
DmlStatement whereSt = null;
String whereSql = null;
if (foreignTable.getPrimaryKeyColumns() == null || foreignTable.getPrimaryKeyColumns().length == 0) {
// No primary keys, create a where clause using the foreign key columns
whereSt = platform.createDmlStatement(DmlType.WHERE, foreignTable.getCatalog(),
foreignTable.getSchema(), foreignTable.getName(), keyColumns,
foreignTable.getColumns(), nullValues, null);
whereSql = whereSt.buildDynamicSql(BinaryEncoding.HEX, row, false, true,
keyColumns).substring(6);
} else {
// Return a TableRow with a where statement that can access this foreign table row by its primary key
whereSt = platform.createDmlStatement(DmlType.WHERE, foreignTable.getCatalog(),
foreignTable.getSchema(), foreignTable.getName(), foreignTable.getPrimaryKeyColumns(),
foreignTable.getColumns(), nullValues, null);
String whereSql = whereSt.buildDynamicSql(BinaryEncoding.HEX, row, false, true,
whereSql = whereSt.buildDynamicSql(BinaryEncoding.HEX, row, false, true,
foreignTable.getPrimaryKeyColumns()).substring(6);
}
String delimiter = platform.getDatabaseInfo().getSqlCommandDelimiter();
if (delimiter != null && delimiter.length() > 0) {
whereSql = whereSql.substring(0, whereSql.length() - delimiter.length());
Expand Down

0 comments on commit df2dec1

Please sign in to comment.