| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Database.Implementations; |
| | | 7 | | using Jellyfin.Server.Implementations.Item; |
| | | 8 | | using Jellyfin.Server.ServerSetupApp; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Collapses conflicting user data rows so every row an item holds for a user says the same thing. |
| | | 16 | | /// </summary> |
| | | 17 | | [JellyfinMigration("2026-09-12T12:00:00", nameof(HarmonizeConflictingUserData))] |
| | | 18 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 19 | | public class HarmonizeConflictingUserData : IAsyncMigrationRoutine |
| | | 20 | | { |
| | | 21 | | private const int BatchSize = 500; |
| | | 22 | | |
| | | 23 | | private readonly IStartupLogger<HarmonizeConflictingUserData> _logger; |
| | | 24 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Initializes a new instance of the <see cref="HarmonizeConflictingUserData"/> class. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="logger">The startup logger.</param> |
| | | 30 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | | 31 | | public HarmonizeConflictingUserData( |
| | | 32 | | IStartupLogger<HarmonizeConflictingUserData> logger, |
| | | 33 | | IDbContextFactory<JellyfinDbContext> dbContextFactory) |
| | | 34 | | { |
| | 2 | 35 | | _logger = logger; |
| | 2 | 36 | | _dbContextFactory = dbContextFactory; |
| | 2 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <inheritdoc/> |
| | | 40 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 41 | | { |
| | 2 | 42 | | var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 2 | 43 | | await using (context.ConfigureAwait(false)) |
| | | 44 | | { |
| | 2 | 45 | | var conflicts = await context.UserData |
| | 2 | 46 | | .Where(e => !e.ItemId.Equals(BaseItemRepository.PlaceholderId)) |
| | 2 | 47 | | .GroupBy(e => new { e.ItemId, e.UserId }) |
| | 2 | 48 | | .Where(g => g.Count() > 1 |
| | 2 | 49 | | && (g.Min(e => e.PlaybackPositionTicks) != g.Max(e => e.PlaybackPositionTicks) |
| | 2 | 50 | | || g.Min(e => e.PlayCount) != g.Max(e => e.PlayCount) |
| | 2 | 51 | | || g.Min(e => e.Played ? 1 : 0) != g.Max(e => e.Played ? 1 : 0) |
| | 2 | 52 | | || g.Min(e => e.IsFavorite ? 1 : 0) != g.Max(e => e.IsFavorite ? 1 : 0) |
| | 2 | 53 | | || g.Min(e => e.LastPlayedDate) != g.Max(e => e.LastPlayedDate))) |
| | 2 | 54 | | .Select(g => new { g.Key.ItemId, g.Key.UserId }) |
| | 2 | 55 | | .ToListAsync(cancellationToken) |
| | 2 | 56 | | .ConfigureAwait(false); |
| | | 57 | | |
| | 2 | 58 | | if (conflicts.Count == 0) |
| | | 59 | | { |
| | 1 | 60 | | _logger.LogInformation("No conflicting user data found."); |
| | 1 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | var conflictKeys = conflicts.ConvertAll(e => new ConflictKey(e.ItemId, e.UserId)); |
| | | 65 | | |
| | 1 | 66 | | _logger.LogInformation("Harmonizing user data for {Count} item/user combinations.", conflictKeys.Count); |
| | | 67 | | |
| | 1 | 68 | | var harmonized = 0; |
| | 4 | 69 | | foreach (var batch in conflictKeys.Chunk(BatchSize)) |
| | | 70 | | { |
| | 1 | 71 | | harmonized += await HarmonizeBatchAsync(context, batch, cancellationToken).ConfigureAwait(false); |
| | | 72 | | } |
| | | 73 | | |
| | 1 | 74 | | _logger.LogInformation("Updated {Count} user data rows.", harmonized); |
| | | 75 | | } |
| | 2 | 76 | | } |
| | | 77 | | |
| | | 78 | | private static async Task<int> HarmonizeBatchAsync(JellyfinDbContext context, ConflictKey[] batch, CancellationToken |
| | | 79 | | { |
| | 1 | 80 | | var itemIds = batch.Select(e => e.ItemId).Distinct().ToArray(); |
| | 1 | 81 | | var wanted = batch.ToHashSet(); |
| | | 82 | | |
| | 1 | 83 | | var rows = await context.UserData |
| | 1 | 84 | | .WhereOneOrMany(itemIds, e => e.ItemId) |
| | 1 | 85 | | .ToListAsync(cancellationToken) |
| | 1 | 86 | | .ConfigureAwait(false); |
| | | 87 | | |
| | 1 | 88 | | var updated = 0; |
| | 4 | 89 | | foreach (var group in rows.GroupBy(e => new ConflictKey(e.ItemId, e.UserId))) |
| | | 90 | | { |
| | 1 | 91 | | if (!wanted.Contains(group.Key)) |
| | | 92 | | { |
| | | 93 | | continue; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | // The most recent play is the state the user last produced; the others are fossils of |
| | | 97 | | // earlier incarnations of the same item. |
| | 1 | 98 | | var winner = group |
| | 1 | 99 | | .OrderByDescending(e => e.LastPlayedDate) |
| | 1 | 100 | | .ThenByDescending(e => e.PlayCount) |
| | 1 | 101 | | .ThenByDescending(e => e.PlaybackPositionTicks) |
| | 1 | 102 | | .First(); |
| | | 103 | | |
| | 6 | 104 | | foreach (var row in group) |
| | | 105 | | { |
| | 2 | 106 | | if (ReferenceEquals(row, winner)) |
| | | 107 | | { |
| | | 108 | | continue; |
| | | 109 | | } |
| | | 110 | | |
| | 1 | 111 | | row.AudioStreamIndex = winner.AudioStreamIndex; |
| | 1 | 112 | | row.IsFavorite = winner.IsFavorite; |
| | 1 | 113 | | row.LastPlayedDate = winner.LastPlayedDate; |
| | 1 | 114 | | row.Likes = winner.Likes; |
| | 1 | 115 | | row.PlaybackPositionTicks = winner.PlaybackPositionTicks; |
| | 1 | 116 | | row.PlayCount = winner.PlayCount; |
| | 1 | 117 | | row.Played = winner.Played; |
| | 1 | 118 | | row.Rating = winner.Rating; |
| | 1 | 119 | | row.SubtitleStreamIndex = winner.SubtitleStreamIndex; |
| | 1 | 120 | | updated++; |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | 1 | 124 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | 1 | 125 | | context.ChangeTracker.Clear(); |
| | | 126 | | |
| | 1 | 127 | | return updated; |
| | 1 | 128 | | } |
| | | 129 | | |
| | | 130 | | private readonly record struct ConflictKey(Guid ItemId, Guid UserId); |
| | | 131 | | } |