Skip to content

Commit 8339590

Browse files
committed
Add tests for MySqlBulkCopy.ConflictOption. Fixes #1148
1 parent a1a72a5 commit 8339590

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

src/MySqlConnector/MySqlBulkCopy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ private async ValueTask<MySqlBulkCopyResult> WriteToServerAsync(IOBehavior ioBeh
344344

345345
Log.Debug("Finished bulk copy to {0}", tableName);
346346

347-
if (!m_wasAborted && rowsInserted != m_rowsCopied)
347+
if (!m_wasAborted && rowsInserted != m_rowsCopied && ConflictOption is MySqlBulkLoaderConflictOption.None)
348348
{
349349
Log.Error("Bulk copy to DestinationTableName={0} failed; RowsCopied={1}; RowsInserted={2}", tableName, m_rowsCopied, rowsInserted);
350350
throw new MySqlException(MySqlErrorCode.BulkCopyFailed, "{0} rows {1} copied to {2} but only {3} {4} inserted.".FormatInvariant(m_rowsCopied, m_rowsCopied == 1 ? "was" : "were", tableName, rowsInserted, rowsInserted == 1 ? "was" : "were"));

tests/SideBySide/BulkLoaderSync.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1094,6 +1094,68 @@ public void BulkCopyNullDataReader()
10941094
var bulkCopy = new MySqlBulkCopy(connection);
10951095
Assert.Throws<ArgumentNullException>(() => bulkCopy.WriteToServer(default(DbDataReader)));
10961096
}
1097+
1098+
[Theory]
1099+
[InlineData(MySqlBulkLoaderConflictOption.None, 1, "one")]
1100+
[InlineData(MySqlBulkLoaderConflictOption.Ignore, 1, "one")]
1101+
[InlineData(MySqlBulkLoaderConflictOption.Replace, 3, "two")]
1102+
public void BulkCopyDataTableConflictOption(MySqlBulkLoaderConflictOption conflictOption, int expectedRowsInserted, string expected)
1103+
{
1104+
var dataTable = new DataTable()
1105+
{
1106+
Columns =
1107+
{
1108+
new DataColumn("id", typeof(int)),
1109+
new DataColumn("data", typeof(string)),
1110+
},
1111+
Rows =
1112+
{
1113+
new object[] { 1, "one" },
1114+
new object[] { 1, "two" },
1115+
},
1116+
};
1117+
1118+
using var connection = new MySqlConnection(GetLocalConnectionString());
1119+
connection.Open();
1120+
using (var cmd = new MySqlCommand(@"drop table if exists bulk_load_data_table;
1121+
create table bulk_load_data_table(a int not null primary key auto_increment, b text);", connection))
1122+
{
1123+
cmd.ExecuteNonQuery();
1124+
}
1125+
1126+
var bulkCopy = new MySqlBulkCopy(connection)
1127+
{
1128+
ConflictOption = conflictOption,
1129+
DestinationTableName = "bulk_load_data_table",
1130+
};
1131+
1132+
switch (conflictOption)
1133+
{
1134+
case MySqlBulkLoaderConflictOption.None:
1135+
var exception = Assert.Throws<MySqlException>(() => bulkCopy.WriteToServer(dataTable));
1136+
Assert.Equal(MySqlErrorCode.BulkCopyFailed, exception.ErrorCode);
1137+
break;
1138+
1139+
case MySqlBulkLoaderConflictOption.Replace:
1140+
var replaceResult = bulkCopy.WriteToServer(dataTable);
1141+
Assert.Equal(expectedRowsInserted, replaceResult.RowsInserted);
1142+
Assert.Empty(replaceResult.Warnings);
1143+
break;
1144+
1145+
case MySqlBulkLoaderConflictOption.Ignore:
1146+
var ignoreResult = bulkCopy.WriteToServer(dataTable);
1147+
Assert.Equal(expectedRowsInserted, ignoreResult.RowsInserted);
1148+
if (!connection.ServerVersion.StartsWith("5.6.", StringComparison.Ordinal))
1149+
{
1150+
var error = Assert.Single(ignoreResult.Warnings);
1151+
Assert.Equal(MySqlErrorCode.DuplicateKeyEntry, error.ErrorCode);
1152+
}
1153+
break;
1154+
}
1155+
1156+
using (var cmd = new MySqlCommand("select b from bulk_load_data_table;", connection))
1157+
Assert.Equal(expected, cmd.ExecuteScalar());
1158+
}
10971159
#endif
10981160

10991161
internal static string GetConnectionString() => AppConfig.ConnectionString;

0 commit comments

Comments
 (0)