RemoveDuplicateExtras.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Linq;
  5. using Emby.Server.Implementations.Data;
  6. using MediaBrowser.Controller;
  7. using Microsoft.Data.Sqlite;
  8. using Microsoft.Extensions.Logging;
  9. namespace Jellyfin.Server.Migrations.Routines
  10. {
  11. /// <summary>
  12. /// Remove duplicate entries which were caused by a bug where a file was considered to be an "Extra" to itself.
  13. /// </summary>
  14. [JellyfinMigration("2025-04-20T08:00:00", nameof(RemoveDuplicateExtras), "ACBE17B7-8435-4A83-8B64-6FCF162CB9BD")]
  15. #pragma warning disable CS0618 // Type or member is obsolete
  16. internal class RemoveDuplicateExtras : IMigrationRoutine
  17. #pragma warning restore CS0618 // Type or member is obsolete
  18. {
  19. private const string DbFilename = "library.db";
  20. private readonly ILogger<RemoveDuplicateExtras> _logger;
  21. private readonly IServerApplicationPaths _paths;
  22. public RemoveDuplicateExtras(ILogger<RemoveDuplicateExtras> logger, IServerApplicationPaths paths)
  23. {
  24. _logger = logger;
  25. _paths = paths;
  26. }
  27. /// <inheritdoc/>
  28. public void Perform()
  29. {
  30. var dataPath = _paths.DataPath;
  31. var dbPath = Path.Combine(dataPath, DbFilename);
  32. using var connection = new SqliteConnection($"Filename={dbPath}");
  33. connection.Open();
  34. using (var transaction = connection.BeginTransaction())
  35. {
  36. // Query the database for the ids of duplicate extras
  37. var queryResult = connection.Query("SELECT t1.Path FROM TypedBaseItems AS t1, TypedBaseItems AS t2 WHERE t1.Path=t2.Path AND t1.Type!=t2.Type AND t1.Type='MediaBrowser.Controller.Entities.Video'");
  38. var bads = string.Join(", ", queryResult.Select(x => x.GetString(0)));
  39. // Do nothing if no duplicate extras were detected
  40. if (bads.Length == 0)
  41. {
  42. _logger.LogInformation("No duplicate extras detected, skipping migration.");
  43. return;
  44. }
  45. // Back up the database before deleting any entries
  46. for (int i = 1; ; i++)
  47. {
  48. var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i);
  49. if (!File.Exists(bakPath))
  50. {
  51. try
  52. {
  53. File.Copy(dbPath, bakPath);
  54. _logger.LogInformation("Library database backed up to {BackupPath}", bakPath);
  55. break;
  56. }
  57. catch (Exception ex)
  58. {
  59. _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPath);
  60. throw;
  61. }
  62. }
  63. }
  64. // Delete all duplicate extras
  65. _logger.LogInformation("Removing found duplicated extras for the following items: {DuplicateExtras}", bads);
  66. connection.Execute("DELETE FROM TypedBaseItems WHERE rowid IN (SELECT t1.rowid FROM TypedBaseItems AS t1, TypedBaseItems AS t2 WHERE t1.Path=t2.Path AND t1.Type!=t2.Type AND t1.Type='MediaBrowser.Controller.Entities.Video')");
  67. transaction.Commit();
  68. }
  69. }
  70. }
  71. }