Skip to content

Commit 7cae58b

Browse files
committed
Backup object when making a change so if an error occurs on serialization it is not wiped
1 parent 0c69cee commit 7cae58b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Figment.Data.Local/LocalDirectorySchemaStorageProvider.cs

+12
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,32 @@ public async Task<bool> SaveAsync(Schema schema, CancellationToken cancellationT
256256

257257
var fileName = $"{schema.Guid}.schema.json";
258258
var filePath = Path.Combine(SchemaDirectoryPath, fileName);
259+
var backupFileName = $"{schema.Guid}.schema.json.backup";
259260

260261
// Convert schema to definition file for serialization in JSON Schema format
261262
var schemaDefinition = new JsonSchemaDefinition(schema);
262263

264+
if (File.Exists(filePath))
265+
File.Move(filePath, backupFileName, true);
266+
263267
using var fs = File.Create(filePath);
264268
try
265269
{
266270
await JsonSerializer.SerializeAsync(fs, schemaDefinition, jsonSerializerOptions, cancellationToken);
267271
await fs.FlushAsync(cancellationToken);
272+
273+
if (File.Exists(backupFileName))
274+
File.Delete(backupFileName);
275+
268276
return true;
269277
}
270278
catch (Exception je)
271279
{
272280
AmbientErrorContext.Provider.LogException(je, $"Unable to serialize schema {schema.Guid} from {filePath}");
281+
282+
if (File.Exists(backupFileName))
283+
File.Move(backupFileName, fileName);
284+
273285
return false;
274286
}
275287
}

Figment.Data.Local/LocalDirectoryThingStorageProvider.cs

+14-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,8 @@ public Task<bool> GuidExists(string thingGuid, CancellationToken _)
245245
return null;
246246
}
247247

248-
var thingLoaded = new Thing("", ""){
248+
var thingLoaded = new Thing("", "")
249+
{
249250
CreatedOn = fileInfo.CreationTimeUtc,
250251
LastModified = fileInfo.LastWriteTimeUtc,
251252
LastAccessed = fileInfo.LastAccessTimeUtc
@@ -403,17 +404,29 @@ public async Task<bool> SaveAsync(Thing thing, CancellationToken cancellationTok
403404

404405
var fileName = $"{thing.Guid}.thing.json";
405406
var filePath = Path.Combine(thingDir.FullName, fileName);
407+
var backupFileName = $"{thing.Guid}.thing.json.backup";
408+
409+
if (File.Exists(filePath))
410+
File.Move(filePath, backupFileName, true);
406411

407412
using var fs = File.Create(filePath);
408413
try
409414
{
410415
await JsonSerializer.SerializeAsync(fs, thing, jsonSerializerOptions, cancellationToken: cancellationToken);
411416
await fs.FlushAsync(cancellationToken);
417+
418+
if (File.Exists(backupFileName))
419+
File.Delete(backupFileName);
420+
412421
return true;
413422
}
414423
catch (Exception je)
415424
{
416425
AmbientErrorContext.Provider.LogException(je, $"Unable to serialize thing {thing.Guid} from {filePath}");
426+
427+
if (File.Exists(backupFileName))
428+
File.Move(backupFileName, fileName);
429+
417430
return false;
418431
}
419432
}

0 commit comments

Comments
 (0)