mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 00:11:10 +00:00
Migrates persisted identifiers to GUID version 7.
This commit is contained in:
1 parent
fb92a38a7b
commit
8eba545b2b
80 files changed
+305
-129
No files matched your search
@@ -0,0 +1,29 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Microsoft.EntityFrameworkCore.ValueGeneration;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.Common.ValueGeneration;
|
||||
|
||||
/// <summary>
|
||||
/// Generates time ordered version 7 GUIDs for identifier properties whose values are assigned by the persistence layer.
|
||||
/// </summary>
|
||||
public sealed class GuidV7ValueGenerator : ValueGenerator<Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the generated values are temporary.
|
||||
/// </summary>
|
||||
/// <remarks>The generated identifiers are stored permanently, so they are not temporary values.</remarks>
|
||||
public override bool GeneratesTemporaryValues => false;
|
||||
|
||||
/// <summary>
|
||||
/// Generates the next time ordered identifier.
|
||||
/// </summary>
|
||||
/// <param name="entry">The tracked entity entry that owns the property.</param>
|
||||
/// <returns>A new version 7 GUID.</returns>
|
||||
public override Guid Next(EntityEntry entry)
|
||||
{
|
||||
return Guid.CreateVersion7();
|
||||
}
|
||||
}
|
||||
@@ -105,6 +105,8 @@ internal sealed class LibraryScanSnapshotRepository : ILibraryScanSnapshotReposi
|
||||
|
||||
// The Id of the inserted rows is generated in SQL as a random 16 byte value formatted as upper case hex (hex(randomblob(16))), which the storage layer parses as a Guid;
|
||||
// generating the Ids in code would defeat the purpose of these single statement bulk inserts.
|
||||
// These Ids are random version 4 values rather than the version 7 values used elsewhere, because SQLite cannot produce a version 7 GUID
|
||||
// inside this statement. See docs/technical/architecture/architecture-knowledge-management/architecture-decision-log/architecture-decision-record-0020.md for details.
|
||||
|
||||
// Add an audit entry for every media library scan snapshot item that is no longer present in the current scan, meaning it was deleted from the storage medium.
|
||||
const string AUDIT_DELETED_SQL = """
|
||||
|
||||
+1
-1
@@ -87,7 +87,7 @@ internal sealed class MediaContributorRepository : IMediaContributorRepository
|
||||
|
||||
MediaContributorEntity contributor = new()
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Id = Guid.CreateVersion7(),
|
||||
DisplayName = displayName,
|
||||
LegalName = legalName,
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
|
||||
@@ -167,7 +167,7 @@ internal sealed class MusicArtworkRepository : IMusicArtworkRepository
|
||||
{
|
||||
_luminaDbContext.MusicArtwork.Add(new MusicArtworkEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Id = Guid.CreateVersion7(),
|
||||
OwnerType = ownerType,
|
||||
OwnerId = ownerId,
|
||||
ArtworkType = ArtworkType.Cover,
|
||||
|
||||
@@ -517,7 +517,7 @@ internal sealed class PlaylistRepository : IPlaylistRepository
|
||||
if (index > 0)
|
||||
insertSql.Append(", ");
|
||||
insertSql.Append($"(@Id{index}, @PlaylistId{index}, @MediaItemId{index}, @mediaItemType, @Position{index}, @createdOnUtc, @createdBy)");
|
||||
Guid entryId = entryIds is null ? Guid.NewGuid() : entryIds[offset + index];
|
||||
Guid entryId = entryIds is null ? Guid.CreateVersion7() : entryIds[offset + index];
|
||||
parameters.Add($"Id{index}", ToStorageId(entryId));
|
||||
parameters.Add($"PlaylistId{index}", playlistIdText);
|
||||
parameters.Add($"MediaItemId{index}", ToStorageId(mediaItemIds[offset + index]));
|
||||
@@ -562,6 +562,8 @@ internal sealed class PlaylistRepository : IPlaylistRepository
|
||||
{
|
||||
// The entry Ids are generated by SQLite in the same upper case, hyphenated TEXT form the Entity Framework provider stores. Generating
|
||||
// them inside the statement is what lets the scope stay a single insert instead of being materialized in C# just to assign Ids.
|
||||
// They are random version 4 values rather than the version 7 values used elsewhere, because SQLite cannot produce a version 7 GUID
|
||||
// inside this statement. See docs/technical/architecture/architecture-knowledge-management/architecture-decision-log/architecture-decision-record-0020.md for details.
|
||||
// The positions are computed by the same ordering the artist and album pages use, so the playlist receives catalogue order directly.
|
||||
// ROW_NUMBER() produces the dense rank, and multiplying it by POSITION_STEP keeps the stored keys sparse.
|
||||
StringBuilder insertSql = new("""
|
||||
|
||||
@@ -229,7 +229,7 @@ public class DataSeedService : IDataSeedService
|
||||
{
|
||||
return new ScheduledJobEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
Id = Guid.CreateVersion7(),
|
||||
Name = name,
|
||||
TaskType = taskType,
|
||||
ScheduleType = scheduleType,
|
||||
|
||||
@@ -11,7 +11,10 @@ using Lumina.Application.Common.DataAccess.Entities.Plugins;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Scheduling;
|
||||
using Lumina.Application.Common.DataAccess.Entities.Themes;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.DataAccess.Common.ValueGeneration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.Core.UoW;
|
||||
@@ -210,6 +213,14 @@ public class LuminaDbContext : DbContext
|
||||
// Find all the entity configurations in the assembly and apply them.
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(LuminaDbContext).Assembly);
|
||||
|
||||
// The relational provider seeds its Guid value generators from Guid.NewGuid, so replace them with a time ordered generator for every
|
||||
// identifier that the persistence layer assigns itself. This covers real keys, such as PermissionEntity.Id, and the shadow keys of
|
||||
// owned collections, such as AlbumReleaseTypes.Id.
|
||||
foreach (IMutableEntityType entityType in modelBuilder.Model.GetEntityTypes())
|
||||
foreach (IMutableProperty property in entityType.GetProperties())
|
||||
if (property.ClrType == typeof(Guid) && property.ValueGenerated == ValueGenerated.OnAdd)
|
||||
property.SetValueGeneratorFactory((generatedProperty, declaringType) => new GuidV7ValueGenerator());
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user