db optimizations around names and case-sensitivity (#2749)
* generate (case-insensitive) unique names for fields that should be unique * move name case-insensitivity down to schema level * update changelog
This commit is contained in:
@@ -13,6 +13,10 @@ public class ChannelConfiguration : IEntityTypeConfiguration<Channel>
|
||||
builder.HasIndex(c => c.Number)
|
||||
.IsUnique();
|
||||
|
||||
builder.Property(c => c.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.Property(c => c.IsEnabled)
|
||||
.HasDefaultValue(true);
|
||||
|
||||
|
||||
@@ -6,5 +6,15 @@ namespace ErsatzTV.Infrastructure.Data.Configurations;
|
||||
|
||||
public class ChannelWatermarkConfiguration : IEntityTypeConfiguration<ChannelWatermark>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<ChannelWatermark> builder) => builder.ToTable("ChannelWatermark");
|
||||
public void Configure(EntityTypeBuilder<ChannelWatermark> builder)
|
||||
{
|
||||
builder.ToTable("ChannelWatermark");
|
||||
|
||||
builder.Property(wm => wm.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(wm => wm.Name)
|
||||
.IsUnique();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,13 @@ public class CollectionConfiguration : IEntityTypeConfiguration<Collection>
|
||||
{
|
||||
builder.ToTable("Collection");
|
||||
|
||||
builder.Property(c => c.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(c => c.Name)
|
||||
.IsUnique();
|
||||
|
||||
builder.HasMany(c => c.MediaItems)
|
||||
.WithMany(m => m.Collections)
|
||||
.UsingEntity<CollectionItem>(
|
||||
|
||||
@@ -10,6 +10,13 @@ public class MultiCollectionConfiguration : IEntityTypeConfiguration<MultiCollec
|
||||
{
|
||||
builder.ToTable("MultiCollection");
|
||||
|
||||
builder.Property(mc => mc.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(mc => mc.Name)
|
||||
.IsUnique();
|
||||
|
||||
builder.HasMany(m => m.Collections)
|
||||
.WithMany(m => m.MultiCollections)
|
||||
.UsingEntity<MultiCollectionItem>(
|
||||
|
||||
@@ -10,7 +10,11 @@ public class PlaylistConfiguration : IEntityTypeConfiguration<Playlist>
|
||||
{
|
||||
builder.ToTable("Playlist");
|
||||
|
||||
builder.HasIndex(d => new { d.PlaylistGroupId, d.Name })
|
||||
builder.Property(p => p.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(p => new { p.PlaylistGroupId, p.Name })
|
||||
.IsUnique();
|
||||
|
||||
builder.HasMany(p => p.Items)
|
||||
|
||||
@@ -10,6 +10,13 @@ public class RerunCollectionConfiguration : IEntityTypeConfiguration<RerunCollec
|
||||
{
|
||||
builder.ToTable("RerunCollection");
|
||||
|
||||
builder.Property(rc => rc.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(rc => rc.Name)
|
||||
.IsUnique();
|
||||
|
||||
builder.HasOne(i => i.Collection)
|
||||
.WithMany()
|
||||
.HasForeignKey(i => i.CollectionId)
|
||||
|
||||
@@ -10,6 +10,10 @@ public class SmartCollectionConfiguration : IEntityTypeConfiguration<SmartCollec
|
||||
{
|
||||
builder.ToTable("SmartCollection");
|
||||
|
||||
builder.Property(sc => sc.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(sc => sc.Name)
|
||||
.IsUnique();
|
||||
}
|
||||
|
||||
@@ -10,6 +10,8 @@ public class TraktListConfiguration : IEntityTypeConfiguration<TraktList>
|
||||
{
|
||||
builder.ToTable("TraktList");
|
||||
|
||||
builder.HasIndex(l => l.Name);
|
||||
|
||||
builder.HasMany(l => l.Items)
|
||||
.WithOne(i => i.TraktList)
|
||||
.HasForeignKey(i => i.TraktListId)
|
||||
|
||||
@@ -10,6 +10,13 @@ public class FillerPresetConfiguration : IEntityTypeConfiguration<FillerPreset>
|
||||
{
|
||||
builder.ToTable("FillerPreset");
|
||||
|
||||
builder.Property(mc => mc.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(c => c.Name)
|
||||
.IsUnique();
|
||||
|
||||
builder.HasOne(i => i.Collection)
|
||||
.WithMany()
|
||||
.HasForeignKey(i => i.CollectionId)
|
||||
|
||||
+11
-9
@@ -10,39 +10,41 @@ public class ArtistMetadataConfiguration : IEntityTypeConfiguration<ArtistMetada
|
||||
{
|
||||
builder.ToTable("ArtistMetadata");
|
||||
|
||||
builder.HasMany(sm => sm.Artwork)
|
||||
builder.HasIndex(am => am.Title);
|
||||
|
||||
builder.HasMany(am => am.Artwork)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Genres)
|
||||
builder.HasMany(am => am.Genres)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Tags)
|
||||
builder.HasMany(am => am.Tags)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Studios)
|
||||
builder.HasMany(am => am.Studios)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Actors)
|
||||
builder.HasMany(am => am.Actors)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(mm => mm.Guids)
|
||||
builder.HasMany(am => am.Guids)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(mm => mm.Subtitles)
|
||||
builder.HasMany(am => am.Subtitles)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Styles)
|
||||
builder.HasMany(am => am.Styles)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Moods)
|
||||
builder.HasMany(am => am.Moods)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
|
||||
@@ -6,5 +6,14 @@ namespace ErsatzTV.Infrastructure.Data.Configurations;
|
||||
|
||||
public class MetadataGuidConfiguration : IEntityTypeConfiguration<MetadataGuid>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<MetadataGuid> builder) => builder.ToTable("MetadataGuid");
|
||||
public void Configure(EntityTypeBuilder<MetadataGuid> builder)
|
||||
{
|
||||
builder.ToTable("MetadataGuid");
|
||||
|
||||
builder.Property(mg => mg.Guid)
|
||||
.HasMaxLength(128)
|
||||
.HasColumnType("varchar(128)");
|
||||
|
||||
builder.HasIndex(mg => mg.Guid);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,23 +10,25 @@ public class MovieMetadataConfiguration : IEntityTypeConfiguration<MovieMetadata
|
||||
{
|
||||
builder.ToTable("MovieMetadata");
|
||||
|
||||
builder.HasMany(sm => sm.Artwork)
|
||||
builder.HasIndex(mm => mm.Title);
|
||||
|
||||
builder.HasMany(mm => mm.Artwork)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Genres)
|
||||
builder.HasMany(mm => mm.Genres)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Tags)
|
||||
builder.HasMany(mm => mm.Tags)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Studios)
|
||||
builder.HasMany(mm => mm.Studios)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
builder.HasMany(sm => sm.Actors)
|
||||
builder.HasMany(mm => mm.Actors)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ public class ShowMetadataConfiguration : IEntityTypeConfiguration<ShowMetadata>
|
||||
{
|
||||
builder.ToTable("ShowMetadata");
|
||||
|
||||
builder.HasIndex(sm => sm.Title);
|
||||
|
||||
builder.HasMany(sm => sm.Artwork)
|
||||
.WithOne()
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
@@ -10,6 +10,10 @@ public class ProgramScheduleConfiguration : IEntityTypeConfiguration<ProgramSche
|
||||
{
|
||||
builder.ToTable("ProgramSchedule");
|
||||
|
||||
builder.Property(p => p.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(ps => ps.Name)
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ public class BlockConfiguration : IEntityTypeConfiguration<Block>
|
||||
{
|
||||
builder.ToTable("Block");
|
||||
|
||||
builder.Property(b => b.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(b => new { b.BlockGroupId, b.Name })
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ public class BlockGroupConfiguration : IEntityTypeConfiguration<BlockGroup>
|
||||
{
|
||||
builder.ToTable("BlockGroup");
|
||||
|
||||
builder.Property(b => b.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(b => b.Name)
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -11,6 +11,10 @@ public class DecoConfiguration : IEntityTypeConfiguration<Deco>
|
||||
{
|
||||
builder.ToTable("Deco");
|
||||
|
||||
builder.Property(d => d.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(d => new { d.DecoGroupId, d.Name })
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ public class DecoGroupConfiguration : IEntityTypeConfiguration<DecoGroup>
|
||||
{
|
||||
builder.ToTable("DecoGroup");
|
||||
|
||||
builder.Property(dg => dg.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(dg => dg.Name)
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ public class DecoTemplateConfiguration : IEntityTypeConfiguration<DecoTemplate>
|
||||
{
|
||||
builder.ToTable("DecoTemplate");
|
||||
|
||||
builder.Property(d => d.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(d => new { d.DecoTemplateGroupId, d.Name })
|
||||
.IsUnique();
|
||||
|
||||
|
||||
+4
@@ -10,6 +10,10 @@ public class DecoTemplateGroupConfiguration : IEntityTypeConfiguration<DecoTempl
|
||||
{
|
||||
builder.ToTable("DecoTemplateGroup");
|
||||
|
||||
builder.Property(d => d.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(b => b.Name)
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ public class TemplateConfiguration : IEntityTypeConfiguration<Template>
|
||||
{
|
||||
builder.ToTable("Template");
|
||||
|
||||
builder.Property(t => t.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(d => new { d.TemplateGroupId, d.Name })
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -10,6 +10,10 @@ public class TemplateGroupConfiguration : IEntityTypeConfiguration<TemplateGroup
|
||||
{
|
||||
builder.ToTable("TemplateGroup");
|
||||
|
||||
builder.Property(t => t.Name)
|
||||
.HasMaxLength(50)
|
||||
.HasColumnType("varchar(50)");
|
||||
|
||||
builder.HasIndex(b => b.Name)
|
||||
.IsUnique();
|
||||
|
||||
|
||||
@@ -51,9 +51,7 @@ public class ChannelRepository(IDbContextFactory<TvContext> dbContextFactory) :
|
||||
|
||||
List<ChannelWatermark> maybeWatermarks = await dbContext.ChannelWatermarks
|
||||
.AsNoTracking()
|
||||
.Where(cw => EF.Functions.Like(
|
||||
EF.Functions.Collate(cw.Name, TvContext.CaseInsensitiveCollation),
|
||||
$"%{name}%"))
|
||||
.Where(cw => EF.Functions.Like(cw.Name, $"%{name}%"))
|
||||
.ToListAsync();
|
||||
|
||||
return maybeWatermarks.HeadOrNone();
|
||||
|
||||
@@ -176,10 +176,7 @@ public class MediaCollectionRepository : IMediaCollectionRepository
|
||||
|
||||
Option<Playlist> maybePlaylist = await dbContext.Playlists
|
||||
.AsNoTracking()
|
||||
.SelectOneAsync(
|
||||
p => p.Name,
|
||||
p => EF.Functions.Collate(p.Name, TvContext.CaseInsensitiveCollation) == name,
|
||||
cancellationToken);
|
||||
.SingleOrDefaultAsync(p => p.Name == name, cancellationToken);
|
||||
|
||||
foreach (Playlist playlist in maybePlaylist)
|
||||
{
|
||||
@@ -360,10 +357,7 @@ public class MediaCollectionRepository : IMediaCollectionRepository
|
||||
|
||||
Option<Collection> maybeCollection = await dbContext.Collections
|
||||
.AsNoTracking()
|
||||
.SelectOneAsync(
|
||||
c => c.Name,
|
||||
c => EF.Functions.Collate(c.Name, TvContext.CaseInsensitiveCollation) == name,
|
||||
cancellationToken);
|
||||
.SingleOrDefaultAsync(c => c.Name == name, cancellationToken);
|
||||
|
||||
foreach (Collection collection in maybeCollection)
|
||||
{
|
||||
@@ -416,10 +410,7 @@ public class MediaCollectionRepository : IMediaCollectionRepository
|
||||
|
||||
Option<MultiCollection> maybeCollection = await dbContext.MultiCollections
|
||||
.AsNoTracking()
|
||||
.SelectOneAsync(
|
||||
mc => mc.Name,
|
||||
mc => EF.Functions.Collate(mc.Name, TvContext.CaseInsensitiveCollation) == name,
|
||||
cancellationToken);
|
||||
.SingleOrDefaultAsync(mc => mc.Name == name, cancellationToken);
|
||||
|
||||
foreach (MultiCollection collection in maybeCollection)
|
||||
{
|
||||
@@ -451,10 +442,7 @@ public class MediaCollectionRepository : IMediaCollectionRepository
|
||||
|
||||
Option<SmartCollection> maybeCollection = await dbContext.SmartCollections
|
||||
.AsNoTracking()
|
||||
.SelectOneAsync(
|
||||
sc => sc.Name,
|
||||
sc => EF.Functions.Collate(sc.Name, TvContext.CaseInsensitiveCollation) == name,
|
||||
cancellationToken);
|
||||
.SingleOrDefaultAsync(sc => sc.Name == name, cancellationToken);
|
||||
|
||||
foreach (SmartCollection collection in maybeCollection)
|
||||
{
|
||||
@@ -694,8 +682,7 @@ public class MediaCollectionRepository : IMediaCollectionRepository
|
||||
|
||||
List<int> nextIds = await dbContext.ShowMetadata
|
||||
.AsNoTracking()
|
||||
.Filter(sm =>
|
||||
sm.Guids.Any(g => EF.Functions.Collate(g.Guid, TvContext.CaseInsensitiveCollation) == guid))
|
||||
.Filter(sm => sm.Guids.Any(g => g.Guid == guid))
|
||||
.Map(sm => sm.ShowId)
|
||||
.ToListAsync();
|
||||
|
||||
|
||||
@@ -86,9 +86,7 @@ public class TelevisionRepository : ITelevisionRepository
|
||||
return await dbContext.ShowMetadata
|
||||
.AsNoTracking()
|
||||
.Where(sm => sm.Show.LibraryPath.LibraryId == libraryId)
|
||||
.Where(sm => EF.Functions.Like(
|
||||
EF.Functions.Collate(sm.Title, TvContext.CaseInsensitiveCollation),
|
||||
$"%{title}%"))
|
||||
.Where(sm => EF.Functions.Like(sm.Title, $"%{title}%"))
|
||||
.Map(sm => sm.ShowId)
|
||||
.FirstOrDefaultAsync()
|
||||
.Map(showId => showId > 0 ? Option<int>.Some(showId) : Option<int>.None);
|
||||
@@ -223,9 +221,8 @@ public class TelevisionRepository : ITelevisionRepository
|
||||
if (maybeId.IsNone)
|
||||
{
|
||||
List<int> maybeShowIds = await dbContext.Episodes
|
||||
.Where(e => e.MediaVersions.Any(mv => mv.MediaFiles.Any(mf => EF.Functions.Like(
|
||||
EF.Functions.Collate(mf.Path, TvContext.CaseInsensitiveCollation),
|
||||
$"{showFolder}%"))))
|
||||
.Where(e => e.MediaVersions.Any(mv =>
|
||||
mv.MediaFiles.Any(mf => EF.Functions.Like(mf.Path, $"{showFolder}%"))))
|
||||
.Map(e => e.Season.ShowId)
|
||||
.Distinct()
|
||||
.ToListAsync();
|
||||
|
||||
@@ -136,18 +136,46 @@ public class TvContext : DbContext
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
string collation = null;
|
||||
|
||||
// mysql-specific configuration
|
||||
if ((Database.ProviderName ?? string.Empty).Contains("MySql", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
modelBuilder.Entity<MediaFile>().Property(mf => mf.Path).HasColumnType("longtext");
|
||||
|
||||
modelBuilder.Entity<SmartCollection>().Property(mc => mc.Name).UseCollation("utf8mb4_general_ci");
|
||||
collation = "utf8mb4_general_ci";
|
||||
}
|
||||
|
||||
// sqlite-specific configuration
|
||||
if ((Database.ProviderName ?? string.Empty).Contains("Sqlite", StringComparison.InvariantCultureIgnoreCase))
|
||||
{
|
||||
modelBuilder.Entity<SmartCollection>().Property(sc => sc.Name).UseCollation("NOCASE");
|
||||
collation = "NOCASE";
|
||||
}
|
||||
|
||||
// case-insensitive columns
|
||||
if (!string.IsNullOrEmpty(collation))
|
||||
{
|
||||
modelBuilder.Entity<ArtistMetadata>().Property(b => b.Title).UseCollation(collation);
|
||||
modelBuilder.Entity<Block>().Property(b => b.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<BlockGroup>().Property(b => b.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<Channel>().Property(c => c.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<ChannelWatermark>().Property(c => c.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<Collection>().Property(c => c.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<Deco>().Property(d => d.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<DecoGroup>().Property(d => d.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<DecoTemplate>().Property(d => d.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<DecoTemplateGroup>().Property(d => d.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<FillerPreset>().Property(fp => fp.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<MetadataGuid>().Property(mg => mg.Guid).UseCollation(collation);
|
||||
modelBuilder.Entity<MovieMetadata>().Property(mm => mm.Title).UseCollation(collation);
|
||||
modelBuilder.Entity<MultiCollection>().Property(mc => mc.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<Playlist>().Property(p => p.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<ProgramSchedule>().Property(ps => ps.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<RerunCollection>().Property(rc => rc.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<ShowMetadata>().Property(sm => sm.Title).UseCollation(collation);
|
||||
modelBuilder.Entity<SmartCollection>().Property(sc => sc.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<Template>().Property(t => t.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<TemplateGroup>().Property(t => t.Name).UseCollation(collation);
|
||||
modelBuilder.Entity<TraktList>().Property(t => t.Name).UseCollation(collation);
|
||||
}
|
||||
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(TvContext).Assembly);
|
||||
|
||||
Reference in New Issue
Block a user