RemoveBuggedExtras.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using System;
  2. using System.IO;
  3. using MediaBrowser.Controller;
  4. using Microsoft.Extensions.Logging;
  5. using SQLitePCL.pretty;
  6. namespace Jellyfin.Server.Migrations.Routines
  7. {
  8. /// <summary>
  9. /// Remove duplicate entries which were caused by a bug where a file was considered to be an "Extra" to itself.
  10. /// </summary>
  11. internal class RemoveBuggedExtras : IMigrationRoutine
  12. {
  13. private const string DbFilename = "library.db";
  14. private readonly ILogger _logger;
  15. private readonly IServerApplicationPaths _paths;
  16. public RemoveBuggedExtras(ILogger<RemoveBuggedExtras> logger, IServerApplicationPaths paths)
  17. {
  18. _logger = logger;
  19. _paths = paths;
  20. }
  21. /// <inheritdoc/>
  22. public Guid Id => Guid.Parse("{ACBE17B7-8435-4A83-8B64-6FCF162CB9BD}");
  23. /// <inheritdoc/>
  24. public string Name => "RemoveBuggedExtras";
  25. /// <inheritdoc/>
  26. public void Perform()
  27. {
  28. var dataPath = _paths.DataPath;
  29. using (var connection = SQLite3.Open(
  30. Path.Combine(dataPath, DbFilename),
  31. ConnectionFlags.ReadWrite,
  32. null))
  33. {
  34. 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'");
  35. var bads = string.Join(", ", queryResult.SelectScalarString());
  36. if (bads.Length != 0)
  37. {
  38. _logger.LogInformation("Removing found duplicated extras for the following items: {0}", bads);
  39. 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')");
  40. }
  41. }
  42. }
  43. }
  44. }