Skip to content

Commit 7403a1e

Browse files
Copilotphilnach
andcommitted
Refine validation to allow all-primary-key columns with DeleteNotMatchedBySource
- Update validation to allow Upsert mode with only primary key columns when DeleteNotMatchedBySource is true - This enables valid use case of syncing which records exist without updating other columns - Add test case to verify this scenario passes validation Co-authored-by: philnach <[email protected]>
1 parent ac59bb1 commit 7403a1e

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

Extensions/SqlServer/Cosmos.DataTransfer.SqlServerExtension.UnitTests/SqlServerSinkSettingsTests.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,35 @@ public void TestSinkSettings_AllColumnsArePrimaryKeys_FailsValidation()
196196
var validationResults = settings.Validate(new ValidationContext(settings)).ToList();
197197

198198
Assert.IsTrue(validationResults.Any(v => v.MemberNames.Contains(nameof(SqlServerSinkSettings.ColumnMappings))),
199-
"Validation should fail when all columns are primary keys");
199+
"Validation should fail when all columns are primary keys without DeleteNotMatchedBySource");
200200

201201
Assert.IsTrue(validationResults.Any(v => v.ErrorMessage!.Contains("non-primary key column")),
202202
"Validation error should mention non-primary key column requirement");
203203
}
204204

205+
[TestMethod]
206+
public void TestSinkSettings_AllColumnsArePrimaryKeys_WithDelete_PassesValidation()
207+
{
208+
var settings = new SqlServerSinkSettings
209+
{
210+
ConnectionString = "Server=.;Database=Test;",
211+
TableName = "TestTable",
212+
WriteMode = SqlWriteMode.Upsert,
213+
PrimaryKeyColumns = new List<string> { "Id", "Name" },
214+
DeleteNotMatchedBySource = true,
215+
ColumnMappings = new List<ColumnMapping>
216+
{
217+
new ColumnMapping { ColumnName = "Id" },
218+
new ColumnMapping { ColumnName = "Name" }
219+
}
220+
};
221+
222+
var validationResults = settings.Validate(new ValidationContext(settings)).ToList();
223+
224+
Assert.IsFalse(validationResults.Any(v => v.MemberNames.Contains(nameof(SqlServerSinkSettings.ColumnMappings))),
225+
"Validation should pass when all columns are primary keys but DeleteNotMatchedBySource is true");
226+
}
227+
205228
[TestMethod]
206229
public void TestSinkSettings_DeleteNotMatchedBySource_DefaultsToFalse()
207230
{

Extensions/SqlServer/Cosmos.DataTransfer.SqlServerExtension/SqlServerSinkSettings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public IEnumerable<ValidationResult> Validate(ValidationContext validationContex
5353
}
5454
else
5555
{
56-
// Ensure at least one non-key column exists for updates
56+
// Ensure at least one non-key column exists for updates, unless we're only doing DELETE sync
5757
var allColumns = ColumnMappings.Select(m => m.ColumnName).ToList();
5858
var nonKeyColumns = allColumns.Except(PrimaryKeyColumns).ToList();
5959

60-
if (nonKeyColumns.Count == 0)
60+
if (nonKeyColumns.Count == 0 && !DeleteNotMatchedBySource)
6161
{
6262
results.Add(new ValidationResult(
63-
"At least one non-primary key column must be specified in ColumnMappings for Upsert mode.",
63+
"At least one non-primary key column must be specified in ColumnMappings for Upsert mode, or set DeleteNotMatchedBySource to true.",
6464
new[] { nameof(ColumnMappings) }));
6565
}
6666
}

0 commit comments

Comments
 (0)