RemoveDuplicateExtras.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. internal class RemoveDuplicateExtras : IMigrationRoutine
  15. {
  16. private const string DbFilename = "library.db";
  17. private readonly ILogger<RemoveDuplicateExtras> _logger;
  18. private readonly IServerApplicationPaths _paths;
  19. public RemoveDuplicateExtras(ILogger<RemoveDuplicateExtras> logger, IServerApplicationPaths paths)
  20. {
  21. _logger = logger;
  22. _paths = paths;
  23. }
  24. /// <inheritdoc/>
  25. public Guid Id => Guid.Parse("{ACBE17B7-8435-4A83-8B64-6FCF162CB9BD}");
  26. /// <inheritdoc/>
  27. public string Name => "RemoveDuplicateExtras";
  28. /// <inheritdoc/>
  29. public bool PerformOnNewInstall => false;
  30. /// <inheritdoc/>
  31. public void Perform()
  32. {
  33. var dataPath = _paths.DataPath;
  34. var dbPath = Path.Combine(dataPath, DbFilename);
  35. using var connection = new SqliteConnection($"Filename={dbPath}");
  36. connection.Open();
  37. using (var transaction = connection.BeginTransaction())
  38. {
  39. // Query the database for the ids of duplicate extras
  40. 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'");
  41. var bads = string.Join(", ", queryResult.Select(x => x.GetString(0)));
  42. // Do nothing if no duplicate extras were detected
  43. if (bads.Length == 0)
  44. {
  45. _logger.LogInformation("No duplicate extras detected, skipping migration.");
  46. return;
  47. }
  48. // Back up the database before deleting any entries
  49. for (int i = 1; ; i++)
  50. {
  51. var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i);
  52. if (!File.Exists(bakPath))
  53. {
  54. try
  55. {
  56. File.Copy(dbPath, bakPath);
  57. _logger.LogInformation("Library database backed up to {BackupPath}", bakPath);
  58. break;
  59. }
  60. catch (Exception ex)
  61. {
  62. _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPath);
  63. throw;
  64. }
  65. }
  66. }
  67. // Delete all duplicate extras
  68. _logger.LogInformation("Removing found duplicated extras for the following items: {DuplicateExtras}", bads);
  69. 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')");
  70. transaction.Commit();
  71. }
  72. }
  73. }
  74. }