diff --git a/src/SIL.Harmony/Db/EntityConfig/ChangeEntityConfig.cs b/src/SIL.Harmony/Db/EntityConfig/ChangeEntityConfig.cs index 306c854..fa3197c 100644 --- a/src/SIL.Harmony/Db/EntityConfig/ChangeEntityConfig.cs +++ b/src/SIL.Harmony/Db/EntityConfig/ChangeEntityConfig.cs @@ -6,23 +6,45 @@ namespace SIL.Harmony.Db.EntityConfig; -public class ChangeEntityConfig(JsonSerializerOptions jsonSerializerOptions) : IEntityTypeConfiguration> +public class ChangeEntityConfig(JsonSerializerOptions jsonSerializerOptions) + : IEntityTypeConfiguration> { + public ChangeEntityConfig() : this(JsonSerializerOptions.Default) + { + } + public void Configure(EntityTypeBuilder> builder) { builder.ToTable("ChangeEntities"); builder.HasKey(c => new { c.CommitId, c.Index }); - builder.Property(c => c.Change) - .HasColumnType("jsonb") - .HasConversion( - change => JsonSerializer.Serialize(change, jsonSerializerOptions), - json => DeserializeChange(json) - ); + var changeProperty = builder.Property(c => c.Change) + .HasColumnType("jsonb"); + if (EF.IsDesignTime) + { + changeProperty + .HasConversion( + change => SerializeChange(change, null), + json => DeserializeChange(json, null) + ); + } + else + { + changeProperty + .HasConversion( + change => SerializeChange(change, jsonSerializerOptions), + json => DeserializeChange(json, jsonSerializerOptions) + ); + } } - private IChange DeserializeChange(string json) + public static IChange DeserializeChange(string json, JsonSerializerOptions? jsonSerializerOptions) { return JsonSerializer.Deserialize(json, jsonSerializerOptions) ?? throw new SerializationException("Could not deserialize Change: " + json); } + + public static string SerializeChange(IChange change, JsonSerializerOptions? jsonSerializerOptions) + { + return JsonSerializer.Serialize(change, jsonSerializerOptions); + } } diff --git a/src/SIL.Harmony/Db/EntityConfig/CommitEntityConfig.cs b/src/SIL.Harmony/Db/EntityConfig/CommitEntityConfig.cs index 9694bb5..f68002a 100644 --- a/src/SIL.Harmony/Db/EntityConfig/CommitEntityConfig.cs +++ b/src/SIL.Harmony/Db/EntityConfig/CommitEntityConfig.cs @@ -1,6 +1,8 @@ +using System.Runtime.Serialization; using System.Text.Json; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; +using SIL.Harmony.Changes; namespace SIL.Harmony.Db.EntityConfig; @@ -24,11 +26,22 @@ public void Configure(EntityTypeBuilder builder) builder.Property(c => c.Metadata) .HasColumnType("jsonb") .HasConversion( - m => JsonSerializer.Serialize(m, (JsonSerializerOptions?)null), - json => JsonSerializer.Deserialize(json, (JsonSerializerOptions?)null) ?? new() + m => Serialize(m, null), + json => Deserialize(json, null) ?? new() ); builder.HasMany(c => c.ChangeEntities) .WithOne() .HasForeignKey(c => c.CommitId); } + + public static CommitMetadata Deserialize(string json, JsonSerializerOptions? jsonSerializerOptions) + { + return JsonSerializer.Deserialize(json, jsonSerializerOptions) ?? + throw new SerializationException("Could not deserialize CommitMetadata: " + json); + } + + public static string Serialize(CommitMetadata commitMetadata, JsonSerializerOptions? jsonSerializerOptions) + { + return JsonSerializer.Serialize(commitMetadata, jsonSerializerOptions); + } } diff --git a/src/SIL.Harmony/Db/EntityConfig/SnapshotEntityConfig.cs b/src/SIL.Harmony/Db/EntityConfig/SnapshotEntityConfig.cs index 8febc27..08db942 100644 --- a/src/SIL.Harmony/Db/EntityConfig/SnapshotEntityConfig.cs +++ b/src/SIL.Harmony/Db/EntityConfig/SnapshotEntityConfig.cs @@ -17,17 +17,32 @@ public void Configure(EntityTypeBuilder builder) .HasOne(s => s.Commit) .WithMany(c => c.Snapshots) .HasForeignKey(s => s.CommitId); - builder.Property(s => s.Entity) - .HasColumnType("jsonb") - .HasConversion( - entry => JsonSerializer.Serialize(entry, jsonSerializerOptions), - json => DeserializeObject(json) + var entityProperty = builder.Property(s => s.Entity) + .HasColumnType("jsonb"); + if (EF.IsDesignTime) + { + entityProperty.HasConversion( + entry => Serialize(entry, null), + json => DeserializeObject(json, null) ); + } + else + { + entityProperty.HasConversion( + entry => Serialize(entry, jsonSerializerOptions), + json => DeserializeObject(json, jsonSerializerOptions) + ); + } } - private IObjectBase DeserializeObject(string json) + public static IObjectBase DeserializeObject(string json, JsonSerializerOptions? jsonSerializerOptions) { return JsonSerializer.Deserialize(json, jsonSerializerOptions) ?? throw new SerializationException($"Could not deserialize Entry: {json}"); } + + public static string Serialize(IObjectBase change, JsonSerializerOptions? jsonSerializerOptions) + { + return JsonSerializer.Serialize(change, jsonSerializerOptions); + } }