Stop user updates from orphaning permission and preference rows (#17645)

* Stop user updates from orphaning permission and preference rows

* Make UserId non-nullable

* Remove unnecessary ToList

* Update Jellyfin.Server.Implementations/Users/UserManager.cs

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>

---------

Co-authored-by: Claus Vium <cvium@users.noreply.github.com>
This commit is contained in:
Tim Eisele
2026-08-20 18:35:26 +02:00
committed by GitHub
parent c70933a23f
commit fb50b4df8b
11 changed files with 2173 additions and 59 deletions
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Jellyfin.Database.Implementations;
@@ -92,28 +91,6 @@ namespace Jellyfin.Server.Implementations.Tests.Users
new NoLockBehavior(NullLogger<NoLockBehavior>.Instance));
}
[Fact]
public async Task UpdateUserAsync_DoesNotLeaveOrphanedPermissionsOrPreferences()
{
var user = await _userManager.CreateUserAsync("updateduser");
var permissionCount = user.Permissions.Count;
var preferenceCount = user.Preferences.Count;
user.LastActivityDate = DateTime.UtcNow;
await _userManager.UpdateUserAsync(user);
await _userManager.UpdateUserAsync(user);
await using var context = CreateDbContext();
Assert.Empty(await context.Permissions
.Where(permission => !permission.UserId.HasValue)
.ToListAsync(TestContext.Current.CancellationToken));
Assert.Empty(await context.Preferences
.Where(preference => !preference.UserId.HasValue)
.ToListAsync(TestContext.Current.CancellationToken));
Assert.Equal(permissionCount, await context.Permissions.CountAsync(TestContext.Current.CancellationToken));
Assert.Equal(preferenceCount, await context.Preferences.CountAsync(TestContext.Current.CancellationToken));
}
[Fact]
public async Task ClearProfileImageAsync_WhenInMemoryImageHasTemporaryKey_RemovesPersistedImage()
{