| | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | 2 | |
|
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Collections.Immutable; |
| | 6 | | using System.Data; |
| | 7 | | using System.Diagnostics; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.IO; |
| | 10 | | using System.Linq; |
| | 11 | | using System.Text; |
| | 12 | | using Emby.Server.Implementations.Data; |
| | 13 | | using Jellyfin.Database.Implementations; |
| | 14 | | using Jellyfin.Database.Implementations.Entities; |
| | 15 | | using Jellyfin.Extensions; |
| | 16 | | using Jellyfin.Server.Implementations.Item; |
| | 17 | | using Jellyfin.Server.ServerSetupApp; |
| | 18 | | using MediaBrowser.Controller; |
| | 19 | | using MediaBrowser.Controller.Entities; |
| | 20 | | using MediaBrowser.Model.Entities; |
| | 21 | | using Microsoft.Data.Sqlite; |
| | 22 | | using Microsoft.EntityFrameworkCore; |
| | 23 | | using Microsoft.Extensions.Logging; |
| | 24 | | using BaseItemEntity = Jellyfin.Database.Implementations.Entities.BaseItemEntity; |
| | 25 | | using Chapter = Jellyfin.Database.Implementations.Entities.Chapter; |
| | 26 | |
|
| | 27 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// The migration routine for migrating the userdata database to EF Core. |
| | 31 | | /// </summary> |
| | 32 | | [JellyfinMigration("2025-04-20T20:00:00", nameof(MigrateLibraryDb))] |
| | 33 | | [JellyfinMigrationBackup(JellyfinDb = true, LegacyLibraryDb = true)] |
| | 34 | | internal class MigrateLibraryDb : IDatabaseMigrationRoutine |
| | 35 | | { |
| | 36 | | private const string DbFilename = "library.db"; |
| | 37 | |
|
| | 38 | | private readonly IStartupLogger _logger; |
| | 39 | | private readonly IServerApplicationPaths _paths; |
| | 40 | | private readonly IJellyfinDatabaseProvider _jellyfinDatabaseProvider; |
| | 41 | | private readonly IDbContextFactory<JellyfinDbContext> _provider; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Initializes a new instance of the <see cref="MigrateLibraryDb"/> class. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="startupLogger">The startup logger for Startup UI intigration.</param> |
| | 47 | | /// <param name="provider">The database provider.</param> |
| | 48 | | /// <param name="paths">The server application paths.</param> |
| | 49 | | /// <param name="jellyfinDatabaseProvider">The database provider for special access.</param> |
| | 50 | | public MigrateLibraryDb( |
| | 51 | | IStartupLogger<MigrateLibraryDb> startupLogger, |
| | 52 | | IDbContextFactory<JellyfinDbContext> provider, |
| | 53 | | IServerApplicationPaths paths, |
| | 54 | | IJellyfinDatabaseProvider jellyfinDatabaseProvider) |
| | 55 | | { |
| 0 | 56 | | _logger = startupLogger; |
| 0 | 57 | | _provider = provider; |
| 0 | 58 | | _paths = paths; |
| 0 | 59 | | _jellyfinDatabaseProvider = jellyfinDatabaseProvider; |
| 0 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <inheritdoc/> |
| | 63 | | public void Perform() |
| | 64 | | { |
| 0 | 65 | | _logger.LogInformation("Migrating the userdata from library.db may take a while, do not stop Jellyfin."); |
| | 66 | |
|
| 0 | 67 | | var dataPath = _paths.DataPath; |
| 0 | 68 | | var libraryDbPath = Path.Combine(dataPath, DbFilename); |
| 0 | 69 | | if (!File.Exists(libraryDbPath)) |
| | 70 | | { |
| 0 | 71 | | _logger.LogError("Cannot migrate {LibraryDb} as it does not exist..", libraryDbPath); |
| 0 | 72 | | return; |
| | 73 | | } |
| | 74 | |
|
| 0 | 75 | | using var connection = new SqliteConnection($"Filename={libraryDbPath};Mode=ReadOnly"); |
| | 76 | |
|
| 0 | 77 | | var fullOperationTimer = new Stopwatch(); |
| 0 | 78 | | fullOperationTimer.Start(); |
| | 79 | |
|
| 0 | 80 | | using (var operation = GetPreparedDbContext("Cleanup database")) |
| | 81 | | { |
| 0 | 82 | | operation.JellyfinDbContext.AttachmentStreamInfos.ExecuteDelete(); |
| 0 | 83 | | operation.JellyfinDbContext.BaseItems.ExecuteDelete(); |
| 0 | 84 | | operation.JellyfinDbContext.ItemValues.ExecuteDelete(); |
| 0 | 85 | | operation.JellyfinDbContext.UserData.ExecuteDelete(); |
| 0 | 86 | | operation.JellyfinDbContext.MediaStreamInfos.ExecuteDelete(); |
| 0 | 87 | | operation.JellyfinDbContext.Peoples.ExecuteDelete(); |
| 0 | 88 | | operation.JellyfinDbContext.PeopleBaseItemMap.ExecuteDelete(); |
| 0 | 89 | | operation.JellyfinDbContext.Chapters.ExecuteDelete(); |
| 0 | 90 | | operation.JellyfinDbContext.AncestorIds.ExecuteDelete(); |
| 0 | 91 | | } |
| | 92 | |
|
| | 93 | | // notify the other migration to just silently abort because the fix has been applied here already. |
| 0 | 94 | | ReseedFolderFlag.RerunGuardFlag = true; |
| | 95 | |
|
| 0 | 96 | | var legacyBaseItemWithUserKeys = new Dictionary<string, BaseItemEntity>(); |
| 0 | 97 | | connection.Open(); |
| | 98 | |
|
| 0 | 99 | | var baseItemIds = new HashSet<Guid>(); |
| 0 | 100 | | using (var operation = GetPreparedDbContext("Moving TypedBaseItem")) |
| | 101 | | { |
| | 102 | | const string typedBaseItemsQuery = |
| | 103 | | """ |
| | 104 | | SELECT guid, type, data, StartDate, EndDate, ChannelId, IsMovie, |
| | 105 | | IsSeries, EpisodeTitle, IsRepeat, CommunityRating, CustomRating, IndexNumber, IsLocked, PreferredMetadataLan |
| | 106 | | PreferredMetadataCountryCode, Width, Height, DateLastRefreshed, Name, Path, PremiereDate, Overview, ParentIn |
| | 107 | | ProductionYear, OfficialRating, ForcedSortName, RunTimeTicks, Size, DateCreated, DateModified, Genres, Paren |
| | 108 | | Audio, ExternalServiceId, IsInMixedFolder, DateLastSaved, LockedFields, Studios, Tags, TrailerTypes, Origina |
| | 109 | | DateLastMediaAdded, Album, LUFS, NormalizationGain, CriticRating, IsVirtualItem, SeriesName, UserDataKey, Se |
| | 110 | | PresentationUniqueKey, InheritedParentalRatingValue, ExternalSeriesId, Tagline, ProviderIds, Images, Product |
| | 111 | | ExtraType, Artists, AlbumArtists, ExternalId, SeriesPresentationUniqueKey, ShowId, OwnerId, MediaType, SortN |
| | 112 | | """; |
| 0 | 113 | | using (new TrackedMigrationStep("Loading TypedBaseItems", _logger)) |
| | 114 | | { |
| 0 | 115 | | foreach (SqliteDataReader dto in connection.Query(typedBaseItemsQuery)) |
| | 116 | | { |
| 0 | 117 | | var baseItem = GetItem(dto); |
| 0 | 118 | | operation.JellyfinDbContext.BaseItems.Add(baseItem.BaseItem); |
| 0 | 119 | | baseItemIds.Add(baseItem.BaseItem.Id); |
| 0 | 120 | | foreach (var dataKey in baseItem.LegacyUserDataKey) |
| | 121 | | { |
| 0 | 122 | | legacyBaseItemWithUserKeys[dataKey] = baseItem.BaseItem; |
| | 123 | | } |
| | 124 | | } |
| | 125 | | } |
| | 126 | |
|
| 0 | 127 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.BaseItems.Local.Count} BaseItem entrie |
| | 128 | | { |
| 0 | 129 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 130 | | } |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | using (var operation = GetPreparedDbContext("Moving ItemValues")) |
| | 134 | | { |
| | 135 | | // do not migrate inherited types as they are now properly mapped in search and lookup. |
| | 136 | | const string itemValueQuery = |
| | 137 | | """ |
| | 138 | | SELECT ItemId, Type, Value, CleanValue FROM ItemValues |
| | 139 | | WHERE Type <> 6 AND EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = ItemValues.I |
| | 140 | | """; |
| | 141 | |
|
| | 142 | | // EFCores local lookup sucks. We cannot use context.ItemValues.Local here because its just super slow. |
| 0 | 143 | | var localItems = new Dictionary<(int Type, string Value), (Database.Implementations.Entities.ItemValue ItemV |
| 0 | 144 | | using (new TrackedMigrationStep("Loading ItemValues", _logger)) |
| | 145 | | { |
| 0 | 146 | | foreach (SqliteDataReader dto in connection.Query(itemValueQuery)) |
| | 147 | | { |
| 0 | 148 | | var itemId = dto.GetGuid(0); |
| 0 | 149 | | var entity = GetItemValue(dto); |
| 0 | 150 | | var key = ((int)entity.Type, entity.Value); |
| 0 | 151 | | if (!localItems.TryGetValue(key, out var existing)) |
| | 152 | | { |
| 0 | 153 | | localItems[key] = existing = (entity, []); |
| | 154 | | } |
| | 155 | |
|
| 0 | 156 | | existing.ItemIds.Add(itemId); |
| | 157 | | } |
| | 158 | |
|
| 0 | 159 | | foreach (var item in localItems) |
| | 160 | | { |
| 0 | 161 | | operation.JellyfinDbContext.ItemValues.Add(item.Value.ItemValue); |
| 0 | 162 | | operation.JellyfinDbContext.ItemValuesMap.AddRange(item.Value.ItemIds.Distinct().Select(f => new Ite |
| 0 | 163 | | { |
| 0 | 164 | | Item = null!, |
| 0 | 165 | | ItemValue = null!, |
| 0 | 166 | | ItemId = f, |
| 0 | 167 | | ItemValueId = item.Value.ItemValue.ItemValueId |
| 0 | 168 | | })); |
| | 169 | | } |
| | 170 | | } |
| | 171 | |
|
| 0 | 172 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.ItemValues.Local.Count} ItemValues ent |
| | 173 | | { |
| 0 | 174 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 175 | | } |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | using (var operation = GetPreparedDbContext("Moving UserData")) |
| | 179 | | { |
| 0 | 180 | | var queryResult = connection.Query( |
| 0 | 181 | | """ |
| 0 | 182 | | SELECT key, userId, rating, played, playCount, isFavorite, playbackPositionTicks, lastPlayedDate, AudioStrea |
| 0 | 183 | |
|
| 0 | 184 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.UserDataKey = UserDatas.key) |
| 0 | 185 | | """); |
| | 186 | |
|
| 0 | 187 | | using (new TrackedMigrationStep("Loading UserData", _logger)) |
| | 188 | | { |
| 0 | 189 | | var users = operation.JellyfinDbContext.Users.AsNoTracking().ToArray(); |
| 0 | 190 | | var userIdBlacklist = new HashSet<int>(); |
| | 191 | |
|
| 0 | 192 | | foreach (var entity in queryResult) |
| | 193 | | { |
| 0 | 194 | | var userData = GetUserData(users, entity, userIdBlacklist, _logger); |
| 0 | 195 | | if (userData is null) |
| | 196 | | { |
| 0 | 197 | | var userDataId = entity.GetString(0); |
| 0 | 198 | | var internalUserId = entity.GetInt32(1); |
| | 199 | |
|
| 0 | 200 | | if (!userIdBlacklist.Contains(internalUserId)) |
| | 201 | | { |
| 0 | 202 | | _logger.LogError("Was not able to migrate user data with key {0} because its id {InternalId} |
| 0 | 203 | | userIdBlacklist.Add(internalUserId); |
| | 204 | | } |
| | 205 | |
|
| 0 | 206 | | continue; |
| | 207 | | } |
| | 208 | |
|
| 0 | 209 | | if (!legacyBaseItemWithUserKeys.TryGetValue(userData.CustomDataKey!, out var refItem)) |
| | 210 | | { |
| 0 | 211 | | _logger.LogError("Was not able to migrate user data with key {0} because it does not reference a |
| 0 | 212 | | continue; |
| | 213 | | } |
| | 214 | |
|
| 0 | 215 | | userData.ItemId = refItem.Id; |
| 0 | 216 | | operation.JellyfinDbContext.UserData.Add(userData); |
| | 217 | | } |
| | 218 | | } |
| | 219 | |
|
| 0 | 220 | | legacyBaseItemWithUserKeys.Clear(); |
| | 221 | |
|
| 0 | 222 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.UserData.Local.Count} UserData entries |
| | 223 | | { |
| 0 | 224 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 225 | | } |
| | 226 | | } |
| | 227 | |
|
| 0 | 228 | | using (var operation = GetPreparedDbContext("Moving MediaStreamInfos")) |
| | 229 | | { |
| | 230 | | const string mediaStreamQuery = |
| | 231 | | """ |
| | 232 | | SELECT ItemId, StreamIndex, StreamType, Codec, Language, ChannelLayout, Profile, AspectRatio, Path, |
| | 233 | | IsInterlaced, BitRate, Channels, SampleRate, IsDefault, IsForced, IsExternal, Height, Width, |
| | 234 | | AverageFrameRate, RealFrameRate, Level, PixelFormat, BitDepth, IsAnamorphic, RefFrames, CodecTag, |
| | 235 | | Comment, NalLengthSize, IsAvc, Title, TimeBase, CodecTimeBase, ColorPrimaries, ColorSpace, ColorTransfer, |
| | 236 | | DvVersionMajor, DvVersionMinor, DvProfile, DvLevel, RpuPresentFlag, ElPresentFlag, BlPresentFlag, DvBlSignal |
| | 237 | | FROM MediaStreams |
| | 238 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = MediaStreams.ItemId) |
| | 239 | | """; |
| | 240 | |
|
| 0 | 241 | | using (new TrackedMigrationStep("Loading MediaStreamInfos", _logger)) |
| | 242 | | { |
| 0 | 243 | | foreach (SqliteDataReader dto in connection.Query(mediaStreamQuery)) |
| | 244 | | { |
| 0 | 245 | | operation.JellyfinDbContext.MediaStreamInfos.Add(GetMediaStream(dto)); |
| | 246 | | } |
| | 247 | | } |
| | 248 | |
|
| 0 | 249 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.MediaStreamInfos.Local.Count} MediaStr |
| | 250 | | { |
| 0 | 251 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 252 | | } |
| | 253 | | } |
| | 254 | |
|
| 0 | 255 | | using (var operation = GetPreparedDbContext("Moving AttachmentStreamInfos")) |
| | 256 | | { |
| | 257 | | const string mediaAttachmentQuery = |
| | 258 | | """ |
| | 259 | | SELECT ItemId, AttachmentIndex, Codec, CodecTag, Comment, filename, MIMEType |
| | 260 | | FROM mediaattachments |
| | 261 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = mediaattachments.ItemId) |
| | 262 | | """; |
| | 263 | |
|
| 0 | 264 | | using (new TrackedMigrationStep("Loading AttachmentStreamInfos", _logger)) |
| | 265 | | { |
| 0 | 266 | | foreach (SqliteDataReader dto in connection.Query(mediaAttachmentQuery)) |
| | 267 | | { |
| 0 | 268 | | operation.JellyfinDbContext.AttachmentStreamInfos.Add(GetMediaAttachment(dto)); |
| | 269 | | } |
| | 270 | | } |
| | 271 | |
|
| 0 | 272 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.AttachmentStreamInfos.Local.Count} Att |
| | 273 | | { |
| 0 | 274 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 275 | | } |
| | 276 | | } |
| | 277 | |
|
| 0 | 278 | | using (var operation = GetPreparedDbContext("Moving People")) |
| | 279 | | { |
| | 280 | | const string personsQuery = |
| | 281 | | """ |
| | 282 | | SELECT ItemId, Name, Role, PersonType, SortOrder FROM People |
| | 283 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = People.ItemId) |
| | 284 | | """; |
| | 285 | |
|
| 0 | 286 | | var peopleCache = new Dictionary<string, (People Person, List<PeopleBaseItemMap> Items)>(); |
| | 287 | |
|
| 0 | 288 | | using (new TrackedMigrationStep("Loading People", _logger)) |
| | 289 | | { |
| 0 | 290 | | foreach (SqliteDataReader reader in connection.Query(personsQuery)) |
| | 291 | | { |
| 0 | 292 | | var itemId = reader.GetGuid(0); |
| 0 | 293 | | if (!baseItemIds.Contains(itemId)) |
| | 294 | | { |
| 0 | 295 | | _logger.LogError("Not saving person {0} because it's not in use by any BaseItem", reader.GetStri |
| 0 | 296 | | continue; |
| | 297 | | } |
| | 298 | |
|
| 0 | 299 | | var entity = GetPerson(reader); |
| 0 | 300 | | if (!peopleCache.TryGetValue(entity.Name, out var personCache)) |
| | 301 | | { |
| 0 | 302 | | peopleCache[entity.Name] = personCache = (entity, []); |
| | 303 | | } |
| | 304 | |
|
| 0 | 305 | | if (reader.TryGetString(2, out var role)) |
| | 306 | | { |
| | 307 | | } |
| | 308 | |
|
| 0 | 309 | | int? sortOrder = reader.IsDBNull(4) ? null : reader.GetInt32(4); |
| | 310 | |
|
| 0 | 311 | | personCache.Items.Add(new PeopleBaseItemMap() |
| 0 | 312 | | { |
| 0 | 313 | | Item = null!, |
| 0 | 314 | | ItemId = itemId, |
| 0 | 315 | | People = null!, |
| 0 | 316 | | PeopleId = personCache.Person.Id, |
| 0 | 317 | | ListOrder = sortOrder, |
| 0 | 318 | | SortOrder = sortOrder, |
| 0 | 319 | | Role = role |
| 0 | 320 | | }); |
| | 321 | | } |
| | 322 | |
|
| 0 | 323 | | baseItemIds.Clear(); |
| | 324 | |
|
| 0 | 325 | | foreach (var item in peopleCache) |
| | 326 | | { |
| 0 | 327 | | operation.JellyfinDbContext.Peoples.Add(item.Value.Person); |
| 0 | 328 | | operation.JellyfinDbContext.PeopleBaseItemMap.AddRange(item.Value.Items.DistinctBy(e => (e.ItemId, e |
| | 329 | | } |
| | 330 | |
|
| 0 | 331 | | peopleCache.Clear(); |
| 0 | 332 | | } |
| | 333 | |
|
| 0 | 334 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.Peoples.Local.Count} People entries an |
| | 335 | | { |
| 0 | 336 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 337 | | } |
| | 338 | | } |
| | 339 | |
|
| 0 | 340 | | using (var operation = GetPreparedDbContext("Moving Chapters")) |
| | 341 | | { |
| | 342 | | const string chapterQuery = |
| | 343 | | """ |
| | 344 | | SELECT ItemId,StartPositionTicks,Name,ImagePath,ImageDateModified,ChapterIndex from Chapters2 |
| | 345 | | WHERE EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = Chapters2.ItemId) |
| | 346 | | """; |
| | 347 | |
|
| 0 | 348 | | using (new TrackedMigrationStep("Loading Chapters", _logger)) |
| | 349 | | { |
| 0 | 350 | | foreach (SqliteDataReader dto in connection.Query(chapterQuery)) |
| | 351 | | { |
| 0 | 352 | | var chapter = GetChapter(dto); |
| 0 | 353 | | operation.JellyfinDbContext.Chapters.Add(chapter); |
| | 354 | | } |
| | 355 | | } |
| | 356 | |
|
| 0 | 357 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.Chapters.Local.Count} Chapters entries |
| | 358 | | { |
| 0 | 359 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 360 | | } |
| | 361 | | } |
| | 362 | |
|
| 0 | 363 | | using (var operation = GetPreparedDbContext("Moving AncestorIds")) |
| | 364 | | { |
| | 365 | | const string ancestorIdsQuery = |
| | 366 | | """ |
| | 367 | | SELECT ItemId, AncestorId, AncestorIdText FROM AncestorIds |
| | 368 | | WHERE |
| | 369 | | EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = AncestorIds.ItemId) |
| | 370 | | AND |
| | 371 | | EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.guid = AncestorIds.AncestorId) |
| | 372 | | """; |
| | 373 | |
|
| 0 | 374 | | using (new TrackedMigrationStep("Loading AncestorIds", _logger)) |
| | 375 | | { |
| 0 | 376 | | foreach (SqliteDataReader dto in connection.Query(ancestorIdsQuery)) |
| | 377 | | { |
| 0 | 378 | | var ancestorId = GetAncestorId(dto); |
| 0 | 379 | | operation.JellyfinDbContext.AncestorIds.Add(ancestorId); |
| | 380 | | } |
| | 381 | | } |
| | 382 | |
|
| 0 | 383 | | using (new TrackedMigrationStep($"Saving {operation.JellyfinDbContext.AncestorIds.Local.Count} AncestorId en |
| | 384 | | { |
| 0 | 385 | | operation.JellyfinDbContext.SaveChanges(); |
| 0 | 386 | | } |
| | 387 | | } |
| | 388 | |
|
| 0 | 389 | | connection.Close(); |
| | 390 | |
|
| 0 | 391 | | _logger.LogInformation("Migration of the Library.db done."); |
| 0 | 392 | | _logger.LogInformation("Migrating Library db took {0}.", fullOperationTimer.Elapsed); |
| | 393 | |
|
| 0 | 394 | | SqliteConnection.ClearAllPools(); |
| | 395 | |
|
| 0 | 396 | | _logger.LogInformation("Move {0} to {1}.", libraryDbPath, libraryDbPath + ".old"); |
| 0 | 397 | | File.Move(libraryDbPath, libraryDbPath + ".old", true); |
| 0 | 398 | | } |
| | 399 | |
|
| | 400 | | private DatabaseMigrationStep GetPreparedDbContext(string operationName) |
| | 401 | | { |
| 0 | 402 | | var dbContext = _provider.CreateDbContext(); |
| 0 | 403 | | dbContext.ChangeTracker.AutoDetectChangesEnabled = false; |
| 0 | 404 | | dbContext.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking; |
| 0 | 405 | | return new DatabaseMigrationStep(dbContext, operationName, _logger); |
| | 406 | | } |
| | 407 | |
|
| | 408 | | internal static UserData? GetUserData(User[] users, SqliteDataReader dto, HashSet<int> userIdBlacklist, ILogger logg |
| | 409 | | { |
| 0 | 410 | | var internalUserId = dto.GetInt32(1); |
| 0 | 411 | | if (userIdBlacklist.Contains(internalUserId)) |
| | 412 | | { |
| 0 | 413 | | return null; |
| | 414 | | } |
| | 415 | |
|
| 0 | 416 | | var user = users.FirstOrDefault(e => e.InternalId == internalUserId); |
| 0 | 417 | | if (user is null) |
| | 418 | | { |
| 0 | 419 | | userIdBlacklist.Add(internalUserId); |
| | 420 | |
|
| 0 | 421 | | logger.LogError("Tried to find user with index '{Idx}' but there are only '{MaxIdx}' users.", internalUserId |
| 0 | 422 | | return null; |
| | 423 | | } |
| | 424 | |
|
| 0 | 425 | | var oldKey = dto.GetString(0); |
| | 426 | |
|
| 0 | 427 | | return new UserData() |
| 0 | 428 | | { |
| 0 | 429 | | ItemId = Guid.NewGuid(), |
| 0 | 430 | | CustomDataKey = oldKey, |
| 0 | 431 | | UserId = user.Id, |
| 0 | 432 | | Rating = dto.IsDBNull(2) ? null : dto.GetDouble(2), |
| 0 | 433 | | Played = dto.GetBoolean(3), |
| 0 | 434 | | PlayCount = dto.GetInt32(4), |
| 0 | 435 | | IsFavorite = dto.GetBoolean(5), |
| 0 | 436 | | PlaybackPositionTicks = dto.GetInt64(6), |
| 0 | 437 | | LastPlayedDate = dto.IsDBNull(7) ? null : dto.GetDateTime(7), |
| 0 | 438 | | AudioStreamIndex = dto.IsDBNull(8) ? null : dto.GetInt32(8), |
| 0 | 439 | | SubtitleStreamIndex = dto.IsDBNull(9) ? null : dto.GetInt32(9), |
| 0 | 440 | | Likes = null, |
| 0 | 441 | | User = null!, |
| 0 | 442 | | Item = null! |
| 0 | 443 | | }; |
| | 444 | | } |
| | 445 | |
|
| | 446 | | private AncestorId GetAncestorId(SqliteDataReader reader) |
| | 447 | | { |
| 0 | 448 | | return new AncestorId() |
| 0 | 449 | | { |
| 0 | 450 | | ItemId = reader.GetGuid(0), |
| 0 | 451 | | ParentItemId = reader.GetGuid(1), |
| 0 | 452 | | Item = null!, |
| 0 | 453 | | ParentItem = null! |
| 0 | 454 | | }; |
| | 455 | | } |
| | 456 | |
|
| | 457 | | /// <summary> |
| | 458 | | /// Gets the chapter. |
| | 459 | | /// </summary> |
| | 460 | | /// <param name="reader">The reader.</param> |
| | 461 | | /// <returns>ChapterInfo.</returns> |
| | 462 | | private Chapter GetChapter(SqliteDataReader reader) |
| | 463 | | { |
| 0 | 464 | | var chapter = new Chapter |
| 0 | 465 | | { |
| 0 | 466 | | StartPositionTicks = reader.GetInt64(1), |
| 0 | 467 | | ChapterIndex = reader.GetInt32(5), |
| 0 | 468 | | Item = null!, |
| 0 | 469 | | ItemId = reader.GetGuid(0), |
| 0 | 470 | | }; |
| | 471 | |
|
| 0 | 472 | | if (reader.TryGetString(2, out var chapterName)) |
| | 473 | | { |
| 0 | 474 | | chapter.Name = chapterName; |
| | 475 | | } |
| | 476 | |
|
| 0 | 477 | | if (reader.TryGetString(3, out var imagePath)) |
| | 478 | | { |
| 0 | 479 | | chapter.ImagePath = imagePath; |
| | 480 | | } |
| | 481 | |
|
| 0 | 482 | | if (reader.TryReadDateTime(4, out var imageDateModified)) |
| | 483 | | { |
| 0 | 484 | | chapter.ImageDateModified = imageDateModified; |
| | 485 | | } |
| | 486 | |
|
| 0 | 487 | | return chapter; |
| | 488 | | } |
| | 489 | |
|
| | 490 | | private ItemValue GetItemValue(SqliteDataReader reader) |
| | 491 | | { |
| 0 | 492 | | return new ItemValue |
| 0 | 493 | | { |
| 0 | 494 | | ItemValueId = Guid.NewGuid(), |
| 0 | 495 | | Type = (ItemValueType)reader.GetInt32(1), |
| 0 | 496 | | Value = reader.GetString(2), |
| 0 | 497 | | CleanValue = reader.GetString(3), |
| 0 | 498 | | }; |
| | 499 | | } |
| | 500 | |
|
| | 501 | | private People GetPerson(SqliteDataReader reader) |
| | 502 | | { |
| 0 | 503 | | var item = new People |
| 0 | 504 | | { |
| 0 | 505 | | Id = Guid.NewGuid(), |
| 0 | 506 | | Name = reader.GetString(1), |
| 0 | 507 | | }; |
| | 508 | |
|
| 0 | 509 | | if (reader.TryGetString(3, out var type)) |
| | 510 | | { |
| 0 | 511 | | item.PersonType = type; |
| | 512 | | } |
| | 513 | |
|
| 0 | 514 | | return item; |
| | 515 | | } |
| | 516 | |
|
| | 517 | | /// <summary> |
| | 518 | | /// Gets the media stream. |
| | 519 | | /// </summary> |
| | 520 | | /// <param name="reader">The reader.</param> |
| | 521 | | /// <returns>MediaStream.</returns> |
| | 522 | | private MediaStreamInfo GetMediaStream(SqliteDataReader reader) |
| | 523 | | { |
| 0 | 524 | | var item = new MediaStreamInfo |
| 0 | 525 | | { |
| 0 | 526 | | StreamIndex = reader.GetInt32(1), |
| 0 | 527 | | StreamType = Enum.Parse<MediaStreamTypeEntity>(reader.GetString(2)), |
| 0 | 528 | | Item = null!, |
| 0 | 529 | | ItemId = reader.GetGuid(0), |
| 0 | 530 | | AspectRatio = null!, |
| 0 | 531 | | ChannelLayout = null!, |
| 0 | 532 | | Codec = null!, |
| 0 | 533 | | IsInterlaced = false, |
| 0 | 534 | | Language = null!, |
| 0 | 535 | | Path = null!, |
| 0 | 536 | | Profile = null!, |
| 0 | 537 | | }; |
| | 538 | |
|
| 0 | 539 | | if (reader.TryGetString(3, out var codec)) |
| | 540 | | { |
| 0 | 541 | | item.Codec = codec; |
| | 542 | | } |
| | 543 | |
|
| 0 | 544 | | if (reader.TryGetString(4, out var language)) |
| | 545 | | { |
| 0 | 546 | | item.Language = language; |
| | 547 | | } |
| | 548 | |
|
| 0 | 549 | | if (reader.TryGetString(5, out var channelLayout)) |
| | 550 | | { |
| 0 | 551 | | item.ChannelLayout = channelLayout; |
| | 552 | | } |
| | 553 | |
|
| 0 | 554 | | if (reader.TryGetString(6, out var profile)) |
| | 555 | | { |
| 0 | 556 | | item.Profile = profile; |
| | 557 | | } |
| | 558 | |
|
| 0 | 559 | | if (reader.TryGetString(7, out var aspectRatio)) |
| | 560 | | { |
| 0 | 561 | | item.AspectRatio = aspectRatio; |
| | 562 | | } |
| | 563 | |
|
| 0 | 564 | | if (reader.TryGetString(8, out var path)) |
| | 565 | | { |
| 0 | 566 | | item.Path = path; |
| | 567 | | } |
| | 568 | |
|
| 0 | 569 | | item.IsInterlaced = reader.GetBoolean(9); |
| | 570 | |
|
| 0 | 571 | | if (reader.TryGetInt32(10, out var bitrate)) |
| | 572 | | { |
| 0 | 573 | | item.BitRate = bitrate; |
| | 574 | | } |
| | 575 | |
|
| 0 | 576 | | if (reader.TryGetInt32(11, out var channels)) |
| | 577 | | { |
| 0 | 578 | | item.Channels = channels; |
| | 579 | | } |
| | 580 | |
|
| 0 | 581 | | if (reader.TryGetInt32(12, out var sampleRate)) |
| | 582 | | { |
| 0 | 583 | | item.SampleRate = sampleRate; |
| | 584 | | } |
| | 585 | |
|
| 0 | 586 | | item.IsDefault = reader.GetBoolean(13); |
| 0 | 587 | | item.IsForced = reader.GetBoolean(14); |
| 0 | 588 | | item.IsExternal = reader.GetBoolean(15); |
| | 589 | |
|
| 0 | 590 | | if (reader.TryGetInt32(16, out var width)) |
| | 591 | | { |
| 0 | 592 | | item.Width = width; |
| | 593 | | } |
| | 594 | |
|
| 0 | 595 | | if (reader.TryGetInt32(17, out var height)) |
| | 596 | | { |
| 0 | 597 | | item.Height = height; |
| | 598 | | } |
| | 599 | |
|
| 0 | 600 | | if (reader.TryGetSingle(18, out var averageFrameRate)) |
| | 601 | | { |
| 0 | 602 | | item.AverageFrameRate = averageFrameRate; |
| | 603 | | } |
| | 604 | |
|
| 0 | 605 | | if (reader.TryGetSingle(19, out var realFrameRate)) |
| | 606 | | { |
| 0 | 607 | | item.RealFrameRate = realFrameRate; |
| | 608 | | } |
| | 609 | |
|
| 0 | 610 | | if (reader.TryGetSingle(20, out var level)) |
| | 611 | | { |
| 0 | 612 | | item.Level = level; |
| | 613 | | } |
| | 614 | |
|
| 0 | 615 | | if (reader.TryGetString(21, out var pixelFormat)) |
| | 616 | | { |
| 0 | 617 | | item.PixelFormat = pixelFormat; |
| | 618 | | } |
| | 619 | |
|
| 0 | 620 | | if (reader.TryGetInt32(22, out var bitDepth)) |
| | 621 | | { |
| 0 | 622 | | item.BitDepth = bitDepth; |
| | 623 | | } |
| | 624 | |
|
| 0 | 625 | | if (reader.TryGetBoolean(23, out var isAnamorphic)) |
| | 626 | | { |
| 0 | 627 | | item.IsAnamorphic = isAnamorphic; |
| | 628 | | } |
| | 629 | |
|
| 0 | 630 | | if (reader.TryGetInt32(24, out var refFrames)) |
| | 631 | | { |
| 0 | 632 | | item.RefFrames = refFrames; |
| | 633 | | } |
| | 634 | |
|
| 0 | 635 | | if (reader.TryGetString(25, out var codecTag)) |
| | 636 | | { |
| 0 | 637 | | item.CodecTag = codecTag; |
| | 638 | | } |
| | 639 | |
|
| 0 | 640 | | if (reader.TryGetString(26, out var comment)) |
| | 641 | | { |
| 0 | 642 | | item.Comment = comment; |
| | 643 | | } |
| | 644 | |
|
| 0 | 645 | | if (reader.TryGetString(27, out var nalLengthSize)) |
| | 646 | | { |
| 0 | 647 | | item.NalLengthSize = nalLengthSize; |
| | 648 | | } |
| | 649 | |
|
| 0 | 650 | | if (reader.TryGetBoolean(28, out var isAVC)) |
| | 651 | | { |
| 0 | 652 | | item.IsAvc = isAVC; |
| | 653 | | } |
| | 654 | |
|
| 0 | 655 | | if (reader.TryGetString(29, out var title)) |
| | 656 | | { |
| 0 | 657 | | item.Title = title; |
| | 658 | | } |
| | 659 | |
|
| 0 | 660 | | if (reader.TryGetString(30, out var timeBase)) |
| | 661 | | { |
| 0 | 662 | | item.TimeBase = timeBase; |
| | 663 | | } |
| | 664 | |
|
| 0 | 665 | | if (reader.TryGetString(31, out var codecTimeBase)) |
| | 666 | | { |
| 0 | 667 | | item.CodecTimeBase = codecTimeBase; |
| | 668 | | } |
| | 669 | |
|
| 0 | 670 | | if (reader.TryGetString(32, out var colorPrimaries)) |
| | 671 | | { |
| 0 | 672 | | item.ColorPrimaries = colorPrimaries; |
| | 673 | | } |
| | 674 | |
|
| 0 | 675 | | if (reader.TryGetString(33, out var colorSpace)) |
| | 676 | | { |
| 0 | 677 | | item.ColorSpace = colorSpace; |
| | 678 | | } |
| | 679 | |
|
| 0 | 680 | | if (reader.TryGetString(34, out var colorTransfer)) |
| | 681 | | { |
| 0 | 682 | | item.ColorTransfer = colorTransfer; |
| | 683 | | } |
| | 684 | |
|
| 0 | 685 | | if (reader.TryGetInt32(35, out var dvVersionMajor)) |
| | 686 | | { |
| 0 | 687 | | item.DvVersionMajor = dvVersionMajor; |
| | 688 | | } |
| | 689 | |
|
| 0 | 690 | | if (reader.TryGetInt32(36, out var dvVersionMinor)) |
| | 691 | | { |
| 0 | 692 | | item.DvVersionMinor = dvVersionMinor; |
| | 693 | | } |
| | 694 | |
|
| 0 | 695 | | if (reader.TryGetInt32(37, out var dvProfile)) |
| | 696 | | { |
| 0 | 697 | | item.DvProfile = dvProfile; |
| | 698 | | } |
| | 699 | |
|
| 0 | 700 | | if (reader.TryGetInt32(38, out var dvLevel)) |
| | 701 | | { |
| 0 | 702 | | item.DvLevel = dvLevel; |
| | 703 | | } |
| | 704 | |
|
| 0 | 705 | | if (reader.TryGetInt32(39, out var rpuPresentFlag)) |
| | 706 | | { |
| 0 | 707 | | item.RpuPresentFlag = rpuPresentFlag; |
| | 708 | | } |
| | 709 | |
|
| 0 | 710 | | if (reader.TryGetInt32(40, out var elPresentFlag)) |
| | 711 | | { |
| 0 | 712 | | item.ElPresentFlag = elPresentFlag; |
| | 713 | | } |
| | 714 | |
|
| 0 | 715 | | if (reader.TryGetInt32(41, out var blPresentFlag)) |
| | 716 | | { |
| 0 | 717 | | item.BlPresentFlag = blPresentFlag; |
| | 718 | | } |
| | 719 | |
|
| 0 | 720 | | if (reader.TryGetInt32(42, out var dvBlSignalCompatibilityId)) |
| | 721 | | { |
| 0 | 722 | | item.DvBlSignalCompatibilityId = dvBlSignalCompatibilityId; |
| | 723 | | } |
| | 724 | |
|
| 0 | 725 | | item.IsHearingImpaired = reader.TryGetBoolean(43, out var result) && result; |
| | 726 | |
|
| | 727 | | // if (reader.TryGetInt32(44, out var rotation)) |
| | 728 | | // { |
| | 729 | | // item.Rotation = rotation; |
| | 730 | | // } |
| | 731 | |
|
| 0 | 732 | | return item; |
| | 733 | | } |
| | 734 | |
|
| | 735 | | /// <summary> |
| | 736 | | /// Gets the attachment. |
| | 737 | | /// </summary> |
| | 738 | | /// <param name="reader">The reader.</param> |
| | 739 | | /// <returns>MediaAttachment.</returns> |
| | 740 | | private AttachmentStreamInfo GetMediaAttachment(SqliteDataReader reader) |
| | 741 | | { |
| 0 | 742 | | var item = new AttachmentStreamInfo |
| 0 | 743 | | { |
| 0 | 744 | | Index = reader.GetInt32(1), |
| 0 | 745 | | Item = null!, |
| 0 | 746 | | ItemId = reader.GetGuid(0), |
| 0 | 747 | | }; |
| | 748 | |
|
| 0 | 749 | | if (reader.TryGetString(2, out var codec)) |
| | 750 | | { |
| 0 | 751 | | item.Codec = codec; |
| | 752 | | } |
| | 753 | |
|
| 0 | 754 | | if (reader.TryGetString(3, out var codecTag)) |
| | 755 | | { |
| 0 | 756 | | item.CodecTag = codecTag; |
| | 757 | | } |
| | 758 | |
|
| 0 | 759 | | if (reader.TryGetString(4, out var comment)) |
| | 760 | | { |
| 0 | 761 | | item.Comment = comment; |
| | 762 | | } |
| | 763 | |
|
| 0 | 764 | | if (reader.TryGetString(5, out var fileName)) |
| | 765 | | { |
| 0 | 766 | | item.Filename = fileName; |
| | 767 | | } |
| | 768 | |
|
| 0 | 769 | | if (reader.TryGetString(6, out var mimeType)) |
| | 770 | | { |
| 0 | 771 | | item.MimeType = mimeType; |
| | 772 | | } |
| | 773 | |
|
| 0 | 774 | | return item; |
| | 775 | | } |
| | 776 | |
|
| | 777 | | private (BaseItemEntity BaseItem, string[] LegacyUserDataKey) GetItem(SqliteDataReader reader) |
| | 778 | | { |
| 0 | 779 | | var entity = new BaseItemEntity() |
| 0 | 780 | | { |
| 0 | 781 | | Id = reader.GetGuid(0), |
| 0 | 782 | | Type = reader.GetString(1), |
| 0 | 783 | | }; |
| | 784 | |
|
| 0 | 785 | | var index = 2; |
| | 786 | |
|
| 0 | 787 | | if (reader.TryGetString(index++, out var data)) |
| | 788 | | { |
| 0 | 789 | | entity.Data = data; |
| | 790 | | } |
| | 791 | |
|
| 0 | 792 | | if (reader.TryReadDateTime(index++, out var startDate)) |
| | 793 | | { |
| 0 | 794 | | entity.StartDate = startDate; |
| | 795 | | } |
| | 796 | |
|
| 0 | 797 | | if (reader.TryReadDateTime(index++, out var endDate)) |
| | 798 | | { |
| 0 | 799 | | entity.EndDate = endDate; |
| | 800 | | } |
| | 801 | |
|
| 0 | 802 | | if (reader.TryGetGuid(index++, out var guid)) |
| | 803 | | { |
| 0 | 804 | | entity.ChannelId = guid; |
| | 805 | | } |
| | 806 | |
|
| 0 | 807 | | if (reader.TryGetBoolean(index++, out var isMovie)) |
| | 808 | | { |
| 0 | 809 | | entity.IsMovie = isMovie; |
| | 810 | | } |
| | 811 | |
|
| 0 | 812 | | if (reader.TryGetBoolean(index++, out var isSeries)) |
| | 813 | | { |
| 0 | 814 | | entity.IsSeries = isSeries; |
| | 815 | | } |
| | 816 | |
|
| 0 | 817 | | if (reader.TryGetString(index++, out var episodeTitle)) |
| | 818 | | { |
| 0 | 819 | | entity.EpisodeTitle = episodeTitle; |
| | 820 | | } |
| | 821 | |
|
| 0 | 822 | | if (reader.TryGetBoolean(index++, out var isRepeat)) |
| | 823 | | { |
| 0 | 824 | | entity.IsRepeat = isRepeat; |
| | 825 | | } |
| | 826 | |
|
| 0 | 827 | | if (reader.TryGetSingle(index++, out var communityRating)) |
| | 828 | | { |
| 0 | 829 | | entity.CommunityRating = communityRating; |
| | 830 | | } |
| | 831 | |
|
| 0 | 832 | | if (reader.TryGetString(index++, out var customRating)) |
| | 833 | | { |
| 0 | 834 | | entity.CustomRating = customRating; |
| | 835 | | } |
| | 836 | |
|
| 0 | 837 | | if (reader.TryGetInt32(index++, out var indexNumber)) |
| | 838 | | { |
| 0 | 839 | | entity.IndexNumber = indexNumber; |
| | 840 | | } |
| | 841 | |
|
| 0 | 842 | | if (reader.TryGetBoolean(index++, out var isLocked)) |
| | 843 | | { |
| 0 | 844 | | entity.IsLocked = isLocked; |
| | 845 | | } |
| | 846 | |
|
| 0 | 847 | | if (reader.TryGetString(index++, out var preferredMetadataLanguage)) |
| | 848 | | { |
| 0 | 849 | | entity.PreferredMetadataLanguage = preferredMetadataLanguage; |
| | 850 | | } |
| | 851 | |
|
| 0 | 852 | | if (reader.TryGetString(index++, out var preferredMetadataCountryCode)) |
| | 853 | | { |
| 0 | 854 | | entity.PreferredMetadataCountryCode = preferredMetadataCountryCode; |
| | 855 | | } |
| | 856 | |
|
| 0 | 857 | | if (reader.TryGetInt32(index++, out var width)) |
| | 858 | | { |
| 0 | 859 | | entity.Width = width; |
| | 860 | | } |
| | 861 | |
|
| 0 | 862 | | if (reader.TryGetInt32(index++, out var height)) |
| | 863 | | { |
| 0 | 864 | | entity.Height = height; |
| | 865 | | } |
| | 866 | |
|
| 0 | 867 | | if (reader.TryReadDateTime(index++, out var dateLastRefreshed)) |
| | 868 | | { |
| 0 | 869 | | entity.DateLastRefreshed = dateLastRefreshed; |
| | 870 | | } |
| | 871 | |
|
| 0 | 872 | | if (reader.TryGetString(index++, out var name)) |
| | 873 | | { |
| 0 | 874 | | entity.Name = name; |
| | 875 | | } |
| | 876 | |
|
| 0 | 877 | | if (reader.TryGetString(index++, out var restorePath)) |
| | 878 | | { |
| 0 | 879 | | entity.Path = restorePath; |
| | 880 | | } |
| | 881 | |
|
| 0 | 882 | | if (reader.TryReadDateTime(index++, out var premiereDate)) |
| | 883 | | { |
| 0 | 884 | | entity.PremiereDate = premiereDate; |
| | 885 | | } |
| | 886 | |
|
| 0 | 887 | | if (reader.TryGetString(index++, out var overview)) |
| | 888 | | { |
| 0 | 889 | | entity.Overview = overview; |
| | 890 | | } |
| | 891 | |
|
| 0 | 892 | | if (reader.TryGetInt32(index++, out var parentIndexNumber)) |
| | 893 | | { |
| 0 | 894 | | entity.ParentIndexNumber = parentIndexNumber; |
| | 895 | | } |
| | 896 | |
|
| 0 | 897 | | if (reader.TryGetInt32(index++, out var productionYear)) |
| | 898 | | { |
| 0 | 899 | | entity.ProductionYear = productionYear; |
| | 900 | | } |
| | 901 | |
|
| 0 | 902 | | if (reader.TryGetString(index++, out var officialRating)) |
| | 903 | | { |
| 0 | 904 | | entity.OfficialRating = officialRating; |
| | 905 | | } |
| | 906 | |
|
| 0 | 907 | | if (reader.TryGetString(index++, out var forcedSortName)) |
| | 908 | | { |
| 0 | 909 | | entity.ForcedSortName = forcedSortName; |
| | 910 | | } |
| | 911 | |
|
| 0 | 912 | | if (reader.TryGetInt64(index++, out var runTimeTicks)) |
| | 913 | | { |
| 0 | 914 | | entity.RunTimeTicks = runTimeTicks; |
| | 915 | | } |
| | 916 | |
|
| 0 | 917 | | if (reader.TryGetInt64(index++, out var size)) |
| | 918 | | { |
| 0 | 919 | | entity.Size = size; |
| | 920 | | } |
| | 921 | |
|
| 0 | 922 | | if (reader.TryReadDateTime(index++, out var dateCreated)) |
| | 923 | | { |
| 0 | 924 | | entity.DateCreated = dateCreated; |
| | 925 | | } |
| | 926 | |
|
| 0 | 927 | | if (reader.TryReadDateTime(index++, out var dateModified)) |
| | 928 | | { |
| 0 | 929 | | entity.DateModified = dateModified; |
| | 930 | | } |
| | 931 | |
|
| 0 | 932 | | if (reader.TryGetString(index++, out var genres)) |
| | 933 | | { |
| 0 | 934 | | entity.Genres = genres; |
| | 935 | | } |
| | 936 | |
|
| 0 | 937 | | if (reader.TryGetGuid(index++, out var parentId)) |
| | 938 | | { |
| 0 | 939 | | entity.ParentId = parentId; |
| | 940 | | } |
| | 941 | |
|
| 0 | 942 | | if (reader.TryGetGuid(index++, out var topParentId)) |
| | 943 | | { |
| 0 | 944 | | entity.TopParentId = topParentId; |
| | 945 | | } |
| | 946 | |
|
| 0 | 947 | | if (reader.TryGetString(index++, out var audioString) && Enum.TryParse<ProgramAudioEntity>(audioString, out var |
| | 948 | | { |
| 0 | 949 | | entity.Audio = audioType; |
| | 950 | | } |
| | 951 | |
|
| 0 | 952 | | if (reader.TryGetString(index++, out var serviceName)) |
| | 953 | | { |
| 0 | 954 | | entity.ExternalServiceId = serviceName; |
| | 955 | | } |
| | 956 | |
|
| 0 | 957 | | if (reader.TryGetBoolean(index++, out var isInMixedFolder)) |
| | 958 | | { |
| 0 | 959 | | entity.IsInMixedFolder = isInMixedFolder; |
| | 960 | | } |
| | 961 | |
|
| 0 | 962 | | if (reader.TryReadDateTime(index++, out var dateLastSaved)) |
| | 963 | | { |
| 0 | 964 | | entity.DateLastSaved = dateLastSaved; |
| | 965 | | } |
| | 966 | |
|
| 0 | 967 | | if (reader.TryGetString(index++, out var lockedFields)) |
| | 968 | | { |
| 0 | 969 | | entity.LockedFields = lockedFields.Split('|').Select(Enum.Parse<MetadataField>) |
| 0 | 970 | | .Select(e => new BaseItemMetadataField() |
| 0 | 971 | | { |
| 0 | 972 | | Id = (int)e, |
| 0 | 973 | | Item = entity, |
| 0 | 974 | | ItemId = entity.Id |
| 0 | 975 | | }) |
| 0 | 976 | | .ToArray(); |
| | 977 | | } |
| | 978 | |
|
| 0 | 979 | | if (reader.TryGetString(index++, out var studios)) |
| | 980 | | { |
| 0 | 981 | | entity.Studios = studios; |
| | 982 | | } |
| | 983 | |
|
| 0 | 984 | | if (reader.TryGetString(index++, out var tags)) |
| | 985 | | { |
| 0 | 986 | | entity.Tags = tags; |
| | 987 | | } |
| | 988 | |
|
| 0 | 989 | | if (reader.TryGetString(index++, out var trailerTypes)) |
| | 990 | | { |
| 0 | 991 | | entity.TrailerTypes = trailerTypes.Split('|').Select(Enum.Parse<TrailerType>) |
| 0 | 992 | | .Select(e => new BaseItemTrailerType() |
| 0 | 993 | | { |
| 0 | 994 | | Id = (int)e, |
| 0 | 995 | | Item = entity, |
| 0 | 996 | | ItemId = entity.Id |
| 0 | 997 | | }) |
| 0 | 998 | | .ToArray(); |
| | 999 | | } |
| | 1000 | |
|
| 0 | 1001 | | if (reader.TryGetString(index++, out var originalTitle)) |
| | 1002 | | { |
| 0 | 1003 | | entity.OriginalTitle = originalTitle; |
| | 1004 | | } |
| | 1005 | |
|
| 0 | 1006 | | if (reader.TryGetString(index++, out var primaryVersionId)) |
| | 1007 | | { |
| 0 | 1008 | | entity.PrimaryVersionId = primaryVersionId; |
| | 1009 | | } |
| | 1010 | |
|
| 0 | 1011 | | if (reader.TryReadDateTime(index++, out var dateLastMediaAdded)) |
| | 1012 | | { |
| 0 | 1013 | | entity.DateLastMediaAdded = dateLastMediaAdded; |
| | 1014 | | } |
| | 1015 | |
|
| 0 | 1016 | | if (reader.TryGetString(index++, out var album)) |
| | 1017 | | { |
| 0 | 1018 | | entity.Album = album; |
| | 1019 | | } |
| | 1020 | |
|
| 0 | 1021 | | if (reader.TryGetSingle(index++, out var lUFS)) |
| | 1022 | | { |
| 0 | 1023 | | entity.LUFS = lUFS; |
| | 1024 | | } |
| | 1025 | |
|
| 0 | 1026 | | if (reader.TryGetSingle(index++, out var normalizationGain)) |
| | 1027 | | { |
| 0 | 1028 | | entity.NormalizationGain = normalizationGain; |
| | 1029 | | } |
| | 1030 | |
|
| 0 | 1031 | | if (reader.TryGetSingle(index++, out var criticRating)) |
| | 1032 | | { |
| 0 | 1033 | | entity.CriticRating = criticRating; |
| | 1034 | | } |
| | 1035 | |
|
| 0 | 1036 | | if (reader.TryGetBoolean(index++, out var isVirtualItem)) |
| | 1037 | | { |
| 0 | 1038 | | entity.IsVirtualItem = isVirtualItem; |
| | 1039 | | } |
| | 1040 | |
|
| 0 | 1041 | | if (reader.TryGetString(index++, out var seriesName)) |
| | 1042 | | { |
| 0 | 1043 | | entity.SeriesName = seriesName; |
| | 1044 | | } |
| | 1045 | |
|
| 0 | 1046 | | var userDataKeys = new List<string>(); |
| 0 | 1047 | | if (reader.TryGetString(index++, out var directUserDataKey)) |
| | 1048 | | { |
| 0 | 1049 | | userDataKeys.Add(directUserDataKey); |
| | 1050 | | } |
| | 1051 | |
|
| 0 | 1052 | | if (reader.TryGetString(index++, out var seasonName)) |
| | 1053 | | { |
| 0 | 1054 | | entity.SeasonName = seasonName; |
| | 1055 | | } |
| | 1056 | |
|
| 0 | 1057 | | if (reader.TryGetGuid(index++, out var seasonId)) |
| | 1058 | | { |
| 0 | 1059 | | entity.SeasonId = seasonId; |
| | 1060 | | } |
| | 1061 | |
|
| 0 | 1062 | | if (reader.TryGetGuid(index++, out var seriesId)) |
| | 1063 | | { |
| 0 | 1064 | | entity.SeriesId = seriesId; |
| | 1065 | | } |
| | 1066 | |
|
| 0 | 1067 | | if (reader.TryGetString(index++, out var presentationUniqueKey)) |
| | 1068 | | { |
| 0 | 1069 | | entity.PresentationUniqueKey = presentationUniqueKey; |
| | 1070 | | } |
| | 1071 | |
|
| 0 | 1072 | | if (reader.TryGetInt32(index++, out var parentalRating)) |
| | 1073 | | { |
| 0 | 1074 | | entity.InheritedParentalRatingValue = parentalRating; |
| | 1075 | | } |
| | 1076 | |
|
| 0 | 1077 | | if (reader.TryGetString(index++, out var externalSeriesId)) |
| | 1078 | | { |
| 0 | 1079 | | entity.ExternalSeriesId = externalSeriesId; |
| | 1080 | | } |
| | 1081 | |
|
| 0 | 1082 | | if (reader.TryGetString(index++, out var tagLine)) |
| | 1083 | | { |
| 0 | 1084 | | entity.Tagline = tagLine; |
| | 1085 | | } |
| | 1086 | |
|
| 0 | 1087 | | if (reader.TryGetString(index++, out var providerIds)) |
| | 1088 | | { |
| 0 | 1089 | | entity.Provider = providerIds.Split('|').Select(e => e.Split("=")) |
| 0 | 1090 | | .Select(e => new BaseItemProvider() |
| 0 | 1091 | | { |
| 0 | 1092 | | Item = null!, |
| 0 | 1093 | | ProviderId = e[0], |
| 0 | 1094 | | ProviderValue = e[1] |
| 0 | 1095 | | }).ToArray(); |
| | 1096 | | } |
| | 1097 | |
|
| 0 | 1098 | | if (reader.TryGetString(index++, out var imageInfos)) |
| | 1099 | | { |
| 0 | 1100 | | entity.Images = DeserializeImages(imageInfos).Select(f => Map(entity.Id, f)).ToArray(); |
| | 1101 | | } |
| | 1102 | |
|
| 0 | 1103 | | if (reader.TryGetString(index++, out var productionLocations)) |
| | 1104 | | { |
| 0 | 1105 | | entity.ProductionLocations = productionLocations; |
| | 1106 | | } |
| | 1107 | |
|
| 0 | 1108 | | if (reader.TryGetString(index++, out var extraIds)) |
| | 1109 | | { |
| 0 | 1110 | | entity.ExtraIds = extraIds; |
| | 1111 | | } |
| | 1112 | |
|
| 0 | 1113 | | if (reader.TryGetInt32(index++, out var totalBitrate)) |
| | 1114 | | { |
| 0 | 1115 | | entity.TotalBitrate = totalBitrate; |
| | 1116 | | } |
| | 1117 | |
|
| 0 | 1118 | | if (reader.TryGetString(index++, out var extraTypeString) && Enum.TryParse<BaseItemExtraType>(extraTypeString, o |
| | 1119 | | { |
| 0 | 1120 | | entity.ExtraType = extraType; |
| | 1121 | | } |
| | 1122 | |
|
| 0 | 1123 | | if (reader.TryGetString(index++, out var artists)) |
| | 1124 | | { |
| 0 | 1125 | | entity.Artists = artists; |
| | 1126 | | } |
| | 1127 | |
|
| 0 | 1128 | | if (reader.TryGetString(index++, out var albumArtists)) |
| | 1129 | | { |
| 0 | 1130 | | entity.AlbumArtists = albumArtists; |
| | 1131 | | } |
| | 1132 | |
|
| 0 | 1133 | | if (reader.TryGetString(index++, out var externalId)) |
| | 1134 | | { |
| 0 | 1135 | | entity.ExternalId = externalId; |
| | 1136 | | } |
| | 1137 | |
|
| 0 | 1138 | | if (reader.TryGetString(index++, out var seriesPresentationUniqueKey)) |
| | 1139 | | { |
| 0 | 1140 | | entity.SeriesPresentationUniqueKey = seriesPresentationUniqueKey; |
| | 1141 | | } |
| | 1142 | |
|
| 0 | 1143 | | if (reader.TryGetString(index++, out var showId)) |
| | 1144 | | { |
| 0 | 1145 | | entity.ShowId = showId; |
| | 1146 | | } |
| | 1147 | |
|
| 0 | 1148 | | if (reader.TryGetString(index++, out var ownerId)) |
| | 1149 | | { |
| 0 | 1150 | | entity.OwnerId = ownerId; |
| | 1151 | | } |
| | 1152 | |
|
| 0 | 1153 | | if (reader.TryGetString(index++, out var mediaType)) |
| | 1154 | | { |
| 0 | 1155 | | entity.MediaType = mediaType; |
| | 1156 | | } |
| | 1157 | |
|
| 0 | 1158 | | if (reader.TryGetString(index++, out var sortName)) |
| | 1159 | | { |
| 0 | 1160 | | entity.SortName = sortName; |
| | 1161 | | } |
| | 1162 | |
|
| 0 | 1163 | | if (reader.TryGetString(index++, out var cleanName)) |
| | 1164 | | { |
| 0 | 1165 | | entity.CleanName = cleanName; |
| | 1166 | | } |
| | 1167 | |
|
| 0 | 1168 | | if (reader.TryGetString(index++, out var unratedType)) |
| | 1169 | | { |
| 0 | 1170 | | entity.UnratedType = unratedType; |
| | 1171 | | } |
| | 1172 | |
|
| 0 | 1173 | | if (reader.TryGetBoolean(index++, out var isFolder)) |
| | 1174 | | { |
| 0 | 1175 | | entity.IsFolder = isFolder; |
| | 1176 | | } |
| | 1177 | |
|
| 0 | 1178 | | var baseItem = BaseItemRepository.DeserializeBaseItem(entity, _logger, null, false); |
| 0 | 1179 | | var dataKeys = baseItem.GetUserDataKeys(); |
| 0 | 1180 | | userDataKeys.AddRange(dataKeys); |
| | 1181 | |
|
| 0 | 1182 | | return (entity, userDataKeys.ToArray()); |
| | 1183 | | } |
| | 1184 | |
|
| | 1185 | | private static BaseItemImageInfo Map(Guid baseItemId, ItemImageInfo e) |
| | 1186 | | { |
| 0 | 1187 | | return new BaseItemImageInfo() |
| 0 | 1188 | | { |
| 0 | 1189 | | ItemId = baseItemId, |
| 0 | 1190 | | Id = Guid.NewGuid(), |
| 0 | 1191 | | Path = e.Path, |
| 0 | 1192 | | Blurhash = e.BlurHash != null ? Encoding.UTF8.GetBytes(e.BlurHash) : null, |
| 0 | 1193 | | DateModified = e.DateModified, |
| 0 | 1194 | | Height = e.Height, |
| 0 | 1195 | | Width = e.Width, |
| 0 | 1196 | | ImageType = (ImageInfoImageType)e.Type, |
| 0 | 1197 | | Item = null! |
| 0 | 1198 | | }; |
| | 1199 | | } |
| | 1200 | |
|
| | 1201 | | internal ItemImageInfo[] DeserializeImages(string value) |
| | 1202 | | { |
| 0 | 1203 | | if (string.IsNullOrWhiteSpace(value)) |
| | 1204 | | { |
| 0 | 1205 | | return Array.Empty<ItemImageInfo>(); |
| | 1206 | | } |
| | 1207 | |
|
| | 1208 | | // TODO The following is an ugly performance optimization, but it's extremely unlikely that the data in the data |
| 0 | 1209 | | var valueSpan = value.AsSpan(); |
| 0 | 1210 | | var count = valueSpan.Count('|') + 1; |
| | 1211 | |
|
| 0 | 1212 | | var position = 0; |
| 0 | 1213 | | var result = new ItemImageInfo[count]; |
| 0 | 1214 | | foreach (var part in valueSpan.Split('|')) |
| | 1215 | | { |
| 0 | 1216 | | var image = ItemImageInfoFromValueString(part); |
| | 1217 | |
|
| 0 | 1218 | | if (image is not null) |
| | 1219 | | { |
| 0 | 1220 | | result[position++] = image; |
| | 1221 | | } |
| | 1222 | | } |
| | 1223 | |
|
| 0 | 1224 | | if (position == count) |
| | 1225 | | { |
| 0 | 1226 | | return result; |
| | 1227 | | } |
| | 1228 | |
|
| 0 | 1229 | | if (position == 0) |
| | 1230 | | { |
| 0 | 1231 | | return Array.Empty<ItemImageInfo>(); |
| | 1232 | | } |
| | 1233 | |
|
| | 1234 | | // Extremely unlikely, but somehow one or more of the image strings were malformed. Cut the array. |
| 0 | 1235 | | return result[..position]; |
| | 1236 | | } |
| | 1237 | |
|
| | 1238 | | internal ItemImageInfo? ItemImageInfoFromValueString(ReadOnlySpan<char> value) |
| | 1239 | | { |
| | 1240 | | const char Delimiter = '*'; |
| | 1241 | |
|
| 0 | 1242 | | var nextSegment = value.IndexOf(Delimiter); |
| 0 | 1243 | | if (nextSegment == -1) |
| | 1244 | | { |
| 0 | 1245 | | return null; |
| | 1246 | | } |
| | 1247 | |
|
| 0 | 1248 | | ReadOnlySpan<char> path = value[..nextSegment]; |
| 0 | 1249 | | value = value[(nextSegment + 1)..]; |
| 0 | 1250 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1251 | | if (nextSegment == -1) |
| | 1252 | | { |
| 0 | 1253 | | return null; |
| | 1254 | | } |
| | 1255 | |
|
| 0 | 1256 | | ReadOnlySpan<char> dateModified = value[..nextSegment]; |
| 0 | 1257 | | value = value[(nextSegment + 1)..]; |
| 0 | 1258 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1259 | | if (nextSegment == -1) |
| | 1260 | | { |
| 0 | 1261 | | nextSegment = value.Length; |
| | 1262 | | } |
| | 1263 | |
|
| 0 | 1264 | | ReadOnlySpan<char> imageType = value[..nextSegment]; |
| | 1265 | |
|
| 0 | 1266 | | var image = new ItemImageInfo |
| 0 | 1267 | | { |
| 0 | 1268 | | Path = path.ToString() |
| 0 | 1269 | | }; |
| | 1270 | |
|
| 0 | 1271 | | if (long.TryParse(dateModified, CultureInfo.InvariantCulture, out var ticks) |
| 0 | 1272 | | && ticks >= DateTime.MinValue.Ticks |
| 0 | 1273 | | && ticks <= DateTime.MaxValue.Ticks) |
| | 1274 | | { |
| 0 | 1275 | | image.DateModified = new DateTime(ticks, DateTimeKind.Utc); |
| | 1276 | | } |
| | 1277 | | else |
| | 1278 | | { |
| 0 | 1279 | | return null; |
| | 1280 | | } |
| | 1281 | |
|
| 0 | 1282 | | if (Enum.TryParse(imageType, true, out ImageType type)) |
| | 1283 | | { |
| 0 | 1284 | | image.Type = type; |
| | 1285 | | } |
| | 1286 | | else |
| | 1287 | | { |
| 0 | 1288 | | return null; |
| | 1289 | | } |
| | 1290 | |
|
| | 1291 | | // Optional parameters: width*height*blurhash |
| 0 | 1292 | | if (nextSegment + 1 < value.Length - 1) |
| | 1293 | | { |
| 0 | 1294 | | value = value[(nextSegment + 1)..]; |
| 0 | 1295 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1296 | | if (nextSegment == -1 || nextSegment == value.Length) |
| | 1297 | | { |
| 0 | 1298 | | return image; |
| | 1299 | | } |
| | 1300 | |
|
| 0 | 1301 | | ReadOnlySpan<char> widthSpan = value[..nextSegment]; |
| | 1302 | |
|
| 0 | 1303 | | value = value[(nextSegment + 1)..]; |
| 0 | 1304 | | nextSegment = value.IndexOf(Delimiter); |
| 0 | 1305 | | if (nextSegment == -1) |
| | 1306 | | { |
| 0 | 1307 | | nextSegment = value.Length; |
| | 1308 | | } |
| | 1309 | |
|
| 0 | 1310 | | ReadOnlySpan<char> heightSpan = value[..nextSegment]; |
| | 1311 | |
|
| 0 | 1312 | | if (int.TryParse(widthSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var width) |
| 0 | 1313 | | && int.TryParse(heightSpan, NumberStyles.Integer, CultureInfo.InvariantCulture, out var height)) |
| | 1314 | | { |
| 0 | 1315 | | image.Width = width; |
| 0 | 1316 | | image.Height = height; |
| | 1317 | | } |
| | 1318 | |
|
| 0 | 1319 | | if (nextSegment < value.Length - 1) |
| | 1320 | | { |
| 0 | 1321 | | value = value[(nextSegment + 1)..]; |
| 0 | 1322 | | var length = value.Length; |
| | 1323 | |
|
| 0 | 1324 | | Span<char> blurHashSpan = stackalloc char[length]; |
| 0 | 1325 | | for (int i = 0; i < length; i++) |
| | 1326 | | { |
| 0 | 1327 | | var c = value[i]; |
| 0 | 1328 | | blurHashSpan[i] = c switch |
| 0 | 1329 | | { |
| 0 | 1330 | | '/' => Delimiter, |
| 0 | 1331 | | '\\' => '|', |
| 0 | 1332 | | _ => c |
| 0 | 1333 | | }; |
| | 1334 | | } |
| | 1335 | |
|
| 0 | 1336 | | image.BlurHash = new string(blurHashSpan); |
| | 1337 | | } |
| | 1338 | | } |
| | 1339 | |
|
| 0 | 1340 | | return image; |
| | 1341 | | } |
| | 1342 | |
|
| | 1343 | | private class TrackedMigrationStep : IDisposable |
| | 1344 | | { |
| | 1345 | | private readonly string _operationName; |
| | 1346 | | private readonly ILogger _logger; |
| | 1347 | | private readonly Stopwatch _operationTimer; |
| | 1348 | | private bool _disposed; |
| | 1349 | |
|
| | 1350 | | public TrackedMigrationStep(string operationName, ILogger logger) |
| | 1351 | | { |
| 0 | 1352 | | _operationName = operationName; |
| 0 | 1353 | | _logger = logger; |
| 0 | 1354 | | _operationTimer = Stopwatch.StartNew(); |
| 0 | 1355 | | logger.LogInformation("Start {OperationName}", operationName); |
| 0 | 1356 | | } |
| | 1357 | |
|
| | 1358 | | public bool Disposed |
| | 1359 | | { |
| 0 | 1360 | | get => _disposed; |
| 0 | 1361 | | set => _disposed = value; |
| | 1362 | | } |
| | 1363 | |
|
| | 1364 | | public virtual void Dispose() |
| | 1365 | | { |
| 0 | 1366 | | if (Disposed) |
| | 1367 | | { |
| 0 | 1368 | | return; |
| | 1369 | | } |
| | 1370 | |
|
| 0 | 1371 | | Disposed = true; |
| 0 | 1372 | | _logger.LogInformation("{OperationName} took '{Time}'", _operationName, _operationTimer.Elapsed); |
| 0 | 1373 | | } |
| | 1374 | | } |
| | 1375 | |
|
| | 1376 | | private sealed class DatabaseMigrationStep : TrackedMigrationStep |
| | 1377 | | { |
| 0 | 1378 | | public DatabaseMigrationStep(JellyfinDbContext jellyfinDbContext, string operationName, ILogger logger) : base(o |
| | 1379 | | { |
| | 1380 | | JellyfinDbContext = jellyfinDbContext; |
| 0 | 1381 | | } |
| | 1382 | |
|
| | 1383 | | public JellyfinDbContext JellyfinDbContext { get; } |
| | 1384 | |
|
| | 1385 | | public override void Dispose() |
| | 1386 | | { |
| 0 | 1387 | | if (Disposed) |
| | 1388 | | { |
| 0 | 1389 | | return; |
| | 1390 | | } |
| | 1391 | |
|
| 0 | 1392 | | JellyfinDbContext.Dispose(); |
| 0 | 1393 | | base.Dispose(); |
| 0 | 1394 | | } |
| | 1395 | | } |
| | 1396 | | } |