Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions src/CoreSync.Http.Client/Implementation/SyncProviderHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,17 @@ private static void ConvertJsonValueToNetObject(SyncChangeSet changeSet)
{
foreach (var itemValueEntry in item.Values.Where(_ => _.Key != "__OP").ToList())
{
item.Values[itemValueEntry.Key].Value = itemValueEntry.Value.Value == null ? null :
ConvertJsonValueToNetObject((JsonElement)itemValueEntry.Value.Value, itemValueEntry.Value.Type);

//WARN: can't convert every Id to lowercase because sqlite is case sensitive!
//if (itemValueEntry.Value.Value != null &&
// itemValueEntry.Value.Type == SyncItemValueType.String &&
// (itemValueEntry.Key == "Id" || itemValueEntry.Key.EndsWith("Id")))
//{
// item.Values[itemValueEntry.Key].Value = itemValueEntry.Value.Value.ToString().ToLowerInvariant();
//}
var rawValue = itemValueEntry.Value.Value;
if (rawValue == null)
{
item.Values[itemValueEntry.Key].Value = null;
}
else if (rawValue is JsonElement jsonElement)
{
item.Values[itemValueEntry.Key].Value =
ConvertJsonValueToNetObject(jsonElement, itemValueEntry.Value.Type);
}
// else: value is already a native .NET type — no conversion needed.
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions src/CoreSync.Http.Server/SyncAgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,18 @@ public async Task<SyncAnchor> CompleteApplyBulkChangesAsync(Guid sessionId)
{
foreach (var itemValueEntry in item.Values.Where(_ => _.Key != "__OP").ToList())
{
item.Values[itemValueEntry.Key].Value = itemValueEntry.Value.Value == null ? null :
ConvertJsonElementToObject((JsonElement)itemValueEntry.Value.Value, itemValueEntry.Value.Type);
var rawValue = itemValueEntry.Value.Value;
if (rawValue == null)
{
item.Values[itemValueEntry.Key].Value = null;
}
else if (rawValue is JsonElement jsonElement)
{
item.Values[itemValueEntry.Key].Value =
ConvertJsonElementToObject(jsonElement, itemValueEntry.Value.Type);
}
// else: value is already a native .NET type (e.g. from MessagePack deserialization
// or newer System.Text.Json versions) — no conversion needed.
}
}

Expand Down
9 changes: 9 additions & 0 deletions src/CoreSync.PostgreSQL/PostgreSQLSyncProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,18 @@ public async Task<SyncAnchor> ApplyChangesAsync([NotNull] SyncChangeSet changeSe

try
{
// Use a savepoint so that a failed statement does not abort
// the entire PostgreSQL transaction (error state 25P02).
await tr.SaveAsync("sync_item", cancellationToken);

affectedRows = await cmd.ExecuteNonQueryAsync(cancellationToken);

if (affectedRows > 0)
{
_logger?.Trace($"[{_storeId}] Successfully applied {item}");
}

await tr.ReleaseAsync("sync_item", cancellationToken);
}
catch (OperationCanceledException)
{
Expand All @@ -98,6 +104,9 @@ public async Task<SyncAnchor> ApplyChangesAsync([NotNull] SyncChangeSet changeSe
catch (Exception ex)
{
_logger?.Error($"Unable to {itemChangeType} item {item} to store for table {table}.{Environment.NewLine}{ex}{Environment.NewLine}Generated SQL:{Environment.NewLine}{cmd.CommandText}");
// Rollback to the savepoint so the transaction can continue
// processing remaining items.
try { await tr.RollbackAsync("sync_item", cancellationToken); } catch { /* already rolled back */ }
}

if (affectedRows == 0)
Expand Down