MigrateDisplayPreferencesDb.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.IO;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. using Jellyfin.Data.Entities;
  6. using Jellyfin.Data.Enums;
  7. using Jellyfin.Server.Implementations;
  8. using MediaBrowser.Controller;
  9. using MediaBrowser.Model.Entities;
  10. using Microsoft.Extensions.Logging;
  11. using SQLitePCL.pretty;
  12. namespace Jellyfin.Server.Migrations.Routines
  13. {
  14. /// <summary>
  15. /// The migration routine for migrating the display preferences database to EF Core.
  16. /// </summary>
  17. public class MigrateDisplayPreferencesDb : IMigrationRoutine
  18. {
  19. private const string DbFilename = "displaypreferences.db";
  20. private readonly ILogger<MigrateDisplayPreferencesDb> _logger;
  21. private readonly IServerApplicationPaths _paths;
  22. private readonly JellyfinDbProvider _provider;
  23. private readonly JsonSerializerOptions _jsonOptions;
  24. /// <summary>
  25. /// Initializes a new instance of the <see cref="MigrateDisplayPreferencesDb"/> class.
  26. /// </summary>
  27. /// <param name="logger">The logger.</param>
  28. /// <param name="paths">The server application paths.</param>
  29. /// <param name="provider">The database provider.</param>
  30. public MigrateDisplayPreferencesDb(ILogger<MigrateDisplayPreferencesDb> logger, IServerApplicationPaths paths, JellyfinDbProvider provider)
  31. {
  32. _logger = logger;
  33. _paths = paths;
  34. _provider = provider;
  35. _jsonOptions = new JsonSerializerOptions();
  36. _jsonOptions.Converters.Add(new JsonStringEnumConverter());
  37. }
  38. /// <inheritdoc />
  39. public Guid Id => Guid.Parse("06387815-C3CC-421F-A888-FB5F9992BEA8");
  40. /// <inheritdoc />
  41. public string Name => "MigrateDisplayPreferencesDatabase";
  42. /// <inheritdoc />
  43. public bool PerformOnNewInstall => false;
  44. /// <inheritdoc />
  45. public void Perform()
  46. {
  47. HomeSectionType[] defaults =
  48. {
  49. HomeSectionType.SmallLibraryTiles,
  50. HomeSectionType.Resume,
  51. HomeSectionType.ResumeAudio,
  52. HomeSectionType.LiveTv,
  53. HomeSectionType.NextUp,
  54. HomeSectionType.LatestMedia,
  55. HomeSectionType.None,
  56. };
  57. var dbFilePath = Path.Combine(_paths.DataPath, DbFilename);
  58. using (var connection = SQLite3.Open(dbFilePath, ConnectionFlags.ReadOnly, null))
  59. {
  60. var dbContext = _provider.CreateContext();
  61. var results = connection.Query("SELECT * FROM userdisplaypreferences");
  62. foreach (var result in results)
  63. {
  64. var dto = JsonSerializer.Deserialize<DisplayPreferencesDto>(result[3].ToString(), _jsonOptions);
  65. var chromecastVersion = dto.CustomPrefs.TryGetValue("chromecastVersion", out var version)
  66. ? Enum.TryParse<ChromecastVersion>(version, true, out var parsed)
  67. ? parsed
  68. : ChromecastVersion.Stable
  69. : ChromecastVersion.Stable;
  70. var displayPreferences = new DisplayPreferences(result[2].ToString(), new Guid(result[1].ToBlob()))
  71. {
  72. ViewType = Enum.TryParse<ViewType>(dto.ViewType, true, out var viewType) ? viewType : (ViewType?)null,
  73. IndexBy = Enum.TryParse<IndexingKind>(dto.IndexBy, true, out var indexBy) ? indexBy : (IndexingKind?)null,
  74. ShowBackdrop = dto.ShowBackdrop,
  75. ShowSidebar = dto.ShowSidebar,
  76. SortBy = dto.SortBy,
  77. SortOrder = dto.SortOrder,
  78. RememberIndexing = dto.RememberIndexing,
  79. RememberSorting = dto.RememberSorting,
  80. ScrollDirection = dto.ScrollDirection,
  81. ChromecastVersion = chromecastVersion
  82. };
  83. for (int i = 0; i < 7; i++)
  84. {
  85. dto.CustomPrefs.TryGetValue("homesection" + i, out var homeSection);
  86. displayPreferences.HomeSections.Add(new HomeSection
  87. {
  88. Order = i,
  89. Type = Enum.TryParse<HomeSectionType>(homeSection, true, out var type) ? type : defaults[i]
  90. });
  91. }
  92. dbContext.Add(displayPreferences);
  93. }
  94. dbContext.SaveChanges();
  95. }
  96. try
  97. {
  98. File.Move(dbFilePath, dbFilePath + ".old");
  99. var journalPath = dbFilePath + "-journal";
  100. if (File.Exists(journalPath))
  101. {
  102. File.Move(journalPath, dbFilePath + ".old-journal");
  103. }
  104. }
  105. catch (IOException e)
  106. {
  107. _logger.LogError(e, "Error renaming legacy display preferences database to 'displaypreferences.db.old'");
  108. }
  109. }
  110. }
  111. }