Skip to content

Commit 58d43bb

Browse files
authored
Improve merge error logging; disable sync deletes (WHEN NOT MATCHED BY SOURCE) (#119)
- Wrap MergeData in try-catch and log scope, statement, and exception before rethrow - Omit WHEN NOT MATCHED BY SOURCE THEN DELETE from merge so target rows are not deleted when missing in source
1 parent e687079 commit 58d43bb

2 files changed

Lines changed: 18 additions & 9 deletions

File tree

src/SqlBulkSyncFunction/Helpers/SqlCommandExtensions.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,19 @@ public static void MergeData(
6464
ILogger logger
6565
)
6666
{
67-
var rowCount = targetConn.Query<long>(
68-
commandTimeout: 500000,
69-
sql: tableSchema.MergeNewOrUpdateStatement
70-
).First();
71-
logger.LogInformation("{Scope} {RowCount} records merged", scope, rowCount);
67+
try
68+
{
69+
var rowCount = targetConn.Query<long>(
70+
commandTimeout: 500000,
71+
sql: tableSchema.MergeNewOrUpdateStatement
72+
).First();
73+
logger.LogInformation("{Scope} {RowCount} records merged", scope, rowCount);
74+
}
75+
catch(Exception ex)
76+
{
77+
logger.LogError(ex, "Merge failed for {Scope} with statement {MergeNewOrUpdateStatement}\r\n{Exception}", scope, tableSchema.MergeNewOrUpdateStatement, ex.Message);
78+
throw;
79+
}
7280
}
7381

7482
public static void DeleteData(

src/SqlBulkSyncFunction/Helpers/SqlStatementExtensions.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,12 @@ THEN UPDATE
121121
),
122122
(
123123
(tableSchema.TargetVersion.CurrentVersion < 0)
124-
? """
124+
? string.Empty
125+
//"""
125126

126-
WHEN NOT MATCHED BY SOURCE
127-
THEN DELETE
128-
"""
127+
//WHEN NOT MATCHED BY SOURCE
128+
// THEN DELETE
129+
//"""
129130
: string.Empty
130131
),
131132
(

0 commit comments

Comments
 (0)