| | 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 | |
|
| | 10 | | namespace Jellyfin.Server.Migrations.Routines; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Remove duplicate entries which were caused by a bug where a file was considered to be an "Extra" to itself. |
| | 14 | | /// </summary> |
| | 15 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | 16 | | [JellyfinMigration("2025-04-20T08:00:00", nameof(RemoveDuplicateExtras), "ACBE17B7-8435-4A83-8B64-6FCF162CB9BD")] |
| | 17 | | internal class RemoveDuplicateExtras : IMigrationRoutine |
| | 18 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | 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 void Perform() |
| | 32 | | { |
| 0 | 33 | | var dataPath = _paths.DataPath; |
| 0 | 34 | | var dbPath = Path.Combine(dataPath, DbFilename); |
| 0 | 35 | | using var connection = new SqliteConnection($"Filename={dbPath}"); |
| 0 | 36 | | connection.Open(); |
| 0 | 37 | | using (var transaction = connection.BeginTransaction()) |
| | 38 | | { |
| | 39 | | // Query the database for the ids of duplicate extras |
| 0 | 40 | | var queryResult = connection.Query("SELECT t1.Path FROM TypedBaseItems AS t1, TypedBaseItems AS t2 WHERE t1. |
| 0 | 41 | | var bads = string.Join(", ", queryResult.Select(x => x.GetString(0))); |
| | 42 | |
|
| | 43 | | // Do nothing if no duplicate extras were detected |
| 0 | 44 | | if (bads.Length == 0) |
| | 45 | | { |
| 0 | 46 | | _logger.LogInformation("No duplicate extras detected, skipping migration."); |
| 0 | 47 | | return; |
| | 48 | | } |
| | 49 | |
|
| | 50 | | // Back up the database before deleting any entries |
| 0 | 51 | | for (int i = 1; ; i++) |
| | 52 | | { |
| 0 | 53 | | var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i); |
| 0 | 54 | | if (!File.Exists(bakPath)) |
| | 55 | | { |
| | 56 | | try |
| | 57 | | { |
| 0 | 58 | | File.Copy(dbPath, bakPath); |
| 0 | 59 | | _logger.LogInformation("Library database backed up to {BackupPath}", bakPath); |
| 0 | 60 | | break; |
| | 61 | | } |
| 0 | 62 | | catch (Exception ex) |
| | 63 | | { |
| 0 | 64 | | _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPa |
| 0 | 65 | | throw; |
| | 66 | | } |
| | 67 | | } |
| | 68 | | } |
| | 69 | |
|
| | 70 | | // Delete all duplicate extras |
| 0 | 71 | | _logger.LogInformation("Removing found duplicated extras for the following items: {DuplicateExtras}", bads); |
| 0 | 72 | | connection.Execute("DELETE FROM TypedBaseItems WHERE rowid IN (SELECT t1.rowid FROM TypedBaseItems AS t1, Ty |
| 0 | 73 | | transaction.Commit(); |
| 0 | 74 | | } |
| 0 | 75 | | } |
| | 76 | | } |