2
0

RemoveDuplicateExtras.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using MediaBrowser.Controller;
  5. using Microsoft.Extensions.Logging;
  6. using SQLitePCL.pretty;
  7. namespace Jellyfin.Server.Migrations.Routines
  8. {
  9. /// <summary>
  10. /// Remove duplicate entries which were caused by a bug where a file was considered to be an "Extra" to itself.
  11. /// </summary>
  12. internal class RemoveDuplicateExtras : IMigrationRoutine
  13. {
  14. private const string DbFilename = "library.db";
  15. private readonly ILogger<RemoveDuplicateExtras> _logger;
  16. private readonly IServerApplicationPaths _paths;
  17. public RemoveDuplicateExtras(ILogger<RemoveDuplicateExtras> logger, IServerApplicationPaths paths)
  18. {
  19. _logger = logger;
  20. _paths = paths;
  21. }
  22. /// <inheritdoc/>
  23. public Guid Id => Guid.Parse("{ACBE17B7-8435-4A83-8B64-6FCF162CB9BD}");
  24. /// <inheritdoc/>
  25. public string Name => "RemoveDuplicateExtras";
  26. /// <inheritdoc/>
  27. public bool PerformOnNewInstall => false;
  28. /// <inheritdoc/>
  29. public void Perform()
  30. {
  31. var dataPath = _paths.DataPath;
  32. var dbPath = Path.Combine(dataPath, DbFilename);
  33. using (var connection = SQLite3.Open(
  34. dbPath,
  35. ConnectionFlags.ReadWrite,
  36. null))
  37. {
  38. // Query the database for the ids of duplicate extras
  39. 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'");
  40. var bads = string.Join(", ", queryResult.SelectScalarString());
  41. // Do nothing if no duplicate extras were detected
  42. if (bads.Length == 0)
  43. {
  44. _logger.LogInformation("No duplicate extras detected, skipping migration.");
  45. return;
  46. }
  47. // Back up the database before deleting any entries
  48. for (int i = 1; ; i++)
  49. {
  50. var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i);
  51. if (!File.Exists(bakPath))
  52. {
  53. try
  54. {
  55. File.Copy(dbPath, bakPath);
  56. _logger.LogInformation("Library database backed up to {BackupPath}", bakPath);
  57. break;
  58. }
  59. catch (Exception ex)
  60. {
  61. _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPath);
  62. throw;
  63. }
  64. }
  65. }
  66. // Delete all duplicate extras
  67. _logger.LogInformation("Removing found duplicated extras for the following items: {DuplicateExtras}", bads);
  68. 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')");
  69. }
  70. }
  71. }
  72. }