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