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