From 921618368b389bb90a4f9bd1e87fcb31e88ce4f3 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 10:05:36 -0500 Subject: [PATCH 1/8] Remove unused schema --- Jellyfin.Server.Implementations/JellyfinDb.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Jellyfin.Server.Implementations/JellyfinDb.cs b/Jellyfin.Server.Implementations/JellyfinDb.cs index dc4f53913c..e34e12e3fe 100644 --- a/Jellyfin.Server.Implementations/JellyfinDb.cs +++ b/Jellyfin.Server.Implementations/JellyfinDb.cs @@ -153,7 +153,6 @@ namespace Jellyfin.Server.Implementations { modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc); base.OnModelCreating(modelBuilder); - modelBuilder.HasDefaultSchema("jellyfin"); // Configuration for each entity is in it's own class inside 'ModelConfiguration'. modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDb).Assembly); From 2a86723caf271be73dca39c6dd3b5f11044eac28 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 10:06:25 -0500 Subject: [PATCH 2/8] Use file-scoped namespace in db context --- Jellyfin.Server.Implementations/JellyfinDb.cs | 283 +++++++++--------- 1 file changed, 141 insertions(+), 142 deletions(-) diff --git a/Jellyfin.Server.Implementations/JellyfinDb.cs b/Jellyfin.Server.Implementations/JellyfinDb.cs index e34e12e3fe..6443100806 100644 --- a/Jellyfin.Server.Implementations/JellyfinDb.cs +++ b/Jellyfin.Server.Implementations/JellyfinDb.cs @@ -8,154 +8,153 @@ using Jellyfin.Data.Entities.Security; using Jellyfin.Data.Interfaces; using Microsoft.EntityFrameworkCore; -namespace Jellyfin.Server.Implementations +namespace Jellyfin.Server.Implementations; + +/// +public class JellyfinDb : DbContext { - /// - public class JellyfinDb : DbContext + /// + /// Initializes a new instance of the class. + /// + /// The database context options. + public JellyfinDb(DbContextOptions options) : base(options) { - /// - /// Initializes a new instance of the class. - /// - /// The database context options. - public JellyfinDb(DbContextOptions options) : base(options) + } + + /// + /// Gets or sets the default connection string. + /// + public static string ConnectionString { get; set; } = @"Data Source=jellyfin.db"; + + public virtual DbSet AccessSchedules { get; set; } + + public virtual DbSet ActivityLogs { get; set; } + + public virtual DbSet ApiKeys { get; set; } + + public virtual DbSet Devices { get; set; } + + public virtual DbSet DeviceOptions { get; set; } + + public virtual DbSet DisplayPreferences { get; set; } + + public virtual DbSet ImageInfos { get; set; } + + public virtual DbSet ItemDisplayPreferences { get; set; } + + public virtual DbSet CustomItemDisplayPreferences { get; set; } + + public virtual DbSet Permissions { get; set; } + + public virtual DbSet Preferences { get; set; } + + public virtual DbSet Users { get; set; } + + /*public virtual DbSet Artwork { get; set; } + + public virtual DbSet Books { get; set; } + + public virtual DbSet BookMetadata { get; set; } + + public virtual DbSet Chapters { get; set; } + + public virtual DbSet Collections { get; set; } + + public virtual DbSet CollectionItems { get; set; } + + public virtual DbSet Companies { get; set; } + + public virtual DbSet CompanyMetadata { get; set; } + + public virtual DbSet CustomItems { get; set; } + + public virtual DbSet CustomItemMetadata { get; set; } + + public virtual DbSet Episodes { get; set; } + + public virtual DbSet EpisodeMetadata { get; set; } + + public virtual DbSet Genres { get; set; } + + public virtual DbSet Groups { get; set; } + + public virtual DbSet Libraries { get; set; } + + public virtual DbSet LibraryItems { get; set; } + + public virtual DbSet LibraryRoot { get; set; } + + public virtual DbSet MediaFiles { get; set; } + + public virtual DbSet MediaFileStream { get; set; } + + public virtual DbSet Metadata { get; set; } + + public virtual DbSet MetadataProviders { get; set; } + + public virtual DbSet MetadataProviderIds { get; set; } + + public virtual DbSet Movies { get; set; } + + public virtual DbSet MovieMetadata { get; set; } + + public virtual DbSet MusicAlbums { get; set; } + + public virtual DbSet MusicAlbumMetadata { get; set; } + + public virtual DbSet People { get; set; } + + public virtual DbSet PersonRoles { get; set; } + + public virtual DbSet Photo { get; set; } + + public virtual DbSet PhotoMetadata { get; set; } + + public virtual DbSet ProviderMappings { get; set; } + + public virtual DbSet Ratings { get; set; } + + /// + /// Repository for global::Jellyfin.Data.Entities.RatingSource - This is the entity to + /// store review ratings, not age ratings. + /// + public virtual DbSet RatingSources { get; set; } + + public virtual DbSet Releases { get; set; } + + public virtual DbSet Seasons { get; set; } + + public virtual DbSet SeasonMetadata { get; set; } + + public virtual DbSet Series { get; set; } + + public virtual DbSet SeriesMetadata { get; set; } + + public virtual DbSet Tracks { get; set; } + + public virtual DbSet TrackMetadata { get; set; }*/ + + /// + public override int SaveChanges() + { + foreach (var saveEntity in ChangeTracker.Entries() + .Where(e => e.State == EntityState.Modified) + .Select(entry => entry.Entity) + .OfType()) { + saveEntity.OnSavingChanges(); } - /// - /// Gets or sets the default connection string. - /// - public static string ConnectionString { get; set; } = @"Data Source=jellyfin.db"; + return base.SaveChanges(); + } - public virtual DbSet AccessSchedules { get; set; } + /// + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc); + base.OnModelCreating(modelBuilder); - public virtual DbSet ActivityLogs { get; set; } - - public virtual DbSet ApiKeys { get; set; } - - public virtual DbSet Devices { get; set; } - - public virtual DbSet DeviceOptions { get; set; } - - public virtual DbSet DisplayPreferences { get; set; } - - public virtual DbSet ImageInfos { get; set; } - - public virtual DbSet ItemDisplayPreferences { get; set; } - - public virtual DbSet CustomItemDisplayPreferences { get; set; } - - public virtual DbSet Permissions { get; set; } - - public virtual DbSet Preferences { get; set; } - - public virtual DbSet Users { get; set; } - - /*public virtual DbSet Artwork { get; set; } - - public virtual DbSet Books { get; set; } - - public virtual DbSet BookMetadata { get; set; } - - public virtual DbSet Chapters { get; set; } - - public virtual DbSet Collections { get; set; } - - public virtual DbSet CollectionItems { get; set; } - - public virtual DbSet Companies { get; set; } - - public virtual DbSet CompanyMetadata { get; set; } - - public virtual DbSet CustomItems { get; set; } - - public virtual DbSet CustomItemMetadata { get; set; } - - public virtual DbSet Episodes { get; set; } - - public virtual DbSet EpisodeMetadata { get; set; } - - public virtual DbSet Genres { get; set; } - - public virtual DbSet Groups { get; set; } - - public virtual DbSet Libraries { get; set; } - - public virtual DbSet LibraryItems { get; set; } - - public virtual DbSet LibraryRoot { get; set; } - - public virtual DbSet MediaFiles { get; set; } - - public virtual DbSet MediaFileStream { get; set; } - - public virtual DbSet Metadata { get; set; } - - public virtual DbSet MetadataProviders { get; set; } - - public virtual DbSet MetadataProviderIds { get; set; } - - public virtual DbSet Movies { get; set; } - - public virtual DbSet MovieMetadata { get; set; } - - public virtual DbSet MusicAlbums { get; set; } - - public virtual DbSet MusicAlbumMetadata { get; set; } - - public virtual DbSet People { get; set; } - - public virtual DbSet PersonRoles { get; set; } - - public virtual DbSet Photo { get; set; } - - public virtual DbSet PhotoMetadata { get; set; } - - public virtual DbSet ProviderMappings { get; set; } - - public virtual DbSet Ratings { get; set; } - - /// - /// Repository for global::Jellyfin.Data.Entities.RatingSource - This is the entity to - /// store review ratings, not age ratings. - /// - public virtual DbSet RatingSources { get; set; } - - public virtual DbSet Releases { get; set; } - - public virtual DbSet Seasons { get; set; } - - public virtual DbSet SeasonMetadata { get; set; } - - public virtual DbSet Series { get; set; } - - public virtual DbSet SeriesMetadata { get; set; } - - public virtual DbSet Tracks { get; set; } - - public virtual DbSet TrackMetadata { get; set; }*/ - - /// - public override int SaveChanges() - { - foreach (var saveEntity in ChangeTracker.Entries() - .Where(e => e.State == EntityState.Modified) - .Select(entry => entry.Entity) - .OfType()) - { - saveEntity.OnSavingChanges(); - } - - return base.SaveChanges(); - } - - /// - protected override void OnModelCreating(ModelBuilder modelBuilder) - { - modelBuilder.SetDefaultDateTimeKind(DateTimeKind.Utc); - base.OnModelCreating(modelBuilder); - - // Configuration for each entity is in it's own class inside 'ModelConfiguration'. - modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDb).Assembly); - } + // Configuration for each entity is in it's own class inside 'ModelConfiguration'. + modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDb).Assembly); } } From ab6baf6486601b6693e98339e2a41646196f0e76 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 10:28:31 -0500 Subject: [PATCH 3/8] Enable nullable for Jellyfin DbContext --- Jellyfin.Server.Implementations/JellyfinDb.cs | 110 +++++++++--------- 1 file changed, 52 insertions(+), 58 deletions(-) diff --git a/Jellyfin.Server.Implementations/JellyfinDb.cs b/Jellyfin.Server.Implementations/JellyfinDb.cs index 6443100806..5d3fc9e7db 100644 --- a/Jellyfin.Server.Implementations/JellyfinDb.cs +++ b/Jellyfin.Server.Implementations/JellyfinDb.cs @@ -1,4 +1,3 @@ -#nullable disable #pragma warning disable CS1591 using System; @@ -21,118 +20,113 @@ public class JellyfinDb : DbContext { } - /// - /// Gets or sets the default connection string. - /// - public static string ConnectionString { get; set; } = @"Data Source=jellyfin.db"; + public DbSet AccessSchedules => Set(); - public virtual DbSet AccessSchedules { get; set; } + public DbSet ActivityLogs => Set(); - public virtual DbSet ActivityLogs { get; set; } + public DbSet ApiKeys => Set(); - public virtual DbSet ApiKeys { get; set; } + public DbSet Devices => Set(); - public virtual DbSet Devices { get; set; } + public DbSet DeviceOptions => Set(); - public virtual DbSet DeviceOptions { get; set; } + public DbSet DisplayPreferences => Set(); - public virtual DbSet DisplayPreferences { get; set; } + public DbSet ImageInfos => Set(); - public virtual DbSet ImageInfos { get; set; } + public DbSet ItemDisplayPreferences => Set(); - public virtual DbSet ItemDisplayPreferences { get; set; } + public DbSet CustomItemDisplayPreferences => Set(); - public virtual DbSet CustomItemDisplayPreferences { get; set; } + public DbSet Permissions => Set(); - public virtual DbSet Permissions { get; set; } + public DbSet Preferences => Set(); - public virtual DbSet Preferences { get; set; } + public DbSet Users => Set(); - public virtual DbSet Users { get; set; } + /*public DbSet Artwork => Set(); - /*public virtual DbSet Artwork { get; set; } + public DbSet Books => Set(); - public virtual DbSet Books { get; set; } + public DbSet BookMetadata => Set(); - public virtual DbSet BookMetadata { get; set; } + public DbSet Chapters => Set(); - public virtual DbSet Chapters { get; set; } + public DbSet Collections => Set(); - public virtual DbSet Collections { get; set; } + public DbSet CollectionItems => Set(); - public virtual DbSet CollectionItems { get; set; } + public DbSet Companies => Set(); - public virtual DbSet Companies { get; set; } + public DbSet CompanyMetadata => Set(); - public virtual DbSet CompanyMetadata { get; set; } + public DbSet CustomItems => Set(); - public virtual DbSet CustomItems { get; set; } + public DbSet CustomItemMetadata => Set(); - public virtual DbSet CustomItemMetadata { get; set; } + public DbSet Episodes => Set(); - public virtual DbSet Episodes { get; set; } + public DbSet EpisodeMetadata => Set(); - public virtual DbSet EpisodeMetadata { get; set; } + public DbSet Genres => Set(); - public virtual DbSet Genres { get; set; } + public DbSet Groups => Set(); - public virtual DbSet Groups { get; set; } + public DbSet Libraries => Set(); - public virtual DbSet Libraries { get; set; } + public DbSet LibraryItems => Set(); - public virtual DbSet LibraryItems { get; set; } + public DbSet LibraryRoot => Set(); - public virtual DbSet LibraryRoot { get; set; } + public DbSet MediaFiles => Set(); - public virtual DbSet MediaFiles { get; set; } + public DbSet MediaFileStream => Set(); - public virtual DbSet MediaFileStream { get; set; } + public DbSet Metadata => Set(); - public virtual DbSet Metadata { get; set; } + public DbSet MetadataProviders => Set(); - public virtual DbSet MetadataProviders { get; set; } + public DbSet MetadataProviderIds => Set(); - public virtual DbSet MetadataProviderIds { get; set; } + public DbSet Movies => Set(); - public virtual DbSet Movies { get; set; } + public DbSet MovieMetadata => Set(); - public virtual DbSet MovieMetadata { get; set; } + public DbSet MusicAlbums => Set(); - public virtual DbSet MusicAlbums { get; set; } + public DbSet MusicAlbumMetadata => Set(); - public virtual DbSet MusicAlbumMetadata { get; set; } + public DbSet People => Set(); - public virtual DbSet People { get; set; } + public DbSet PersonRoles => Set(); - public virtual DbSet PersonRoles { get; set; } + public DbSet Photo => Set(); - public virtual DbSet Photo { get; set; } + public DbSet PhotoMetadata => Set(); - public virtual DbSet PhotoMetadata { get; set; } + public DbSet ProviderMappings => Set(); - public virtual DbSet ProviderMappings { get; set; } - - public virtual DbSet Ratings { get; set; } + public DbSet Ratings => Set(); /// /// Repository for global::Jellyfin.Data.Entities.RatingSource - This is the entity to /// store review ratings, not age ratings. /// - public virtual DbSet RatingSources { get; set; } + public DbSet RatingSources => Set(); - public virtual DbSet Releases { get; set; } + public DbSet Releases => Set(); - public virtual DbSet Seasons { get; set; } + public DbSet Seasons => Set(); - public virtual DbSet SeasonMetadata { get; set; } + public DbSet SeasonMetadata => Set(); - public virtual DbSet Series { get; set; } + public DbSet Series => Set(); - public virtual DbSet SeriesMetadata { get; set; } + public DbSet SeriesMetadata => Set Tracks { get; set; } + public DbSet Tracks => Set(); - public virtual DbSet TrackMetadata { get; set; }*/ + public DbSet TrackMetadata => Set();*/ /// public override int SaveChanges() From f07553abdf9c3b1462a94de154ec0072cdbf686a Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 11:49:59 -0500 Subject: [PATCH 4/8] Optimize EF Core queries and remove unnecessary AsQueryable calls --- .../Activity/ActivityManager.cs | 28 +++++++++---------- .../Devices/DeviceManager.cs | 26 +++++------------ .../Security/AuthenticationManager.cs | 2 -- .../Users/DisplayPreferencesManager.cs | 3 -- .../Users/UserManager.cs | 1 - 5 files changed, 21 insertions(+), 39 deletions(-) diff --git a/Jellyfin.Server.Implementations/Activity/ActivityManager.cs b/Jellyfin.Server.Implementations/Activity/ActivityManager.cs index 9d6ca6aabe..fc03cd6ae4 100644 --- a/Jellyfin.Server.Implementations/Activity/ActivityManager.cs +++ b/Jellyfin.Server.Implementations/Activity/ActivityManager.cs @@ -48,18 +48,10 @@ namespace Jellyfin.Server.Implementations.Activity var dbContext = await _provider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { - IQueryable entries = dbContext.ActivityLogs - .OrderByDescending(entry => entry.DateCreated); - - if (query.MinDate.HasValue) - { - entries = entries.Where(entry => entry.DateCreated >= query.MinDate); - } - - if (query.HasUserId.HasValue) - { - entries = entries.Where(entry => (!entry.UserId.Equals(default)) == query.HasUserId.Value); - } + var entries = dbContext.ActivityLogs + .OrderByDescending(entry => entry.DateCreated) + .Where(entry => query.MinDate == null || entry.DateCreated >= query.MinDate) + .Where(entry => !query.HasUserId.HasValue || entry.UserId.Equals(default) != query.HasUserId.Value); return new QueryResult( query.Skip, @@ -67,8 +59,16 @@ namespace Jellyfin.Server.Implementations.Activity await entries .Skip(query.Skip ?? 0) .Take(query.Limit ?? 100) - .AsAsyncEnumerable() - .Select(ConvertToOldModel) + .Select(entity => new ActivityLogEntry(entity.Name, entity.Type, entity.UserId) + { + Id = entity.Id, + Overview = entity.Overview, + ShortOverview = entity.ShortOverview, + ItemId = entity.ItemId, + Date = entity.DateCreated, + Severity = entity.LogSeverity + }) + .AsQueryable() .ToListAsync() .ConfigureAwait(false)); } diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs index 15ac5c668a..a9b974be0e 100644 --- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs +++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs @@ -54,7 +54,7 @@ namespace Jellyfin.Server.Implementations.Devices var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { - deviceOptions = await dbContext.DeviceOptions.AsQueryable().FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false); + deviceOptions = await dbContext.DeviceOptions.FirstOrDefaultAsync(dev => dev.DeviceId == deviceId).ConfigureAwait(false); if (deviceOptions is null) { deviceOptions = new DeviceOptions(deviceId); @@ -132,22 +132,11 @@ namespace Jellyfin.Server.Implementations.Devices var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { - var devices = dbContext.Devices.AsQueryable(); - - if (query.UserId.HasValue) - { - devices = devices.Where(device => device.UserId.Equals(query.UserId.Value)); - } - - if (query.DeviceId is not null) - { - devices = devices.Where(device => device.DeviceId == query.DeviceId); - } - - if (query.AccessToken is not null) - { - devices = devices.Where(device => device.AccessToken == query.AccessToken); - } + var devices = dbContext.Devices + .OrderBy(d => d.Id) + .Where(device => !query.UserId.HasValue || device.UserId.Equals(query.UserId.Value)) + .Where(device => query.DeviceId == null || device.DeviceId == query.DeviceId) + .Where(device => query.AccessToken == null || device.AccessToken == query.AccessToken); var count = await devices.CountAsync().ConfigureAwait(false); @@ -179,11 +168,10 @@ namespace Jellyfin.Server.Implementations.Devices /// public async Task> GetDevicesForUser(Guid? userId, bool? supportsSync) { - IAsyncEnumerable sessions; var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { - sessions = dbContext.Devices + IAsyncEnumerable sessions = dbContext.Devices .Include(d => d.User) .OrderByDescending(d => d.DateLastActivity) .ThenBy(d => d.DeviceId) diff --git a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs b/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs index 810e578075..1b56237fe7 100644 --- a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs +++ b/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs @@ -40,7 +40,6 @@ namespace Jellyfin.Server.Implementations.Security await using (dbContext.ConfigureAwait(false)) { return await dbContext.ApiKeys - .AsAsyncEnumerable() .Select(key => new AuthenticationInfo { AppName = key.Name, @@ -60,7 +59,6 @@ namespace Jellyfin.Server.Implementations.Security await using (dbContext.ConfigureAwait(false)) { var key = await dbContext.ApiKeys - .AsQueryable() .Where(apiKey => apiKey.AccessToken == accessToken) .FirstOrDefaultAsync() .ConfigureAwait(false); diff --git a/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs b/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs index fddad1c4f9..8936f57c62 100644 --- a/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs +++ b/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs @@ -62,7 +62,6 @@ namespace Jellyfin.Server.Implementations.Users public IList ListItemDisplayPreferences(Guid userId, string client) { return _dbContext.ItemDisplayPreferences - .AsQueryable() .Where(prefs => prefs.UserId.Equals(userId) && !prefs.ItemId.Equals(default) && string.Equals(prefs.Client, client)) .ToList(); } @@ -71,7 +70,6 @@ namespace Jellyfin.Server.Implementations.Users public Dictionary ListCustomItemDisplayPreferences(Guid userId, Guid itemId, string client) { return _dbContext.CustomItemDisplayPreferences - .AsQueryable() .Where(prefs => prefs.UserId.Equals(userId) && prefs.ItemId.Equals(itemId) && string.Equals(prefs.Client, client)) @@ -82,7 +80,6 @@ namespace Jellyfin.Server.Implementations.Users public void SetCustomItemDisplayPreferences(Guid userId, Guid itemId, string client, Dictionary customPreferences) { var existingPrefs = _dbContext.CustomItemDisplayPreferences - .AsQueryable() .Where(prefs => prefs.UserId.Equals(userId) && prefs.ItemId.Equals(itemId) && string.Equals(prefs.Client, client)); diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.cs index 19ac007b93..5f9814ed37 100644 --- a/Jellyfin.Server.Implementations/Users/UserManager.cs +++ b/Jellyfin.Server.Implementations/Users/UserManager.cs @@ -143,7 +143,6 @@ namespace Jellyfin.Server.Implementations.Users await using (dbContext.ConfigureAwait(false)) { if (await dbContext.Users - .AsQueryable() .AnyAsync(u => u.Username == newName && !u.Id.Equals(user.Id)) .ConfigureAwait(false)) { From 40e4370689501789974cdd827341da74f4c71c79 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 11:52:53 -0500 Subject: [PATCH 5/8] Specify AsSplitQuery in initial users query --- Jellyfin.Server.Implementations/Users/UserManager.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.cs index 5f9814ed37..266bff5e8d 100644 --- a/Jellyfin.Server.Implementations/Users/UserManager.cs +++ b/Jellyfin.Server.Implementations/Users/UserManager.cs @@ -85,6 +85,7 @@ namespace Jellyfin.Server.Implementations.Users _users = new ConcurrentDictionary(); using var dbContext = _dbProvider.CreateDbContext(); foreach (var user in dbContext.Users + .AsSplitQuery() .Include(user => user.Permissions) .Include(user => user.Preferences) .Include(user => user.AccessSchedules) From 3f66a482069ab98396952292db379e7248cb1166 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 12:13:06 -0500 Subject: [PATCH 6/8] Document JellyfinDb --- Jellyfin.Server.Implementations/JellyfinDb.cs | 38 ++++++++++++++++++- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/Jellyfin.Server.Implementations/JellyfinDb.cs b/Jellyfin.Server.Implementations/JellyfinDb.cs index 5d3fc9e7db..064118ca54 100644 --- a/Jellyfin.Server.Implementations/JellyfinDb.cs +++ b/Jellyfin.Server.Implementations/JellyfinDb.cs @@ -1,5 +1,3 @@ -#pragma warning disable CS1591 - using System; using System.Linq; using Jellyfin.Data.Entities; @@ -20,28 +18,64 @@ public class JellyfinDb : DbContext { } + /// + /// Gets the containing the access schedules. + /// public DbSet AccessSchedules => Set(); + /// + /// Gets the containing the activity logs. + /// public DbSet ActivityLogs => Set(); + /// + /// Gets the containing the API keys. + /// public DbSet ApiKeys => Set(); + /// + /// Gets the containing the devices. + /// public DbSet Devices => Set(); + /// + /// Gets the containing the device options. + /// public DbSet DeviceOptions => Set(); + /// + /// Gets the containing the display preferences. + /// public DbSet DisplayPreferences => Set(); + /// + /// Gets the containing the image infos. + /// public DbSet ImageInfos => Set(); + /// + /// Gets the containing the item display preferences. + /// public DbSet ItemDisplayPreferences => Set(); + /// + /// Gets the containing the custom item display preferences. + /// public DbSet CustomItemDisplayPreferences => Set(); + /// + /// Gets the containing the permissions. + /// public DbSet Permissions => Set(); + /// + /// Gets the containing the preferences. + /// public DbSet Preferences => Set(); + /// + /// Gets the containing the users. + /// public DbSet Users => Set(); /*public DbSet Artwork => Set(); From 8479f0f90cd8b7180f45340a59d7122755987859 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 12:14:44 -0500 Subject: [PATCH 7/8] Rename JellyfinDb to JellyfinDbContext --- Emby.Server.Implementations/ApplicationHost.cs | 2 +- .../ScheduledTasks/Tasks/OptimizeDatabaseTask.cs | 4 ++-- .../Activity/ActivityManager.cs | 4 ++-- .../Devices/DeviceManager.cs | 4 ++-- .../Extensions/ServiceCollectionExtensions.cs | 2 +- .../{JellyfinDb.cs => JellyfinDbContext.cs} | 8 ++++---- .../20200514181226_AddActivityLog.Designer.cs | 2 +- .../Migrations/20200613202153_AddUsers.Designer.cs | 2 +- .../20200728005145_AddDisplayPreferences.Designer.cs | 2 +- ...200905220533_FixDisplayPreferencesIndex.Designer.cs | 2 +- .../20201004171403_AddMaxActiveSessions.Designer.cs | 2 +- ...01204223655_AddCustomDisplayPreferences.Designer.cs | 2 +- .../20210320181425_AddIndexesAndCollations.Designer.cs | 2 +- .../20210407110544_NullableCustomPrefValue.Designer.cs | 2 +- .../Migrations/20210814002109_AddDevices.Designer.cs | 2 +- ...2080052_AddIndexActivityLogsDateCreated.Designer.cs | 2 +- .../Migrations/DesignTimeJellyfinDbFactory.cs | 10 +++++----- .../Migrations/JellyfinDbModelSnapshot.cs | 2 +- .../Security/AuthenticationManager.cs | 4 ++-- .../Security/AuthorizationContext.cs | 4 ++-- .../Users/DisplayPreferencesManager.cs | 4 ++-- Jellyfin.Server.Implementations/Users/UserManager.cs | 8 ++++---- Jellyfin.Server/CoreAppHost.cs | 2 +- .../Migrations/Routines/MigrateActivityLogDb.cs | 4 ++-- .../Migrations/Routines/MigrateAuthenticationDb.cs | 4 ++-- .../Migrations/Routines/MigrateDisplayPreferencesDb.cs | 4 ++-- Jellyfin.Server/Migrations/Routines/MigrateUserDb.cs | 4 ++-- Jellyfin.Server/Program.cs | 2 +- Jellyfin.Server/Startup.cs | 2 +- 29 files changed, 49 insertions(+), 49 deletions(-) rename Jellyfin.Server.Implementations/{JellyfinDb.cs => JellyfinDbContext.cs} (96%) diff --git a/Emby.Server.Implementations/ApplicationHost.cs b/Emby.Server.Implementations/ApplicationHost.cs index 7b3d07dfc1..0b8a314192 100644 --- a/Emby.Server.Implementations/ApplicationHost.cs +++ b/Emby.Server.Implementations/ApplicationHost.cs @@ -654,7 +654,7 @@ namespace Emby.Server.Implementations /// A task representing the service initialization operation. public async Task InitializeServices() { - var jellyfinDb = await Resolve>().CreateDbContextAsync().ConfigureAwait(false); + var jellyfinDb = await Resolve>().CreateDbContextAsync().ConfigureAwait(false); await using (jellyfinDb.ConfigureAwait(false)) { if ((await jellyfinDb.Database.GetPendingMigrationsAsync().ConfigureAwait(false)).Any()) diff --git a/Emby.Server.Implementations/ScheduledTasks/Tasks/OptimizeDatabaseTask.cs b/Emby.Server.Implementations/ScheduledTasks/Tasks/OptimizeDatabaseTask.cs index 1efacd8562..1f3cb9b63f 100644 --- a/Emby.Server.Implementations/ScheduledTasks/Tasks/OptimizeDatabaseTask.cs +++ b/Emby.Server.Implementations/ScheduledTasks/Tasks/OptimizeDatabaseTask.cs @@ -17,7 +17,7 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks { private readonly ILogger _logger; private readonly ILocalizationManager _localization; - private readonly IDbContextFactory _provider; + private readonly IDbContextFactory _provider; /// /// Initializes a new instance of the class. @@ -28,7 +28,7 @@ namespace Emby.Server.Implementations.ScheduledTasks.Tasks public OptimizeDatabaseTask( ILogger logger, ILocalizationManager localization, - IDbContextFactory provider) + IDbContextFactory provider) { _logger = logger; _localization = localization; diff --git a/Jellyfin.Server.Implementations/Activity/ActivityManager.cs b/Jellyfin.Server.Implementations/Activity/ActivityManager.cs index fc03cd6ae4..ce1c54cbb2 100644 --- a/Jellyfin.Server.Implementations/Activity/ActivityManager.cs +++ b/Jellyfin.Server.Implementations/Activity/ActivityManager.cs @@ -15,13 +15,13 @@ namespace Jellyfin.Server.Implementations.Activity /// public class ActivityManager : IActivityManager { - private readonly IDbContextFactory _provider; + private readonly IDbContextFactory _provider; /// /// Initializes a new instance of the class. /// /// The Jellyfin database provider. - public ActivityManager(IDbContextFactory provider) + public ActivityManager(IDbContextFactory provider) { _provider = provider; } diff --git a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs index a9b974be0e..8b15d6823d 100644 --- a/Jellyfin.Server.Implementations/Devices/DeviceManager.cs +++ b/Jellyfin.Server.Implementations/Devices/DeviceManager.cs @@ -23,7 +23,7 @@ namespace Jellyfin.Server.Implementations.Devices /// public class DeviceManager : IDeviceManager { - private readonly IDbContextFactory _dbProvider; + private readonly IDbContextFactory _dbProvider; private readonly IUserManager _userManager; private readonly ConcurrentDictionary _capabilitiesMap = new(); @@ -32,7 +32,7 @@ namespace Jellyfin.Server.Implementations.Devices /// /// The database provider. /// The user manager. - public DeviceManager(IDbContextFactory dbProvider, IUserManager userManager) + public DeviceManager(IDbContextFactory dbProvider, IUserManager userManager) { _dbProvider = dbProvider; _userManager = userManager; diff --git a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs index 05c6229316..de64911b42 100644 --- a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs +++ b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs @@ -29,7 +29,7 @@ public static class ServiceCollectionExtensions .SkipCachingResults(result => result.Value is null || (result.Value is EFTableRows rows && rows.RowsCount == 0))); - serviceCollection.AddPooledDbContextFactory((serviceProvider, opt) => + serviceCollection.AddPooledDbContextFactory((serviceProvider, opt) => { var applicationPaths = serviceProvider.GetRequiredService(); var loggerFactory = serviceProvider.GetRequiredService(); diff --git a/Jellyfin.Server.Implementations/JellyfinDb.cs b/Jellyfin.Server.Implementations/JellyfinDbContext.cs similarity index 96% rename from Jellyfin.Server.Implementations/JellyfinDb.cs rename to Jellyfin.Server.Implementations/JellyfinDbContext.cs index 064118ca54..0d91707e3e 100644 --- a/Jellyfin.Server.Implementations/JellyfinDb.cs +++ b/Jellyfin.Server.Implementations/JellyfinDbContext.cs @@ -8,13 +8,13 @@ using Microsoft.EntityFrameworkCore; namespace Jellyfin.Server.Implementations; /// -public class JellyfinDb : DbContext +public class JellyfinDbContext : DbContext { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The database context options. - public JellyfinDb(DbContextOptions options) : base(options) + public JellyfinDbContext(DbContextOptions options) : base(options) { } @@ -183,6 +183,6 @@ public class JellyfinDb : DbContext base.OnModelCreating(modelBuilder); // Configuration for each entity is in it's own class inside 'ModelConfiguration'. - modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDb).Assembly); + modelBuilder.ApplyConfigurationsFromAssembly(typeof(JellyfinDbContext).Assembly); } } diff --git a/Jellyfin.Server.Implementations/Migrations/20200514181226_AddActivityLog.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20200514181226_AddActivityLog.Designer.cs index 98a83b7450..4be6c2faa3 100644 --- a/Jellyfin.Server.Implementations/Migrations/20200514181226_AddActivityLog.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20200514181226_AddActivityLog.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20200514181226_AddActivityLog")] partial class AddActivityLog { diff --git a/Jellyfin.Server.Implementations/Migrations/20200613202153_AddUsers.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20200613202153_AddUsers.Designer.cs index 6342ce9cf3..f3254734ad 100644 --- a/Jellyfin.Server.Implementations/Migrations/20200613202153_AddUsers.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20200613202153_AddUsers.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20200613202153_AddUsers")] partial class AddUsers { diff --git a/Jellyfin.Server.Implementations/Migrations/20200728005145_AddDisplayPreferences.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20200728005145_AddDisplayPreferences.Designer.cs index d44707d069..12d6faa8f5 100644 --- a/Jellyfin.Server.Implementations/Migrations/20200728005145_AddDisplayPreferences.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20200728005145_AddDisplayPreferences.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20200728005145_AddDisplayPreferences")] partial class AddDisplayPreferences { diff --git a/Jellyfin.Server.Implementations/Migrations/20200905220533_FixDisplayPreferencesIndex.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20200905220533_FixDisplayPreferencesIndex.Designer.cs index 2234f9d5fd..f1cc208058 100644 --- a/Jellyfin.Server.Implementations/Migrations/20200905220533_FixDisplayPreferencesIndex.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20200905220533_FixDisplayPreferencesIndex.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20200905220533_FixDisplayPreferencesIndex")] partial class FixDisplayPreferencesIndex { diff --git a/Jellyfin.Server.Implementations/Migrations/20201004171403_AddMaxActiveSessions.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20201004171403_AddMaxActiveSessions.Designer.cs index e5c326a326..f134d363c8 100644 --- a/Jellyfin.Server.Implementations/Migrations/20201004171403_AddMaxActiveSessions.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20201004171403_AddMaxActiveSessions.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20201004171403_AddMaxActiveSessions")] partial class AddMaxActiveSessions { diff --git a/Jellyfin.Server.Implementations/Migrations/20201204223655_AddCustomDisplayPreferences.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20201204223655_AddCustomDisplayPreferences.Designer.cs index 10663d0655..ec65205d45 100644 --- a/Jellyfin.Server.Implementations/Migrations/20201204223655_AddCustomDisplayPreferences.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20201204223655_AddCustomDisplayPreferences.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20201204223655_AddCustomDisplayPreferences")] partial class AddCustomDisplayPreferences { diff --git a/Jellyfin.Server.Implementations/Migrations/20210320181425_AddIndexesAndCollations.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20210320181425_AddIndexesAndCollations.Designer.cs index 8696768245..45dad6be68 100644 --- a/Jellyfin.Server.Implementations/Migrations/20210320181425_AddIndexesAndCollations.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20210320181425_AddIndexesAndCollations.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20210320181425_AddIndexesAndCollations")] partial class AddIndexesAndCollations { diff --git a/Jellyfin.Server.Implementations/Migrations/20210407110544_NullableCustomPrefValue.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20210407110544_NullableCustomPrefValue.Designer.cs index d332d19f28..eff84b4574 100644 --- a/Jellyfin.Server.Implementations/Migrations/20210407110544_NullableCustomPrefValue.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20210407110544_NullableCustomPrefValue.Designer.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20210407110544_NullableCustomPrefValue")] partial class NullableCustomPrefValue { diff --git a/Jellyfin.Server.Implementations/Migrations/20210814002109_AddDevices.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20210814002109_AddDevices.Designer.cs index 7e9566e2ea..ad7c2dd2c9 100644 --- a/Jellyfin.Server.Implementations/Migrations/20210814002109_AddDevices.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20210814002109_AddDevices.Designer.cs @@ -10,7 +10,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20210814002109_AddDevices")] partial class AddDevices { diff --git a/Jellyfin.Server.Implementations/Migrations/20221022080052_AddIndexActivityLogsDateCreated.Designer.cs b/Jellyfin.Server.Implementations/Migrations/20221022080052_AddIndexActivityLogsDateCreated.Designer.cs index 03e3f3c921..f9497a3b69 100644 --- a/Jellyfin.Server.Implementations/Migrations/20221022080052_AddIndexActivityLogsDateCreated.Designer.cs +++ b/Jellyfin.Server.Implementations/Migrations/20221022080052_AddIndexActivityLogsDateCreated.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] [Migration("20221022080052_AddIndexActivityLogsDateCreated")] partial class AddIndexActivityLogsDateCreated { diff --git a/Jellyfin.Server.Implementations/Migrations/DesignTimeJellyfinDbFactory.cs b/Jellyfin.Server.Implementations/Migrations/DesignTimeJellyfinDbFactory.cs index 72a4a8c3b6..940cf7c5d5 100644 --- a/Jellyfin.Server.Implementations/Migrations/DesignTimeJellyfinDbFactory.cs +++ b/Jellyfin.Server.Implementations/Migrations/DesignTimeJellyfinDbFactory.cs @@ -4,17 +4,17 @@ using Microsoft.EntityFrameworkCore.Design; namespace Jellyfin.Server.Implementations.Migrations { /// - /// The design time factory for . + /// The design time factory for . /// This is only used for the creation of migrations and not during runtime. /// - internal class DesignTimeJellyfinDbFactory : IDesignTimeDbContextFactory + internal class DesignTimeJellyfinDbFactory : IDesignTimeDbContextFactory { - public JellyfinDb CreateDbContext(string[] args) + public JellyfinDbContext CreateDbContext(string[] args) { - var optionsBuilder = new DbContextOptionsBuilder(); + var optionsBuilder = new DbContextOptionsBuilder(); optionsBuilder.UseSqlite("Data Source=jellyfin.db"); - return new JellyfinDb(optionsBuilder.Options); + return new JellyfinDbContext(optionsBuilder.Options); } } } diff --git a/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs b/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs index 2dd7b094aa..dd5f7f0121 100644 --- a/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs +++ b/Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs @@ -9,7 +9,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace Jellyfin.Server.Implementations.Migrations { - [DbContext(typeof(JellyfinDb))] + [DbContext(typeof(JellyfinDbContext))] partial class JellyfinDbModelSnapshot : ModelSnapshot { protected override void BuildModel(ModelBuilder modelBuilder) diff --git a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs b/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs index 1b56237fe7..b2dfe60a14 100644 --- a/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs +++ b/Jellyfin.Server.Implementations/Security/AuthenticationManager.cs @@ -10,13 +10,13 @@ namespace Jellyfin.Server.Implementations.Security /// public class AuthenticationManager : IAuthenticationManager { - private readonly IDbContextFactory _dbProvider; + private readonly IDbContextFactory _dbProvider; /// /// Initializes a new instance of the class. /// /// The database provider. - public AuthenticationManager(IDbContextFactory dbProvider) + public AuthenticationManager(IDbContextFactory dbProvider) { _dbProvider = dbProvider; } diff --git a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs index ec5742bab0..63d3e8a04c 100644 --- a/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs +++ b/Jellyfin.Server.Implementations/Security/AuthorizationContext.cs @@ -16,12 +16,12 @@ namespace Jellyfin.Server.Implementations.Security { public class AuthorizationContext : IAuthorizationContext { - private readonly IDbContextFactory _jellyfinDbProvider; + private readonly IDbContextFactory _jellyfinDbProvider; private readonly IUserManager _userManager; private readonly IServerApplicationHost _serverApplicationHost; public AuthorizationContext( - IDbContextFactory jellyfinDb, + IDbContextFactory jellyfinDb, IUserManager userManager, IServerApplicationHost serverApplicationHost) { diff --git a/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs b/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs index 8936f57c62..bfae81e4ca 100644 --- a/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs +++ b/Jellyfin.Server.Implementations/Users/DisplayPreferencesManager.cs @@ -15,13 +15,13 @@ namespace Jellyfin.Server.Implementations.Users /// public class DisplayPreferencesManager : IDisplayPreferencesManager { - private readonly JellyfinDb _dbContext; + private readonly JellyfinDbContext _dbContext; /// /// Initializes a new instance of the class. /// /// The database context factory. - public DisplayPreferencesManager(IDbContextFactory dbContextFactory) + public DisplayPreferencesManager(IDbContextFactory dbContextFactory) { _dbContext = dbContextFactory.CreateDbContext(); } diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.cs index 266bff5e8d..dc9d78857e 100644 --- a/Jellyfin.Server.Implementations/Users/UserManager.cs +++ b/Jellyfin.Server.Implementations/Users/UserManager.cs @@ -33,7 +33,7 @@ namespace Jellyfin.Server.Implementations.Users /// public class UserManager : IUserManager { - private readonly IDbContextFactory _dbProvider; + private readonly IDbContextFactory _dbProvider; private readonly IEventManager _eventManager; private readonly ICryptoProvider _cryptoProvider; private readonly INetworkManager _networkManager; @@ -59,7 +59,7 @@ namespace Jellyfin.Server.Implementations.Users /// The image processor. /// The logger. public UserManager( - IDbContextFactory dbProvider, + IDbContextFactory dbProvider, IEventManager eventManager, ICryptoProvider cryptoProvider, INetworkManager networkManager, @@ -172,7 +172,7 @@ namespace Jellyfin.Server.Implementations.Users } } - internal async Task CreateUserInternalAsync(string name, JellyfinDb dbContext) + internal async Task CreateUserInternalAsync(string name, JellyfinDbContext dbContext) { // TODO: Remove after user item data is migrated. var max = await dbContext.Users.AsQueryable().AnyAsync().ConfigureAwait(false) @@ -886,7 +886,7 @@ namespace Jellyfin.Server.Implementations.Users await UpdateUserAsync(user).ConfigureAwait(false); } - private async Task UpdateUserInternalAsync(JellyfinDb dbContext, User user) + private async Task UpdateUserInternalAsync(JellyfinDbContext dbContext, User user) { dbContext.Users.Update(user); _users[user.Id] = user; diff --git a/Jellyfin.Server/CoreAppHost.cs b/Jellyfin.Server/CoreAppHost.cs index d70b8f3ab7..40cd5a0446 100644 --- a/Jellyfin.Server/CoreAppHost.cs +++ b/Jellyfin.Server/CoreAppHost.cs @@ -107,7 +107,7 @@ namespace Jellyfin.Server yield return typeof(CoreAppHost).Assembly; // Jellyfin.Server.Implementations - yield return typeof(JellyfinDb).Assembly; + yield return typeof(JellyfinDbContext).Assembly; } /// diff --git a/Jellyfin.Server/Migrations/Routines/MigrateActivityLogDb.cs b/Jellyfin.Server/Migrations/Routines/MigrateActivityLogDb.cs index bf66f75ff9..e8a0af9f88 100644 --- a/Jellyfin.Server/Migrations/Routines/MigrateActivityLogDb.cs +++ b/Jellyfin.Server/Migrations/Routines/MigrateActivityLogDb.cs @@ -19,7 +19,7 @@ namespace Jellyfin.Server.Migrations.Routines private const string DbFilename = "activitylog.db"; private readonly ILogger _logger; - private readonly IDbContextFactory _provider; + private readonly IDbContextFactory _provider; private readonly IServerApplicationPaths _paths; /// @@ -28,7 +28,7 @@ namespace Jellyfin.Server.Migrations.Routines /// The logger. /// The server application paths. /// The database provider. - public MigrateActivityLogDb(ILogger logger, IServerApplicationPaths paths, IDbContextFactory provider) + public MigrateActivityLogDb(ILogger logger, IServerApplicationPaths paths, IDbContextFactory provider) { _logger = logger; _provider = provider; diff --git a/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs b/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs index bf1ea8233d..09daae0ff9 100644 --- a/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs +++ b/Jellyfin.Server/Migrations/Routines/MigrateAuthenticationDb.cs @@ -20,7 +20,7 @@ namespace Jellyfin.Server.Migrations.Routines private const string DbFilename = "authentication.db"; private readonly ILogger _logger; - private readonly IDbContextFactory _dbProvider; + private readonly IDbContextFactory _dbProvider; private readonly IServerApplicationPaths _appPaths; private readonly IUserManager _userManager; @@ -33,7 +33,7 @@ namespace Jellyfin.Server.Migrations.Routines /// The user manager. public MigrateAuthenticationDb( ILogger logger, - IDbContextFactory dbProvider, + IDbContextFactory dbProvider, IServerApplicationPaths appPaths, IUserManager userManager) { diff --git a/Jellyfin.Server/Migrations/Routines/MigrateDisplayPreferencesDb.cs b/Jellyfin.Server/Migrations/Routines/MigrateDisplayPreferencesDb.cs index 0fad77cfe6..4b692d14f0 100644 --- a/Jellyfin.Server/Migrations/Routines/MigrateDisplayPreferencesDb.cs +++ b/Jellyfin.Server/Migrations/Routines/MigrateDisplayPreferencesDb.cs @@ -25,7 +25,7 @@ namespace Jellyfin.Server.Migrations.Routines private readonly ILogger _logger; private readonly IServerApplicationPaths _paths; - private readonly IDbContextFactory _provider; + private readonly IDbContextFactory _provider; private readonly JsonSerializerOptions _jsonOptions; private readonly IUserManager _userManager; @@ -39,7 +39,7 @@ namespace Jellyfin.Server.Migrations.Routines public MigrateDisplayPreferencesDb( ILogger logger, IServerApplicationPaths paths, - IDbContextFactory provider, + IDbContextFactory provider, IUserManager userManager) { _logger = logger; diff --git a/Jellyfin.Server/Migrations/Routines/MigrateUserDb.cs b/Jellyfin.Server/Migrations/Routines/MigrateUserDb.cs index 2dbd82e8fd..ea2f033027 100644 --- a/Jellyfin.Server/Migrations/Routines/MigrateUserDb.cs +++ b/Jellyfin.Server/Migrations/Routines/MigrateUserDb.cs @@ -27,7 +27,7 @@ namespace Jellyfin.Server.Migrations.Routines private readonly ILogger _logger; private readonly IServerApplicationPaths _paths; - private readonly IDbContextFactory _provider; + private readonly IDbContextFactory _provider; private readonly IXmlSerializer _xmlSerializer; /// @@ -40,7 +40,7 @@ namespace Jellyfin.Server.Migrations.Routines public MigrateUserDb( ILogger logger, IServerApplicationPaths paths, - IDbContextFactory provider, + IDbContextFactory provider, IXmlSerializer xmlSerializer) { _logger = logger; diff --git a/Jellyfin.Server/Program.cs b/Jellyfin.Server/Program.cs index 540375dce6..70d7a07011 100644 --- a/Jellyfin.Server/Program.cs +++ b/Jellyfin.Server/Program.cs @@ -236,7 +236,7 @@ namespace Jellyfin.Server { _logger.LogInformation("Running query planner optimizations in the database... This might take a while"); // Run before disposing the application - var context = await appHost.ServiceProvider.GetRequiredService>().CreateDbContextAsync().ConfigureAwait(false); + var context = await appHost.ServiceProvider.GetRequiredService>().CreateDbContextAsync().ConfigureAwait(false); await using (context.ConfigureAwait(false)) { if (context.Database.IsSqlite()) diff --git a/Jellyfin.Server/Startup.cs b/Jellyfin.Server/Startup.cs index f89f81c766..c5f20b205b 100644 --- a/Jellyfin.Server/Startup.cs +++ b/Jellyfin.Server/Startup.cs @@ -119,7 +119,7 @@ namespace Jellyfin.Server .ConfigurePrimaryHttpMessageHandler(defaultHttpClientHandlerDelegate); services.AddHealthChecks() - .AddCheck>(nameof(JellyfinDb)); + .AddCheck>(nameof(JellyfinDbContext)); services.AddHlsPlaylistGenerator(); } From f3e5139cfc05ae0c3968f9b07fabfe8b3d5868e4 Mon Sep 17 00:00:00 2001 From: Patrick Barron Date: Mon, 16 Jan 2023 18:15:05 -0500 Subject: [PATCH 8/8] Use default ASP.NET Core logger factory for DbContext factory --- .../Extensions/ServiceCollectionExtensions.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs index de64911b42..bb8d4dd14f 100644 --- a/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs +++ b/Jellyfin.Server.Implementations/Extensions/ServiceCollectionExtensions.cs @@ -4,7 +4,6 @@ using EFCoreSecondLevelCacheInterceptor; using MediaBrowser.Common.Configuration; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; namespace Jellyfin.Server.Implementations.Extensions; @@ -32,10 +31,8 @@ public static class ServiceCollectionExtensions serviceCollection.AddPooledDbContextFactory((serviceProvider, opt) => { var applicationPaths = serviceProvider.GetRequiredService(); - var loggerFactory = serviceProvider.GetRequiredService(); opt.UseSqlite($"Filename={Path.Combine(applicationPaths.DataPath, "jellyfin.db")}") - .AddInterceptors(serviceProvider.GetRequiredService()) - .UseLoggerFactory(loggerFactory); + .AddInterceptors(serviceProvider.GetRequiredService()); }); return serviceCollection;