Skip to content

Commit

Permalink
Implement DB and EF changes for new static attribute Guids #3196
Browse files Browse the repository at this point in the history
  • Loading branch information
tvatavuk committed Oct 24, 2023
1 parent b43eb64 commit b68a743
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
8 changes: 8 additions & 0 deletions ToSic.Eav.Core/Serialization/IDataDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,13 @@ public interface IDataDeserializer: IHasLog
/// </param>
/// <returns>A list of entity objects</returns>
List<IEntity> Deserialize(List<string> serialized, bool allowDynamic = false);


/// <summary>
/// De-serialize ContentTypeAttributeSysSettings from SysSettings string field in ToSicEavAttributes and ToSicEavAttributeSets (EF/DB)
/// </summary>
/// <param name="serialized"></param>
/// <returns>ContentTypeAttributeSysSettings or null</returns>
ContentTypeAttributeSysSettings DeserializeAttributeSysSettings(string serialized);
}
}
17 changes: 17 additions & 0 deletions ToSic.Eav.ImportExport/Json/JsonDeserializer_ContentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using ToSic.Eav.Data;
using ToSic.Eav.Data.Source;
using ToSic.Eav.ImportExport.Json.V1;
using ToSic.Eav.Plumbing;
using ToSic.Eav.Serialization;
using ToSic.Lib.Logging;
using IEntity = ToSic.Eav.Data.IEntity;

Expand Down Expand Up @@ -100,5 +102,20 @@ IEntity ConvertPart(JsonEntity e) =>
});
return lMain.ReturnAsOk(contentType);
}

public ContentTypeAttributeSysSettings DeserializeAttributeSysSettings(string serialized)
{
var l = Log.Fn<ContentTypeAttributeSysSettings>($"{serialized?.Substring(0, Math.Min(50, serialized.Length))}...");
if (serialized.IsEmpty()) return l.Return(null, "empty serialized");
try
{
var json = System.Text.Json.JsonSerializer.Deserialize<JsonAttributeSysSettings>(serialized, JsonOptions.UnsafeJsonWithoutEncodingHtml);
return l.Return(json.ToSysSettings(), $"deserialized sysSettings");
}
catch (Exception e)
{
throw l.Done(e);
}
}
}
}
13 changes: 10 additions & 3 deletions ToSic.Eav.Persistence.Efc/Efc11Loader_ContentTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using Microsoft.EntityFrameworkCore;
using ToSic.Eav.Apps;
using ToSic.Eav.Data;
using ToSic.Lib.Logging;
using ToSic.Eav.Metadata;
using ToSic.Eav.Serialization;

namespace ToSic.Eav.Persistence.Efc
{
Expand Down Expand Up @@ -90,6 +92,9 @@ private ImmutableList<IContentType> LoadContentTypesIntoLocalCache(int appId, IH
var query = _dbContext.ToSicEavAttributeSets
.Where(set => set.AppId == appId && set.ChangeLogDeleted == null);

var serializer = _dataDeserializer.New();
serializer.Initialize(appId, new List<IContentType>(), null);

var contentTypes = query
.Include(set => set.ToSicEavAttributesInSets)
.ThenInclude(attrs => attrs.Attribute)
Expand All @@ -112,7 +117,9 @@ private ImmutableList<IContentType> LoadContentTypesIntoLocalCache(int appId, IH
isTitle: a.IsTitle,
id: a.AttributeId,
sortOrder: a.SortOrder,
metaSourceFinder: () => source)),
metaSourceFinder: () => source,
guid: a.Attribute.Guid,
sysSettings: serializer.DeserializeAttributeSysSettings(a.Attribute.SysSettings))),
IsGhost = set.UsesConfigurationOfAttributeSet,
SharedDefinitionId = set.UsesConfigurationOfAttributeSet,
AppId = set.UsesConfigurationOfAttributeSetNavigation?.AppId ?? set.AppId,
Expand Down Expand Up @@ -142,7 +149,8 @@ private ImmutableList<IContentType> LoadContentTypesIntoLocalCache(int appId, IH
id: a.AttributeId,
sortOrder: a.SortOrder,
// Must get own MetaSourceFinder since they come from other apps
metaSourceFinder: () => _appStates.Get(s.AppId)))
metaSourceFinder: () => _appStates.Get(s.AppId),
sysSettings: serializer.DeserializeAttributeSysSettings(a.Attribute.SysSettings)))
);
sqlTime.Stop();

Expand Down Expand Up @@ -177,6 +185,5 @@ private ImmutableList<IContentType> LoadContentTypesIntoLocalCache(int appId, IH

return wrapLog.Return(newTypes.ToImmutableList());
}

}
}
11 changes: 11 additions & 0 deletions ToSic.Eav.Persistence.Efc/Models/EavDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey(d => d.UsesConfigurationOfAttributeSet)
.HasConstraintName("FK_ToSIC_EAV_AttributeSets_ToSIC_EAV_AttributeSets");

entity.Property(e => e.SysSettings)
.HasColumnName("SysSettings")
.HasColumnType("nvarchar(MAX)");
});

modelBuilder.Entity<ToSicEavAttributeTypes>(entity =>
Expand Down Expand Up @@ -212,6 +215,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasForeignKey(d => d.Type)
.OnDelete(DeleteBehavior.Restrict)
.HasConstraintName("FK_ToSIC_EAV_Attributes_ToSIC_EAV_Types");

entity.Property(e => e.Guid)
.HasColumnName("Guid")
.HasColumnType("uniqueidentifier");

entity.Property(e => e.SysSettings)
.HasColumnName("SysSettings")
.HasColumnType("nvarchar(MAX)");
});

modelBuilder.Entity<ToSicEavAttributesInSets>(entity =>
Expand Down
1 change: 1 addition & 0 deletions ToSic.Eav.Persistence.Efc/Models/ToSicEavAttributeSets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public ToSicEavAttributeSets()
public int AppId { get; set; }
public int? UsesConfigurationOfAttributeSet { get; set; }
public bool AlwaysShareConfiguration { get; set; }
public string SysSettings { get; set; }

public virtual ICollection<ToSicEavAttributeGroups> ToSicEavAttributeGroups { get; set; }
public virtual ICollection<ToSicEavAttributesInSets> ToSicEavAttributesInSets { get; set; }
Expand Down
5 changes: 4 additions & 1 deletion ToSic.Eav.Persistence.Efc/Models/ToSicEavAttributes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace ToSic.Eav.Persistence.Efc.Models
{
Expand All @@ -16,6 +17,8 @@ public ToSicEavAttributes()
public string Type { get; set; }
public int ChangeLogCreated { get; set; }
public int? ChangeLogDeleted { get; set; }
public Guid? Guid { get; set; }
public string SysSettings { get; set; }

public virtual ICollection<ToSicEavAttributesInSets> ToSicEavAttributesInSets { get; set; }
public virtual ICollection<ToSicEavEntityRelationships> ToSicEavEntityRelationships { get; set; }
Expand Down

0 comments on commit b68a743

Please sign in to comment.