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