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