| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using Jellyfin.Database.Implementations; |
| | | 10 | | using Jellyfin.Database.Implementations.Entities; |
| | | 11 | | using Jellyfin.Database.Implementations.Enums; |
| | | 12 | | using Jellyfin.Server.ServerSetupApp; |
| | | 13 | | using MediaBrowser.Controller.Configuration; |
| | | 14 | | using MediaBrowser.Controller.Entities; |
| | | 15 | | using MediaBrowser.Controller.Library; |
| | | 16 | | using MediaBrowser.Model.IO; |
| | | 17 | | using Microsoft.EntityFrameworkCore; |
| | | 18 | | using Microsoft.Extensions.Logging; |
| | | 19 | | |
| | | 20 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Moves the views whose id used to be derived from their localized name onto their name independent id. |
| | | 24 | | /// </summary> |
| | | 25 | | [JellyfinMigration("2026-08-25T20:00:00", nameof(ConsolidateLocalizedUserViews))] |
| | | 26 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 27 | | internal class ConsolidateLocalizedUserViews : IAsyncMigrationRoutine |
| | | 28 | | { |
| | | 29 | | private readonly IStartupLogger<ConsolidateLocalizedUserViews> _logger; |
| | | 30 | | private readonly ILibraryManager _libraryManager; |
| | | 31 | | private readonly IServerConfigurationManager _configurationManager; |
| | | 32 | | private readonly IFileSystem _fileSystem; |
| | | 33 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="ConsolidateLocalizedUserViews"/> class. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="logger">The startup logger.</param> |
| | | 39 | | /// <param name="libraryManager">The library manager.</param> |
| | | 40 | | /// <param name="configurationManager">The server configuration manager.</param> |
| | | 41 | | /// <param name="fileSystem">The file system.</param> |
| | | 42 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 43 | | public ConsolidateLocalizedUserViews( |
| | | 44 | | IStartupLogger<ConsolidateLocalizedUserViews> logger, |
| | | 45 | | ILibraryManager libraryManager, |
| | | 46 | | IServerConfigurationManager configurationManager, |
| | | 47 | | IFileSystem fileSystem, |
| | | 48 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | | 49 | | { |
| | 3 | 50 | | _logger = logger; |
| | 3 | 51 | | _libraryManager = libraryManager; |
| | 3 | 52 | | _configurationManager = configurationManager; |
| | 3 | 53 | | _fileSystem = fileSystem; |
| | 3 | 54 | | _dbProvider = dbProvider; |
| | 3 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <inheritdoc /> |
| | | 58 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 59 | | { |
| | | 60 | | // The Live TV view is the one that hurts: every channel and program is parented to it, so a |
| | | 61 | | // translation update or a change of UI culture used to leave them behind under a view nothing |
| | | 62 | | // looks up any more. |
| | 3 | 63 | | var views = _libraryManager.GetItemList(new InternalItemsQuery |
| | 3 | 64 | | { |
| | 3 | 65 | | IncludeItemTypes = [BaseItemKind.UserView] |
| | 3 | 66 | | }).OfType<UserView>().Where(view => view.ViewType.HasValue).ToArray(); |
| | | 67 | | |
| | 3 | 68 | | if (views.Length == 0) |
| | | 69 | | { |
| | 0 | 70 | | return; |
| | | 71 | | } |
| | | 72 | | |
| | 3 | 73 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 3 | 74 | | await using (dbContext.ConfigureAwait(false)) |
| | | 75 | | { |
| | 12 | 76 | | foreach (var group in views.GroupBy(view => view.ViewType!.Value)) |
| | | 77 | | { |
| | 3 | 78 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 79 | | |
| | 3 | 80 | | var viewType = group.Key; |
| | 3 | 81 | | var folderName = _fileSystem.GetValidFilename(viewType.ToString()); |
| | 3 | 82 | | var path = Path.Combine(_configurationManager.ApplicationPaths.InternalMetadataPath, "views", folderName |
| | | 83 | | |
| | | 84 | | // Only the views created for a view type as a whole are named after it. The per user and |
| | | 85 | | // per parent ones get a folder of their own, and carry no children to lose. Match on the |
| | | 86 | | // folder rather than the whole path so a metadata directory that has since moved still |
| | | 87 | | // lines up. |
| | 3 | 88 | | var candidates = group |
| | 3 | 89 | | .Where(view => !string.IsNullOrEmpty(view.Path) |
| | 3 | 90 | | && string.Equals(Path.GetFileName(view.Path.TrimEnd(Path.DirectorySeparatorChar)), folderName, S |
| | 3 | 91 | | .ToArray(); |
| | 3 | 92 | | if (candidates.Length == 0) |
| | | 93 | | { |
| | | 94 | | continue; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | // Mirrors LibraryManager.GetNamedView(name, viewType, sortName). |
| | 3 | 98 | | var canonicalId = _libraryManager.GetNewItemId(path + "_namedview_" + viewType.ToString(), typeof(UserVi |
| | | 99 | | |
| | 3 | 100 | | var stale = candidates.Where(view => !view.Id.Equals(canonicalId)).ToArray(); |
| | 3 | 101 | | if (stale.Length == 0) |
| | | 102 | | { |
| | | 103 | | continue; |
| | | 104 | | } |
| | | 105 | | |
| | 3 | 106 | | await ConsolidateAsync(dbContext, viewType, path, canonicalId, candidates, stale, cancellationToken).Con |
| | | 107 | | } |
| | | 108 | | } |
| | 3 | 109 | | } |
| | | 110 | | |
| | | 111 | | private async Task ConsolidateAsync( |
| | | 112 | | JellyfinDbContext dbContext, |
| | | 113 | | CollectionType viewType, |
| | | 114 | | string path, |
| | | 115 | | Guid canonicalId, |
| | | 116 | | IReadOnlyList<UserView> candidates, |
| | | 117 | | IReadOnlyList<UserView> stale, |
| | | 118 | | CancellationToken cancellationToken) |
| | | 119 | | { |
| | 3 | 120 | | var staleIds = stale.Select(view => view.Id).ToArray(); |
| | 3 | 121 | | Guid? newParentId = canonicalId; |
| | 3 | 122 | | var sourceId = Guid.Empty; |
| | | 123 | | |
| | 3 | 124 | | if (!candidates.Any(view => view.Id.Equals(canonicalId))) |
| | | 125 | | { |
| | | 126 | | // Whichever of the old views the items ended up under is the one worth keeping, so give the |
| | | 127 | | // canonical id a copy of it. |
| | 3 | 128 | | var source = await PickSourceAsync(dbContext, stale, staleIds, cancellationToken).ConfigureAwait(false); |
| | 3 | 129 | | sourceId = source.Id; |
| | | 130 | | |
| | 3 | 131 | | _libraryManager.CreateItem( |
| | 3 | 132 | | new UserView |
| | 3 | 133 | | { |
| | 3 | 134 | | Path = path, |
| | 3 | 135 | | Id = canonicalId, |
| | 3 | 136 | | DateCreated = source.DateCreated, |
| | 3 | 137 | | DateModified = source.DateModified, |
| | 3 | 138 | | Name = source.Name, |
| | 3 | 139 | | ViewType = viewType, |
| | 3 | 140 | | ForcedSortName = source.ForcedSortName |
| | 3 | 141 | | }, |
| | 3 | 142 | | null); |
| | | 143 | | } |
| | | 144 | | |
| | 3 | 145 | | var reparented = await dbContext.BaseItems |
| | 3 | 146 | | .Where(e => e.ParentId.HasValue) |
| | 3 | 147 | | .WhereOneOrMany(staleIds, e => e.ParentId!.Value) |
| | 3 | 148 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.ParentId, newParentId), cancellationToken) |
| | 3 | 149 | | .ConfigureAwait(false); |
| | | 150 | | |
| | 3 | 151 | | await dbContext.BaseItems |
| | 3 | 152 | | .Where(e => e.TopParentId.HasValue) |
| | 3 | 153 | | .WhereOneOrMany(staleIds, e => e.TopParentId!.Value) |
| | 3 | 154 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.TopParentId, newParentId), cancellationToken) |
| | 3 | 155 | | .ConfigureAwait(false); |
| | | 156 | | |
| | 3 | 157 | | await MoveAncestorsAsync(dbContext, canonicalId, staleIds, cancellationToken).ConfigureAwait(false); |
| | 3 | 158 | | await MoveUserSettingsAsync(dbContext, canonicalId, sourceId, staleIds, cancellationToken).ConfigureAwait(false) |
| | 3 | 159 | | await MoveRemainingReferencesAsync(dbContext, newParentId, staleIds, cancellationToken).ConfigureAwait(false); |
| | | 160 | | |
| | | 161 | | // Nothing points at them any more, and BaseItems cascades on ParentId, so this has to come last. |
| | 3 | 162 | | await dbContext.BaseItems |
| | 3 | 163 | | .WhereOneOrMany(staleIds, e => e.Id) |
| | 3 | 164 | | .ExecuteDeleteAsync(cancellationToken) |
| | 3 | 165 | | .ConfigureAwait(false); |
| | | 166 | | |
| | 3 | 167 | | _logger.LogInformation( |
| | 3 | 168 | | "Moved {Reparented} items and dropped {Stale} stale {ViewType} views in favour of {CanonicalId}", |
| | 3 | 169 | | reparented, |
| | 3 | 170 | | staleIds.Length, |
| | 3 | 171 | | viewType, |
| | 3 | 172 | | canonicalId); |
| | 3 | 173 | | } |
| | | 174 | | |
| | | 175 | | private static async Task MoveRemainingReferencesAsync( |
| | | 176 | | JellyfinDbContext dbContext, |
| | | 177 | | Guid? canonicalId, |
| | | 178 | | IReadOnlyList<Guid> staleIds, |
| | | 179 | | CancellationToken cancellationToken) |
| | | 180 | | { |
| | 3 | 181 | | await dbContext.BaseItems |
| | 3 | 182 | | .Where(e => e.OwnerId.HasValue) |
| | 3 | 183 | | .WhereOneOrMany(staleIds, e => e.OwnerId!.Value) |
| | 3 | 184 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.OwnerId, canonicalId), cancellationToken) |
| | 3 | 185 | | .ConfigureAwait(false); |
| | | 186 | | |
| | | 187 | | // Keyed by (ParentId, SortOrder), so these cannot be repointed onto the canonical view |
| | | 188 | | // without risking a collision, and a view listing linked children is meaningless anyway. |
| | 3 | 189 | | await dbContext.LinkedChildren |
| | 3 | 190 | | .WhereOneOrMany(staleIds, e => e.ParentId) |
| | 3 | 191 | | .ExecuteDeleteAsync(cancellationToken) |
| | 3 | 192 | | .ConfigureAwait(false); |
| | | 193 | | |
| | 3 | 194 | | await dbContext.LinkedChildren |
| | 3 | 195 | | .WhereOneOrMany(staleIds, e => e.ChildId) |
| | 3 | 196 | | .ExecuteDeleteAsync(cancellationToken) |
| | 3 | 197 | | .ConfigureAwait(false); |
| | 3 | 198 | | } |
| | | 199 | | |
| | | 200 | | private async Task<UserView> PickSourceAsync( |
| | | 201 | | JellyfinDbContext dbContext, |
| | | 202 | | IReadOnlyList<UserView> stale, |
| | | 203 | | IReadOnlyList<Guid> staleIds, |
| | | 204 | | CancellationToken cancellationToken) |
| | | 205 | | { |
| | 3 | 206 | | var childCounts = await dbContext.BaseItems |
| | 3 | 207 | | .Where(e => e.ParentId.HasValue) |
| | 3 | 208 | | .WhereOneOrMany(staleIds, e => e.ParentId!.Value) |
| | 3 | 209 | | .GroupBy(e => e.ParentId!.Value) |
| | 3 | 210 | | .Select(g => new { ParentId = g.Key, Count = g.Count() }) |
| | 3 | 211 | | .ToDictionaryAsync(e => e.ParentId, e => e.Count, cancellationToken) |
| | 3 | 212 | | .ConfigureAwait(false); |
| | | 213 | | |
| | 3 | 214 | | return stale |
| | 3 | 215 | | .OrderByDescending(view => childCounts.GetValueOrDefault(view.Id)) |
| | 3 | 216 | | .ThenBy(view => view.DateCreated) |
| | 3 | 217 | | .First(); |
| | 3 | 218 | | } |
| | | 219 | | |
| | | 220 | | private static async Task MoveUserSettingsAsync( |
| | | 221 | | JellyfinDbContext dbContext, |
| | | 222 | | Guid canonicalId, |
| | | 223 | | Guid sourceId, |
| | | 224 | | IReadOnlyList<Guid> staleIds, |
| | | 225 | | CancellationToken cancellationToken) |
| | | 226 | | { |
| | | 227 | | // Everything below is keyed by the view's id, and a view holding no children still holds the |
| | | 228 | | // ordering it was given and whether it was hidden. Only the view that was promoted can hand |
| | | 229 | | // those over - the rest would collide on the one row per user, item and client - so the others |
| | | 230 | | // are dropped instead. |
| | 3 | 231 | | var dropped = staleIds.Where(id => !id.Equals(sourceId)).ToArray(); |
| | | 232 | | |
| | 3 | 233 | | if (!sourceId.Equals(Guid.Empty)) |
| | | 234 | | { |
| | 3 | 235 | | var moved = new[] { sourceId }; |
| | | 236 | | |
| | 3 | 237 | | await dbContext.DisplayPreferences |
| | 3 | 238 | | .WhereOneOrMany(moved, e => e.ItemId) |
| | 3 | 239 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.ItemId, canonicalId), cancellationToken) |
| | 3 | 240 | | .ConfigureAwait(false); |
| | | 241 | | |
| | 3 | 242 | | await dbContext.ItemDisplayPreferences |
| | 3 | 243 | | .WhereOneOrMany(moved, e => e.ItemId) |
| | 3 | 244 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.ItemId, canonicalId), cancellationToken) |
| | 3 | 245 | | .ConfigureAwait(false); |
| | | 246 | | |
| | 3 | 247 | | await dbContext.CustomItemDisplayPreferences |
| | 3 | 248 | | .WhereOneOrMany(moved, e => e.ItemId) |
| | 3 | 249 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.ItemId, canonicalId), cancellationToken) |
| | 3 | 250 | | .ConfigureAwait(false); |
| | 3 | 251 | | } |
| | | 252 | | |
| | 3 | 253 | | if (dropped.Length > 0) |
| | | 254 | | { |
| | 0 | 255 | | await dbContext.DisplayPreferences.WhereOneOrMany(dropped, e => e.ItemId).ExecuteDeleteAsync(cancellationTok |
| | 0 | 256 | | await dbContext.ItemDisplayPreferences.WhereOneOrMany(dropped, e => e.ItemId).ExecuteDeleteAsync(cancellatio |
| | 0 | 257 | | await dbContext.CustomItemDisplayPreferences.WhereOneOrMany(dropped, e => e.ItemId).ExecuteDeleteAsync(cance |
| | | 258 | | } |
| | | 259 | | |
| | 3 | 260 | | var stale = staleIds.ToHashSet(); |
| | 3 | 261 | | var preferences = await dbContext.Preferences |
| | 3 | 262 | | .Where(e => e.Kind == PreferenceKind.OrderedViews || e.Kind == PreferenceKind.MyMediaExcludes) |
| | 3 | 263 | | .ToListAsync(cancellationToken) |
| | 3 | 264 | | .ConfigureAwait(false); |
| | | 265 | | |
| | 3 | 266 | | var changed = false; |
| | | 267 | | |
| | 6 | 268 | | foreach (var preference in preferences) |
| | | 269 | | { |
| | 0 | 270 | | var values = preference.Value.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntr |
| | 0 | 271 | | var rewritten = new List<string>(values.Length); |
| | 0 | 272 | | var seen = new HashSet<Guid>(); |
| | 0 | 273 | | var touched = false; |
| | | 274 | | |
| | 0 | 275 | | foreach (var value in values) |
| | | 276 | | { |
| | | 277 | | // Clients write these in both the dashed and the plain form, so compare them parsed. |
| | 0 | 278 | | if (!Guid.TryParse(value, out var parsed)) |
| | | 279 | | { |
| | 0 | 280 | | rewritten.Add(value); |
| | 0 | 281 | | continue; |
| | | 282 | | } |
| | | 283 | | |
| | 0 | 284 | | var isStale = stale.Contains(parsed); |
| | 0 | 285 | | if (isStale) |
| | | 286 | | { |
| | 0 | 287 | | parsed = canonicalId; |
| | 0 | 288 | | touched = true; |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | // The same view can be listed twice once both of its ids point at the same place. |
| | 0 | 292 | | if (!seen.Add(parsed)) |
| | | 293 | | { |
| | | 294 | | continue; |
| | | 295 | | } |
| | | 296 | | |
| | 0 | 297 | | rewritten.Add(isStale |
| | 0 | 298 | | ? parsed.ToString(value.Contains('-', StringComparison.Ordinal) ? "D" : "N", CultureInfo.InvariantCu |
| | 0 | 299 | | : value); |
| | | 300 | | } |
| | | 301 | | |
| | 0 | 302 | | if (!touched) |
| | | 303 | | { |
| | | 304 | | continue; |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | preference.Value = string.Join(',', rewritten); |
| | 0 | 308 | | changed = true; |
| | | 309 | | } |
| | | 310 | | |
| | 3 | 311 | | if (changed) |
| | | 312 | | { |
| | 0 | 313 | | await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | | 314 | | } |
| | 3 | 315 | | } |
| | | 316 | | |
| | | 317 | | private static async Task MoveAncestorsAsync( |
| | | 318 | | JellyfinDbContext dbContext, |
| | | 319 | | Guid canonicalId, |
| | | 320 | | IReadOnlyList<Guid> staleIds, |
| | | 321 | | CancellationToken cancellationToken) |
| | | 322 | | { |
| | | 323 | | // Ancestry recorded against items that no longer exist is dead weight. |
| | 3 | 324 | | var items = await dbContext.AncestorIds |
| | 3 | 325 | | .WhereOneOrMany(staleIds, e => e.ParentItemId) |
| | 3 | 326 | | .Where(e => dbContext.BaseItems.Any(item => item.Id.Equals(e.ItemId))) |
| | 3 | 327 | | .Select(e => e.ItemId) |
| | 3 | 328 | | .Distinct() |
| | 3 | 329 | | .ToListAsync(cancellationToken) |
| | 3 | 330 | | .ConfigureAwait(false); |
| | | 331 | | |
| | 3 | 332 | | await dbContext.AncestorIds |
| | 3 | 333 | | .WhereOneOrMany(staleIds, e => e.ParentItemId) |
| | 3 | 334 | | .ExecuteDeleteAsync(cancellationToken) |
| | 3 | 335 | | .ConfigureAwait(false); |
| | | 336 | | |
| | 3 | 337 | | if (items.Count == 0) |
| | | 338 | | { |
| | 2 | 339 | | return; |
| | | 340 | | } |
| | | 341 | | |
| | | 342 | | // The pair is the primary key, so anything already recorded against the canonical view stays put. |
| | 1 | 343 | | var existing = await dbContext.AncestorIds |
| | 1 | 344 | | .Where(e => e.ParentItemId.Equals(canonicalId)) |
| | 1 | 345 | | .Select(e => e.ItemId) |
| | 1 | 346 | | .ToListAsync(cancellationToken) |
| | 1 | 347 | | .ConfigureAwait(false); |
| | | 348 | | |
| | 4 | 349 | | foreach (var itemId in items.Except(existing)) |
| | | 350 | | { |
| | 1 | 351 | | dbContext.AncestorIds.Add(new AncestorId |
| | 1 | 352 | | { |
| | 1 | 353 | | ItemId = itemId, |
| | 1 | 354 | | ParentItemId = canonicalId, |
| | 1 | 355 | | Item = null!, |
| | 1 | 356 | | ParentItem = null! |
| | 1 | 357 | | }); |
| | | 358 | | } |
| | | 359 | | |
| | 1 | 360 | | await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | 3 | 361 | | } |
| | | 362 | | } |